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. How to propagate/limit stylesheets through code
Forum Updated to NodeBB v4.3 + New Features

How to propagate/limit stylesheets through code

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 776 Views 1 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.
  • T Offline
    T Offline
    Taytoo
    wrote on 7 Jun 2020, 17:09 last edited by
    #1

    I have a main stylesheet file (qss) with styles for various controls (QDialog, QLabel, QEdit etc) defined all in one file. In my program I need to show certain windows with this stylesheet and others without (e.g. show native file dialog, messagebox etc with system appearance).

    Noticed following odd behavior:

    1. If I copy/paste style to each Dialog's stylesheet property in Qt Designer, then all controls/widgets update correctly as well, implying that child widgets are getting stylesheet from parent. The downside is that I have to copy stylesheet and update each dialog at design time - so not an option.

    2. If I programmatically read the qss file into QString and then apply that to dialog via setStylesheet property, then only dialog's appearance seems to change, all the controls/widgets on dialog still have default appearance. This is odd, why does it work in Qt Designer though?

    3. If I use QApplication:setStylesheet() method, then all dialogs and child controls have correct appearance. However, now even the windows I don't want styled have stylesheet applied.

    I'd like to go with option 2, but why isn't it working? Also, even when it works, I've noticed that all child widgets (e.g. QMessageBox) get stylesheet applied even if I set QMessageBox::setStyleSheet("")?

    J 1 Reply Last reply 7 Jun 2020, 18:19
    0
    • T Offline
      T Offline
      Taytoo
      wrote 14 days ago last edited by
      #5

      5 years later, I've stumbled on the solution for Option 2. What I was doing wrong was not calling unpolish() and polish() methods. The correct order is call unpolish(), setstylesheet(), and polish(). Now the styles are applied correctly on all child widgets.

      1 Reply Last reply
      2
      • T Taytoo
        7 Jun 2020, 17:09

        I have a main stylesheet file (qss) with styles for various controls (QDialog, QLabel, QEdit etc) defined all in one file. In my program I need to show certain windows with this stylesheet and others without (e.g. show native file dialog, messagebox etc with system appearance).

        Noticed following odd behavior:

        1. If I copy/paste style to each Dialog's stylesheet property in Qt Designer, then all controls/widgets update correctly as well, implying that child widgets are getting stylesheet from parent. The downside is that I have to copy stylesheet and update each dialog at design time - so not an option.

        2. If I programmatically read the qss file into QString and then apply that to dialog via setStylesheet property, then only dialog's appearance seems to change, all the controls/widgets on dialog still have default appearance. This is odd, why does it work in Qt Designer though?

        3. If I use QApplication:setStylesheet() method, then all dialogs and child controls have correct appearance. However, now even the windows I don't want styled have stylesheet applied.

        I'd like to go with option 2, but why isn't it working? Also, even when it works, I've noticed that all child widgets (e.g. QMessageBox) get stylesheet applied even if I set QMessageBox::setStyleSheet("")?

        J Offline
        J Offline
        JonB
        wrote on 7 Jun 2020, 18:19 last edited by JonB 6 Jul 2020, 18:20
        #2

        @Taytoo
        For option 2: Qt Designer is not magic, look at the generated ui....h file and compare what it's generating against how you're doing it.

        For option 3, one way is you could be more selective in your stylesheets selectors. Don't forget e.g.

        #SpecificDialog QPushButton
        

        only applies to buttons on that named dialog. There are classes too.

        T 1 Reply Last reply 7 Jun 2020, 19:56
        1
        • J JonB
          7 Jun 2020, 18:19

          @Taytoo
          For option 2: Qt Designer is not magic, look at the generated ui....h file and compare what it's generating against how you're doing it.

          For option 3, one way is you could be more selective in your stylesheets selectors. Don't forget e.g.

          #SpecificDialog QPushButton
          

          only applies to buttons on that named dialog. There are classes too.

          T Offline
          T Offline
          Taytoo
          wrote on 7 Jun 2020, 19:56 last edited by Taytoo 6 Jul 2020, 19:59
          #3

          @JonB said in How to propagate/limit stylesheets through code:

          @Taytoo
          For option 2: Qt Designer is not magic, look at the generated ui....h file and compare what it's generating against how you're doing it.

          It's not doing anything special, just setting stylesheet for QDialog and nowhere else.

          For option 3, one way is you could be more selective in your stylesheets selectors. Don't forget e.g.

          #SpecificDialog QPushButton
          

          only applies to buttons on that named dialog. There are classes too.

          Defining custom classes gets too tricky e.g. with QStackedWidget and QTabWidget, I tried specifying a custom class but for some reason the style just wasn't applying

          There has to be a reason why setstylesheet works for all widgets at QDialog level when set in Designer but fails when doing it in code?

          J 1 Reply Last reply 8 Jun 2020, 08:13
          0
          • T Taytoo
            7 Jun 2020, 19:56

            @JonB said in How to propagate/limit stylesheets through code:

            @Taytoo
            For option 2: Qt Designer is not magic, look at the generated ui....h file and compare what it's generating against how you're doing it.

            It's not doing anything special, just setting stylesheet for QDialog and nowhere else.

            For option 3, one way is you could be more selective in your stylesheets selectors. Don't forget e.g.

            #SpecificDialog QPushButton
            

            only applies to buttons on that named dialog. There are classes too.

            Defining custom classes gets too tricky e.g. with QStackedWidget and QTabWidget, I tried specifying a custom class but for some reason the style just wasn't applying

            There has to be a reason why setstylesheet works for all widgets at QDialog level when set in Designer but fails when doing it in code?

            J Offline
            J Offline
            JonB
            wrote on 8 Jun 2020, 08:13 last edited by
            #4

            @Taytoo said in How to propagate/limit stylesheets through code:

            It's not doing anything special, just setting stylesheet for QDialog and nowhere else.

            Then in principle if it works from Designer-generated code you should be able to make it work in manual code?

            Defining custom classes gets too tricky e.g. with QStackedWidget and QTabWidget, I tried specifying a custom class but for some reason the style just wasn't applying

            I can only say that I do not find this. I do use custom classes when necessary, and it works for me. If I want to apply stylesheet rules across a variety of elements, I use the "dynamic class" principle described at https://doc.qt.io/Qt-5/stylesheet-syntax.html#selector-types Property Selector & https://doc.qt.io/Qt-5/stylesheet-examples.html#customizing-using-dynamic-properties. I use this against a dynamic class property, so I can have things like:

            /* stylesheet */
            .backgroundColorTransparent { background-color: transparent; }
            
            /* code */
            widget.setProperty("class", "backgroundColorTransparent");
            

            I can't recall now, but I think you can associate multiple classes with a widget as a space-separated list on the class property.

            One other thought: if your only problem is with *background color", have a read of https://forum.qt.io/topic/115640/how-to-see-the-area-detected-by-qmouseevent/9.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Taytoo
              wrote 14 days ago last edited by
              #5

              5 years later, I've stumbled on the solution for Option 2. What I was doing wrong was not calling unpolish() and polish() methods. The correct order is call unpolish(), setstylesheet(), and polish(). Now the styles are applied correctly on all child widgets.

              1 Reply Last reply
              2
              • T Taytoo has marked this topic as solved 14 days ago

              • Login

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