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. Qt setStyleSheet it not working
Forum Updated to NodeBB v4.3 + New Features

Qt setStyleSheet it not working

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.4k Views 3 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.
  • Y Offline
    Y Offline
    yugosonegi
    wrote on 30 Dec 2017, 01:58 last edited by
    #1

    I have a class called MyWidget and I'm trying to load a global style using this code on my main.cpp:

    QFile file(":/style.qss");
    file.open(QFile::ReadOnly);
    QString stylesheet = file.readAll();
    application.setStyleSheet(stylesheet);
    

    And it's working, the problem now is that I want to change in that file the background color of my MyWidget. I tried to use the setObjectName('MyWidget') and do something like:

    QWidget#MyWidget {
        background-color: yellow;
    }
    

    But it doesn't work. What to do?

    K 1 Reply Last reply 30 Dec 2017, 05:44
    0
    • Y yugosonegi
      30 Dec 2017, 01:58

      I have a class called MyWidget and I'm trying to load a global style using this code on my main.cpp:

      QFile file(":/style.qss");
      file.open(QFile::ReadOnly);
      QString stylesheet = file.readAll();
      application.setStyleSheet(stylesheet);
      

      And it's working, the problem now is that I want to change in that file the background color of my MyWidget. I tried to use the setObjectName('MyWidget') and do something like:

      QWidget#MyWidget {
          background-color: yellow;
      }
      

      But it doesn't work. What to do?

      K Offline
      K Offline
      kenchan
      wrote on 30 Dec 2017, 05:44 last edited by
      #2

      @yugosonegi
      If MyWidget is a subclass of QWidget did you see the docs related to this...

      "QWidget
      Supports only the background, background-clip and background-origin properties.
      If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

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

      The above code is a no-operation if there is no stylesheet set.
      Warning: Make sure you define the Q_OBJECT macro for your custom widget."

      Other than that it should work.

      1 Reply Last reply
      4
      • Y Offline
        Y Offline
        yugosonegi
        wrote on 30 Dec 2017, 15:47 last edited by
        #3

        With your answer it means that I can't chance the background the way I'm doing? I can't understand your answer right.

        M 1 Reply Last reply 30 Dec 2017, 16:34
        0
        • Y yugosonegi
          30 Dec 2017, 15:47

          With your answer it means that I can't chance the background the way I'm doing? I can't understand your answer right.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 30 Dec 2017, 16:34 last edited by mrjj
          #4

          @yugosonegi

          Hi
          mr @kenchan says

          If you MyWidget is a class you made yourself. ( versus one of Qts just named MyClass)
          you need to do as he shows in your class
          paintEvent for a stylesheet to have any effect.

          So is MyWidget your own class ?
          as in
          class MyWidget : QWidget {}

          1 Reply Last reply
          1
          • Y Offline
            Y Offline
            yugosonegi
            wrote on 30 Dec 2017, 17:53 last edited by
            #5

            Yes, it's a subclass of QWidget.

            M 1 Reply Last reply 31 Dec 2017, 09:05
            1
            • Y yugosonegi
              30 Dec 2017, 17:53

              Yes, it's a subclass of QWidget.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 31 Dec 2017, 09:05 last edited by mrjj
              #6

              @yugosonegi
              Ok, for such a custom widget , you must use the code shown in paintEvent for it to use
              stylesheet at all. ( you can have extra code for own drawing but QStyleOption and drawPrimitive
              should be used also/first)

              1 Reply Last reply
              1

              4/6

              30 Dec 2017, 16:34

              • Login

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