Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change property of a class during runtime
QtWS25 Last Chance

Change property of a class during runtime

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstyleqspinbox
1 Posts 1 Posters 224 Views
  • 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.
  • C Offline
    C Offline
    CJha
    wrote on 23 Jul 2021, 10:01 last edited by CJha
    #1

    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
    0

    1/1

    23 Jul 2021, 10:01

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved