Change property of a class during runtime
Unsolved
General and Desktop
-
Hi, I would like to change the
QAbstractSpinBox::ButtonSymbols
during runtime. I tried changing it in my own subclass ofQProxyStyle
:void GuiStyle::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const { if((control == QStyle::CC_SpinBox) && (spinBoxBtns == false)) { const QStyleOptionSpinBox* buttonOption = qstyleoption_cast<const QStyleOptionSpinBox*>(option); QStyleOptionSpinBox newBtnOption = *buttonOption; if(spinBoxBtns) newBtnOption.buttonSymbols = QAbstractSpinBox::UpDownArrows; else newBtnOption.buttonSymbols = QAbstractSpinBox::NoButtons; QProxyStyle::drawComplexControl(control, &newBtnOption, painter, widget); } else { QProxyStyle::drawComplexControl(control, option, painter, widget); } }
but it ends up not readjusting the alignment of the text and so if I remove the up-down button then the text ends up being not in the centre anymore. Then I tried doing it on a per-object basis:
void applySpinBoxButtons(bool val) { QWidgetList allWidgets = qApp->allWidgets(); for(QWidget* wid : allWidgets) { QString str = wid->metaObject()->className(); if(str == "QSpinBox") { QSpinBox* spin = static_cast<QSpinBox*>(wid); if(val) spin->setButtonSymbols(QAbstractSpinBox::UpDownArrows); else spin->setButtonSymbols(QAbstractSpinBox::NoButtons); } } }
It works, but this does not affect any new
QSpinBox
that is created (e.g. inQColorDialog
). Is there any way I can do this on a class basis likeQApplication
allows fonts and palettes to be set on a class basis usingvoid QApplication::setPalette(const QPalette &palette, const char *className = nullptr)
?