Make initStyleOption virtual for various classes

Often a simple change in the styleoptions is needed in e.g a paintEvent,
but it was impossible without copying the whole paint-function which
is not always trivial and also wouldn't be kept up-to-date if it was
changed in Qt.
The initStyleOption is similar to viewOptions in QAbstractTableView
and it is handy that this function is virtual. Furthermore
QStyledItemDelegate::initStyleOption is already virtual.

This change only makes initStyleOption virtual for public classes.

[ChangeLog][QtWidgets] InitStyleOption has been made virtual in
public Qt classes to make override of stylebehavior more simple.

Change-Id: I38974c6d4dd0793ca5976ecf3aa28892215a1579
Task-number: QTBUG-77642
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
This commit is contained in:
Thorbjørn Lund Martsum 2020-07-15 09:24:20 +02:00
parent 9c0f448f5a
commit 24d60cb64d
27 changed files with 39 additions and 23 deletions

View File

@ -3385,6 +3385,14 @@ void QHeaderView::initStyleOption(QStyleOptionHeader *option) const
option->section = 0;
}
void QHeaderView::initStyleOption(QStyleOptionFrame *option) const
{
// The QFrame version is only here to avoid compiler warnings.
// If invoked (it can be invoked with QFrame *f = headerview; f->initStyleOption()),
// and here we just pass it on to the frame version.
QFrame::initStyleOption(option);
}
bool QHeaderViewPrivate::isSectionSelected(int section) const
{
int i = section * 2;

View File

@ -230,12 +230,14 @@ protected:
QModelIndex moveCursor(CursorAction, Qt::KeyboardModifiers) override;
void setSelection(const QRect& rect, QItemSelectionModel::SelectionFlags flags) override;
QRegion visualRegionForSelection(const QItemSelection &selection) const override;
void initStyleOption(QStyleOptionHeader *option) const;
virtual void initStyleOption(QStyleOptionHeader *option) const;
friend class QTableView;
friend class QTreeView;
private:
void initStyleOption(QStyleOptionFrame *option) const override;
// ### Qt6: make them protected slots in QHeaderViewPrivate
Q_PRIVATE_SLOT(d_func(), void _q_sectionsRemoved(const QModelIndex &parent, int logicalFirst, int logicalLast))
Q_PRIVATE_SLOT(d_func(), void _q_sectionsAboutToBeMoved(const QModelIndex &sourceParent, int logicalStart, int logicalEnd, const QModelIndex &destinationParent, int logicalDestination))

View File

@ -1629,6 +1629,8 @@ void QAbstractSpinBoxPrivate::init()
edit->setValidator(validator);
QStyleOptionSpinBox opt;
// ### This is called from the ctor and thus we shouldn't call initStyleOption yet
// ### as we only call the base class implementation of initStyleOption called.
q->initStyleOption(&opt);
spinClickTimerInterval = q->style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatRate, &opt, q);
spinClickThresholdTimerInterval = q->style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatThreshold, &opt, q);

View File

@ -160,7 +160,7 @@ protected:
void timerEvent(QTimerEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void showEvent(QShowEvent *event) override;
void initStyleOption(QStyleOptionSpinBox *option) const;
virtual void initStyleOption(QStyleOptionSpinBox *option) const;
QLineEdit *lineEdit() const;
void setLineEdit(QLineEdit *edit);

View File

@ -81,7 +81,7 @@ protected:
void nextCheckState() override;
void paintEvent(QPaintEvent *) override;
void mouseMoveEvent(QMouseEvent *) override;
void initStyleOption(QStyleOptionButton *option) const;
virtual void initStyleOption(QStyleOptionButton *option) const;
private:

View File

@ -230,7 +230,7 @@ protected:
void contextMenuEvent(QContextMenuEvent *e) override;
#endif // QT_NO_CONTEXTMENU
void inputMethodEvent(QInputMethodEvent *) override;
void initStyleOption(QStyleOptionComboBox *option) const;
virtual void initStyleOption(QStyleOptionComboBox *option) const;
protected:

View File

@ -193,7 +193,7 @@ protected:
StepEnabled stepEnabled() const override;
void mousePressEvent(QMouseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void initStyleOption(QStyleOptionSpinBox *option) const;
void initStyleOption(QStyleOptionSpinBox *option) const override;
QDateTimeEdit(const QVariant &val, QMetaType::Type parserType, QWidget *parent = nullptr);
private:

View File

@ -90,7 +90,7 @@ protected:
void mouseMoveEvent(QMouseEvent *me) override;
void sliderChange(SliderChange change) override;
void initStyleOption(QStyleOptionSlider *option) const;
virtual void initStyleOption(QStyleOptionSlider *option) const;
private:

View File

@ -116,7 +116,7 @@ protected:
void closeEvent(QCloseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
bool event(QEvent *event) override;
void initStyleOption(QStyleOptionDockWidget *option) const;
virtual void initStyleOption(QStyleOptionDockWidget *option) const;
private:
Q_DECLARE_PRIVATE(QDockWidget)

View File

@ -64,7 +64,7 @@ protected:
bool eventFilter(QObject *, QEvent *) override;
void paintEvent(QPaintEvent *) override;
void initStyleOption(QStyleOption *option) const;
virtual void initStyleOption(QStyleOption *option) const;
private:
Q_DECLARE_PRIVATE(QFocusFrame)

View File

@ -116,7 +116,7 @@ protected:
protected:
QFrame(QFramePrivate &dd, QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
void initStyleOption(QStyleOptionFrame *option) const;
virtual void initStyleOption(QStyleOptionFrame *option) const;
private:
Q_DISABLE_COPY(QFrame)

View File

@ -94,7 +94,7 @@ protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void initStyleOption(QStyleOptionGroupBox *option) const;
virtual void initStyleOption(QStyleOptionGroupBox *option) const;
private:

View File

@ -229,7 +229,7 @@ protected:
#endif
void inputMethodEvent(QInputMethodEvent *) override;
void initStyleOption(QStyleOptionFrame *option) const;
virtual void initStyleOption(QStyleOptionFrame *option) const;
public:
QVariant inputMethodQuery(Qt::InputMethodQuery) const override;
Q_INVOKABLE QVariant inputMethodQuery(Qt::InputMethodQuery property, QVariant argument) const;

View File

@ -257,7 +257,7 @@ protected:
void timerEvent(QTimerEvent *) override;
bool event(QEvent *) override;
bool focusNextPrevChild(bool next) override;
void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
private Q_SLOTS:
void internalDelayedPopup();

View File

@ -150,7 +150,7 @@ protected:
void timerEvent(QTimerEvent *) override;
bool eventFilter(QObject *, QEvent *) override;
bool event(QEvent *) override;
void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
virtual void initStyleOption(QStyleOptionMenuItem *option, const QAction *action) const;
private:
Q_DECLARE_PRIVATE(QMenuBar)

View File

@ -107,6 +107,8 @@ void QProgressBarPrivate::resetLayoutItemMargins()
{
Q_Q(QProgressBar);
QStyleOptionProgressBar option;
// ### It seems like this can be called directly from the constructor which should be avoided
// ### if possible, since we will not call a possible re-implemented version
q->initStyleOption(&option);
setLayoutItemMargins(QStyle::SE_ProgressBarLayoutItem, &option);
}

View File

@ -111,7 +111,7 @@ Q_SIGNALS:
protected:
bool event(QEvent *e) override;
void paintEvent(QPaintEvent *) override;
void initStyleOption(QStyleOptionProgressBar *option) const;
virtual void initStyleOption(QStyleOptionProgressBar *option) const;
private:
Q_DECLARE_PRIVATE(QProgressBar)

View File

@ -93,7 +93,7 @@ protected:
void keyPressEvent(QKeyEvent *) override;
void focusInEvent(QFocusEvent *) override;
void focusOutEvent(QFocusEvent *) override;
void initStyleOption(QStyleOptionButton *option) const;
virtual void initStyleOption(QStyleOptionButton *option) const;
bool hitButton(const QPoint &pos) const override;
QPushButton(QPushButtonPrivate &dd, QWidget* parent = nullptr);

View File

@ -68,7 +68,7 @@ protected:
bool hitButton(const QPoint &) const override;
void paintEvent(QPaintEvent *) override;
void mouseMoveEvent(QMouseEvent *) override;
void initStyleOption(QStyleOptionButton *button) const;
virtual void initStyleOption(QStyleOptionButton *button) const;
private:

View File

@ -79,7 +79,7 @@ protected:
void showEvent(QShowEvent *) override;
void resizeEvent(QResizeEvent *) override;
void moveEvent(QMoveEvent *) override;
void initStyleOption(QStyleOptionRubberBand *option) const;
virtual void initStyleOption(QStyleOptionRubberBand *option) const;
private:
Q_DECLARE_PRIVATE(QRubberBand)

View File

@ -76,7 +76,7 @@ protected:
#ifndef QT_NO_CONTEXTMENU
void contextMenuEvent(QContextMenuEvent *) override;
#endif
void initStyleOption(QStyleOptionSlider *option) const;
virtual void initStyleOption(QStyleOptionSlider *option) const;
private:

View File

@ -91,6 +91,8 @@ void QSliderPrivate::resetLayoutItemMargins()
{
Q_Q(QSlider);
QStyleOptionSlider opt;
// ### This is (also) reached from the ctor which is unfortunate since a possible
// ### re-implementation of initStyleOption is then not called.
q->initStyleOption(&opt);
setLayoutItemMargins(QStyle::SE_SliderLayoutItem, &opt);
}

View File

@ -89,7 +89,7 @@ protected:
void mousePressEvent(QMouseEvent *ev) override;
void mouseReleaseEvent(QMouseEvent *ev) override;
void mouseMoveEvent(QMouseEvent *ev) override;
void initStyleOption(QStyleOptionSlider *option) const;
virtual void initStyleOption(QStyleOptionSlider *option) const;
private:

View File

@ -212,7 +212,7 @@ protected:
void keyPressEvent(QKeyEvent *) override;
void changeEvent(QEvent *) override;
void timerEvent(QTimerEvent *event) override;
void initStyleOption(QStyleOptionTab *option, int tabIndex) const;
virtual void initStyleOption(QStyleOptionTab *option, int tabIndex) const;
#ifndef QT_NO_ACCESSIBILITY
friend class QAccessibleTabBar;

View File

@ -171,7 +171,7 @@ protected:
void setTabBar(QTabBar *);
void changeEvent(QEvent *) override;
bool event(QEvent *) override;
void initStyleOption(QStyleOptionTabWidgetFrame *option) const;
virtual void initStyleOption(QStyleOptionTabWidgetFrame *option) const;
private:

View File

@ -181,7 +181,7 @@ protected:
void changeEvent(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
bool event(QEvent *event) override;
void initStyleOption(QStyleOptionToolBar *option) const;
virtual void initStyleOption(QStyleOptionToolBar *option) const;
private:

View File

@ -119,7 +119,7 @@ protected:
bool hitButton(const QPoint &pos) const override;
void nextCheckState() override;
void initStyleOption(QStyleOptionToolButton *option) const;
virtual void initStyleOption(QStyleOptionToolButton *option) const;
private:
Q_DISABLE_COPY(QToolButton)