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. Model BackgroundRole overridden by style sheet
QtWS25 Last Chance

Model BackgroundRole overridden by style sheet

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 7 Posters 2.5k 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.
  • P Offline
    P Offline
    Patrick Wright
    wrote on last edited by
    #1

    Hello,

    I have a custom model which sets the BackgroundRole display role to a red color when a certain item has an error associated with it. This works well except when I apply a style sheet to the List View displaying the model. Once the style sheet is applied, it appears to override any of the display role settings (i.e. no more red color).

    Is there a way to make sure (a) that the display role returned by the model is applied or (b) that I can apply a style sheet selector based upon the display role to set the color there?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What aspects are you currently styling through stylesheets ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What aspects are you currently styling through stylesheets ?

        P Offline
        P Offline
        Patrick Wright
        wrote on last edited by
        #3

        @SGaist This is the stylesheet I am applying to the ListView widgets.

        QListView::item {
        	border-bottom: 1px solid black;
        	color: black;
        	height: 30px;
        }
        
        QListView::item:selected {
        	background: #e6e6e6;
        }
        
        QListView::item:hover {
        	background: #f2f2f2;
        }
        
        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          That's imo exactly what's described here: https://bugreports.qt.io/browse/QTBUG-70100
          I'm unsure if this should really be the case because the style sheet wins by definition and this would be an exception. Don't set the background in the style sheet and return the proper color directly in the model is imho the way to go.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            That's imo exactly what's described here: https://bugreports.qt.io/browse/QTBUG-70100
            I'm unsure if this should really be the case because the style sheet wins by definition and this would be an exception. Don't set the background in the style sheet and return the proper color directly in the model is imho the way to go.

            P Offline
            P Offline
            Patrick Wright
            wrote on last edited by
            #5

            @Christian-Ehrlicher It does like that request refers to the same issue. The only odd this is, if I remove all of the
            "background" items in the style sheet (just preserving the size and border changes), I still loose the color set by the model. This leads me to believe that, if any style sheet is in affect, Qt simply ignores all the formatting coming from the model.

            If this is the case, is there any way I could have the model apply a property or something similar to the data I want colored, then use a style sheet selector to change the background colors that way?

            kshegunovK 1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              If there is no background property set I would expect that the background from the model is used - if this is not the case it's imo a bug.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Patrick Wright
                wrote on last edited by
                #7

                Ok, in the case that this really is a bug, I will take step back (since this bug will most likely not get fixed any time soon) to explain what I would like to accomplish. Perhaps there is a different solution.

                I have a model which represents objects that can be in different states. I want the views (list, table, etc...) attached to these models to change their row background colors based upon the state of those objects. For example, when an object is in an error condition, the corresponding row in the model should become red. Returning the correct color in the data() method of the model based upon the role is a good solution, however I also want to easily be able to change these colors WITHOUT having to modify the code and recompile, hence why I was trying to use style sheets.

                I suppose one alternative would be to use settings which the model uses when returning the color. I like the style sheet idea because it easily allows me to make "themes" for the application without complicated code for that purpose.

                Any suggestions to accomplish this goal?

                JonBJ 1 Reply Last reply
                0
                • P Patrick Wright

                  @Christian-Ehrlicher It does like that request refers to the same issue. The only odd this is, if I remove all of the
                  "background" items in the style sheet (just preserving the size and border changes), I still loose the color set by the model. This leads me to believe that, if any style sheet is in affect, Qt simply ignores all the formatting coming from the model.

                  If this is the case, is there any way I could have the model apply a property or something similar to the data I want colored, then use a style sheet selector to change the background colors that way?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @Patrick-Wright said in Model BackgroundRole overridden by style sheet:

                  This leads me to believe that, if any style sheet is in affect, Qt simply ignores all the formatting coming from the model.

                  I believe this is correct. I think the model's hint goes through the palette, while the stylesheet disables the palette.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    I'm not sure if this can help you since I don't know if this will work for items: http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-using-dynamic-properties
                    Wrt to the possible bug - please create a bug report so is not forgotten.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    2
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      Could poster not use a QStyledItemDelegate ?
                      with void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
                      implementation. ?

                      1 Reply Last reply
                      0
                      • P Patrick Wright

                        Ok, in the case that this really is a bug, I will take step back (since this bug will most likely not get fixed any time soon) to explain what I would like to accomplish. Perhaps there is a different solution.

                        I have a model which represents objects that can be in different states. I want the views (list, table, etc...) attached to these models to change their row background colors based upon the state of those objects. For example, when an object is in an error condition, the corresponding row in the model should become red. Returning the correct color in the data() method of the model based upon the role is a good solution, however I also want to easily be able to change these colors WITHOUT having to modify the code and recompile, hence why I was trying to use style sheets.

                        I suppose one alternative would be to use settings which the model uses when returning the color. I like the style sheet idea because it easily allows me to make "themes" for the application without complicated code for that purpose.

                        Any suggestions to accomplish this goal?

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Patrick-Wright
                        Now that you have explained what you're trying to achieve, I'd like to second @Christian-Ehrlicher's post above and suggest you look at that Qt "dynamic properties" link. It is a fact that stylesheets override code palette colors which I think is what BackgroundRole etc. use. I use dynamic properties at present to "attach" certain style rules to widgets. Like him I'm not sure if/how you can get this to work from the model cells to the view, but it's worth a thought....

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          orlahirish
                          Banned
                          wrote on last edited by
                          #12
                          This post is deleted!
                          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