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. Application-Wide Stylesheet not applied on widget
Forum Updated to NodeBB v4.3 + New Features

Application-Wide Stylesheet not applied on widget

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 4 Posters 5.3k Views 4 Watching
  • 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.
  • jensen82J jensen82

    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!

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

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

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • jensen82J Offline
      jensen82J Offline
      jensen82
      wrote on last edited by
      #3

      Hi!

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

      Thank you!

      raven-worxR 1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #4

        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.

        jensen82J 2 Replies Last reply
        0
        • jensen82J jensen82

          Hi!

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

          Thank you!

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #5

          @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.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          jensen82J 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            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.

            jensen82J Offline
            jensen82J Offline
            jensen82
            wrote on last edited by
            #6

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

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @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.

              jensen82J Offline
              jensen82J Offline
              jensen82
              wrote on last edited by
              #7

              @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;
              };
              
              raven-worxR 2 Replies Last reply
              0
              • jensen82J jensen82

                @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;
                };
                
                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #8

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

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  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.

                  jensen82J Offline
                  jensen82J Offline
                  jensen82
                  wrote on last edited by
                  #9

                  @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

                  1 Reply Last reply
                  0
                  • jensen82J jensen82

                    @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;
                    };
                    
                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #10

                    @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.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by A Former User
                      #11

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

                      jensen82J 1 Reply Last reply
                      0
                      • ? A Former User

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

                        jensen82J Offline
                        jensen82J Offline
                        jensen82
                        wrote on last edited by
                        #12

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

                        1 Reply Last reply
                        0
                        • jensen82J Offline
                          jensen82J Offline
                          jensen82
                          wrote on last edited by
                          #13

                          Thank you all for your help!

                          1 Reply Last reply
                          0

                          • Login

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