QDoubleSPINBOX text color not changing



  • Tried about 20 different ways to try to change the text color in a DoubleSpinBox. it somehow gets created as a SpinBox
    even thought the ui header code shows it being used as a double spin box ... anyhow , so I have to pass in a regular spinbox object to the function. But no matter what I try , I can't change the text color ... Here is some code modified from an example that is supposed to work with a regular spinbox ... Any ideas ? I know I sure ran out of them...

    void main::memberfunction(QSpinBox *inpBox) {
    QPalette *p = new QPalette();
    p->setColor(QPalette::Text, QColor(161, 161, 161));
    inpBox->setPalette(*p);

    // inpBox.update(); ????????????
    }



  • Uhhh, too many long nights of programming. I did forget that I had to spinboxes as well as 2 doublespin boxes. OMG me head is spinning already......



  • Hmm, even after changing the function over to accepting the DoubleSpin boxes , it still won't change text color. Updating the spinbox was just desperation :)

    void MainWindow::memberfunction(QDoubleSpinBox *inpBox)
    {
    QPalette *p = new QPalette();
    p->setColor(QPalette::Text, QColor(161, 161, 161));
    inpBox->setPalette(*p);
    inpBox->update();

    }


  • Moderators

    That code is leaking memory(palette instance). Data struct types like QPalette should always be created on the stack.

    As for the question - how a widget looks is style dependent. Some styles might not use certain palette colors for certain elements at all. Even if you manage to set a palette the way you want on your computer it's not gonna look the same everywhere. Also, if a user changes style at runtime, your widget won't update properly, as you override the default palette.

    Changing palettes for a widget is usually not a good idea. If you want to force a specific color on the widget you can use stylesheet:

    inpBox->setStyleSheet("color: rgb(161,161,161); ");
    

    Note though, that setting some style attributes might disable the native styling for the widget, for example setting a border color will most definitely do that.



  • You are right about the memory leak . I had earlier coded it so that it would not need a pointer or the new operator..this particular code should have to delete qpalette to free up resources :) Now I had tried code similar to that for setting stylesheet ,but had used ::Text in the parameters ,like I had seen in an example online. Seems the stylesheet was even easier to set than I had thought !!! Thanks , where do I click to make this issue solved ?


  • Moderators

    where do I click to make this issue solved ?

    There's a "Mark as solved" option in the "Topic Tools" button menu.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.