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

Qt setStyleSheet it not working

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 5.4k 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.
  • Y Offline
    Y Offline
    yugosonegi
    wrote on 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
    0
    • Y yugosonegi

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

        mrjjM 1 Reply Last reply
        0
        • Y yugosonegi

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on 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 last edited by
            #5

            Yes, it's a subclass of QWidget.

            mrjjM 1 Reply Last reply
            1
            • Y yugosonegi

              Yes, it's a subclass of QWidget.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on 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

              • Login

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