Make QLayout::spacing/setSpacing virtual, remove qobject_cast hack

Change-Id: If256609a1f561b957378010d88120f5aaf94a45e
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
This commit is contained in:
Volker Hilsheimer 2020-06-08 18:28:52 +02:00
parent 385f0732d9
commit fd856532d7
5 changed files with 16 additions and 39 deletions

View File

@ -83,8 +83,8 @@ public:
void insertLayout(int index, QLayout *layout, int stretch = 0);
void insertItem(int index, QLayoutItem *);
int spacing() const;
void setSpacing(int spacing);
int spacing() const override;
void setSpacing(int spacing) override;
bool setStretchFactor(QWidget *w, int stretch);
bool setStretchFactor(QLayout *l, int stretch);

View File

@ -105,8 +105,8 @@ public:
void setVerticalSpacing(int spacing);
int verticalSpacing() const;
int spacing() const;
void setSpacing(int);
int spacing() const override;
void setSpacing(int) override;
void addRow(QWidget *label, QWidget *field);
void addRow(QWidget *label, QLayout *field);

View File

@ -74,8 +74,8 @@ public:
int horizontalSpacing() const;
void setVerticalSpacing(int spacing);
int verticalSpacing() const;
void setSpacing(int spacing);
int spacing() const;
void setSpacing(int spacing) override;
int spacing() const override;
void setRowStretch(int row, int stretch);
void setColumnStretch(int column, int stretch);

View File

@ -55,9 +55,6 @@
#include "qvariant.h"
#include "qwidget_p.h"
#include "qlayout_p.h"
#if QT_CONFIG(formlayout)
#include "qformlayout.h"
#endif
QT_BEGIN_NAMESPACE
@ -301,40 +298,20 @@ bool QLayout::setAlignment(QLayout *l, Qt::Alignment alignment)
int QLayout::spacing() const
{
if (const QBoxLayout* boxlayout = qobject_cast<const QBoxLayout*>(this)) {
return boxlayout->spacing();
} else if (const QGridLayout* gridlayout = qobject_cast<const QGridLayout*>(this)) {
return gridlayout->spacing();
#if QT_CONFIG(formlayout)
} else if (const QFormLayout* formlayout = qobject_cast<const QFormLayout*>(this)) {
return formlayout->spacing();
#endif
Q_D(const QLayout);
if (d->insideSpacing >=0) {
return d->insideSpacing;
} else {
Q_D(const QLayout);
if (d->insideSpacing >=0) {
return d->insideSpacing;
} else {
// arbitrarily prefer horizontal spacing to vertical spacing
return qSmartSpacing(this, QStyle::PM_LayoutHorizontalSpacing);
}
// arbitrarily prefer horizontal spacing to vertical spacing
return qSmartSpacing(this, QStyle::PM_LayoutHorizontalSpacing);
}
}
void QLayout::setSpacing(int spacing)
{
if (QBoxLayout* boxlayout = qobject_cast<QBoxLayout*>(this)) {
boxlayout->setSpacing(spacing);
} else if (QGridLayout* gridlayout = qobject_cast<QGridLayout*>(this)) {
gridlayout->setSpacing(spacing);
#if QT_CONFIG(formlayout)
} else if (QFormLayout* formlayout = qobject_cast<QFormLayout*>(this)) {
formlayout->setSpacing(spacing);
#endif
} else {
Q_D(QLayout);
d->insideSpacing = spacing;
invalidate();
}
Q_D(QLayout);
d->insideSpacing = spacing;
invalidate();
}
/*!

View File

@ -80,8 +80,8 @@ public:
QLayout();
~QLayout();
int spacing() const;
void setSpacing(int);
virtual int spacing() const;
virtual void setSpacing(int);
void setContentsMargins(int left, int top, int right, int bottom);
void setContentsMargins(const QMargins &margins);