Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Change property of a class during runtime

    General and Desktop
    qstyle qspinbox
    1
    1
    80
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • CJha
      CJha last edited by CJha

      Hi, I would like to change the QAbstractSpinBox::ButtonSymbols during runtime. I tried changing it in my own subclass of QProxyStyle:

      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. in QColorDialog). Is there any way I can do this on a class basis like QApplication allows fonts and palettes to be set on a class basis using void QApplication::setPalette(const QPalette &palette, const char *className = nullptr)?

      1 Reply Last reply Reply Quote 0
      • First post
        Last post