Application-Wide Stylesheet not applied on widget



  • Hi,

    i have subclassed widget and want to apply a application-wide stylesheet. I'm loading the stylesheet and after that i create/construct the widgets. The base widget (AUTBaseWidget from QWidget) has an implemented paintEvent() to ensure the stylesheet is applied to this widget. It works for a bunch of widget but on widgets which are childs of the centralwidget not. The color seems to be mixed with the underlying widget. But when i set the stylesheet in the constructor of my widget, it does...the correct color is displayed. What's wrong?

    Some code following...

    /*The style.qss that i load works for all widgets except for this*/
    TopNavigationLine {
        background-color: rbg(77, 77, 77);
        min-height: 50px;
        max-height: 50px;
    }
    
    /*In the constructor it works:*/
    
    TopNavigationLine::TopNavigationLine(QWidget *parent) :
        AUTBaseWidget (parent),
        ui(new Ui::TopNavigationLine)
    {
        ui->setupUi(this);
        setLayout(ui->m_layMain);
        setObjectName("TopNavigationLine");
        ui->m_tbSelectLanguage->setObjectName("SelectLanguageButton");
        show();
        setStyleSheet("background-color: rgb(77 , 77, 77);");
    }
    

    Here are two pics:
    alt text
    This is the result if i load it from qss-file

    alt text
    This is the result if i use it in constructor

    Thank you for your help!


  • Moderators

    @jensen82
    Did you maybe forget to include Q_OBJECT macro in the header file of TopNavigationLine class?



  • Hi!

    No, the MACRO is there. If i've had forgotten the MACRO the setStyleSheet in the constructor wouldn't work.

    Thank you!


  • Moderators

    Sounds like a problem with missing/incorrect implementation of the paintEvent. Can you show it?

    Btw. Calling show() from a constructor is bad manners. What if someone just wants an instance of your class without showing it (e.g. to put it in a stack or a tab)? Don't decide for them.


  • Moderators

    @jensen82 said in Application-Wide Stylesheet not applied on widget:

    No, the MACRO is there. If i've had forgotten the MACRO the setStyleSheet in the constructor wouldn't work.

    no, since you're not using a class name in this style declaration...

    Show the definition in the QSS file before the one that isn't working. Maybe you've a parsing error in it.



  • @Chris-Kawa Yes, i know. The show was for testing purposes.



  • @raven-worx The parsing-test was successful. No parsing error present.

    Here's the Headerfile for TopNavigationLine:

    class TopNavigationLine : public AUTBaseWidget {
        Q_OBJECT
        public:
        explicit TopNavigationLine(QWidget* parent = 0);
        ~TopNavigationLine();
    
        private:
        Ui::TopNavigationLine* ui;
    };
    

  • Moderators

    @jensen82
    you should please provide what you were asked for in order to receive proper help.



  • @Chris-Kawa So here's the paintEvent

    void AUTBaseWidget::paintEvent(QPaintEvent* event)
    {
        Q_UNUSED(event);
        QStyleOption opt;
        opt.init(this);
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    }
    
    

    This code works for other derived widgets


  • Moderators

    @jensen82 said in Application-Wide Stylesheet not applied on widget:

    The parsing-test was successful. No parsing error present.

    i am wondering how you exactly checked for an error?
    A normal CSS parser isn't appropriate here btw.



  • Isn't there a superfluous semi-colon in setStyleSheet("background-color: rgb(77 , 77, 77);"); ?



  • @Ian-Bray No. I think i got the error. I saved the file in UTF-8 and now it works. Strange...



  • Thank you all for your help!


Log in to reply
 

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