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. QTableView with custom model: color of disabled items
Forum Updated to NodeBB v4.3 + New Features

QTableView with custom model: color of disabled items

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 6 Posters 3.5k 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.
  • artwawA artwaw

    @mpergand Rule of thumb: model provides the data, nothing else. Delegate is responsible for rendering the data. I agree it is often tempting to make a shortcut, like one you proposed, but this will bite back in the least expected moment. Better off to stick with the paradigm even if it doesn't seem to make sense in a given moment and involves a bit more work.

    S Offline
    S Offline
    StarterKit
    wrote on last edited by
    #15

    @artwaw said in QTableView with custom model: color of disabled items:

    @mpergand Rule of thumb: model provides the data, nothing else. Delegate is responsible for rendering the data. I agree it is often tempting to make a shortcut, like one you proposed, but this will bite back in the least expected moment. Better off to stick with the paradigm even if it doesn't seem to make sense in a given moment and involves a bit more work.

    I can't fully agree with it. First of all - I'm legally able to return color from data() method() as Qt.ForegroundRole is definded.
    Second - I may say that color of my item is also a piece of data (and it really is - i.e. I may provide it as a separate field for delegate but ForegroundRole gives a smoother way to do it)

    1 Reply Last reply
    0
    • JonBJ JonB

      @StarterKit
      I tested this at least on a QTableWidget I have, and agree I saw items with a color supplied from data() not showing as "dimmed/greyed". It seems that only applies if no color is specified, whether that is right or wrong.

      Given which, the only correct way to go is indeed to provide a QStyledItemDelegate as a custom delegate, because only a delegate has access to the state of the parent view. A minimal delegate to do the desired here is actually only a small number of lines of code.

      S Offline
      S Offline
      StarterKit
      wrote on last edited by
      #16

      @JonB said in QTableView with custom model: color of disabled items:

      I tested this at least on a QTableWidget I have, and agree I saw items with a color supplied from data() not showing as "dimmed/greyed". It seems that only applies if no color is specified, whether that is right or wrong.
      Given which, the only correct way to go is indeed to provide a QStyledItemDelegate as a custom delegate, because only a delegate has access to the state of the parent view. A minimal delegate to do the desired here is actually only a small number of lines of code.

      Thanks for your efforts. I assume you got similar result to what I posted above.
      In reality I have more complex coloring rules applied and some months ago I handled it with QStyledItemDelegate descendants. I had a lot of different delegates here and there, it was quite a mess.
      Then I got a better grasp on Qt MV paradigm and re-wrote my models. I discovered ForegroundRole/BackgroundRole options and got rid of almost all of my delegates (I still have several but for specific things).
      This is why I have no desire to go back to that messy code that did only minor things...
      I fully understand that it isn't a bug. But from my point of view this behavior of QTableView isn't proper. Is it possible to report it as enhancement proposal somewhere?
      (I probably may do a code change by myself... the only problem I don't have C/C++ compiler at hand for last couple of years...)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        StarterKit
        wrote on last edited by StarterKit
        #17

        I just checked Qt.BackroundRole to be sure - I can confirm it has the same behavior. I.e. cell background is not grey:
        background

        I think QTableView and probably other widgets should be improved.
        One simple approach I may propose - to ignore colors provided by ForegroundRole/BackgroundRole if widget is disabled.
        As more complex thing - color might be "dimmed".
        Without such improvement, text may even occasionally become invisible after disabling of widget (if custom foreground will match with disabled background or vice versa)

        Christian EhrlicherC 1 Reply Last reply
        0
        • S StarterKit

          I just checked Qt.BackroundRole to be sure - I can confirm it has the same behavior. I.e. cell background is not grey:
          background

          I think QTableView and probably other widgets should be improved.
          One simple approach I may propose - to ignore colors provided by ForegroundRole/BackgroundRole if widget is disabled.
          As more complex thing - color might be "dimmed".
          Without such improvement, text may even occasionally become invisible after disabling of widget (if custom foreground will match with disabled background or vice versa)

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #18

          @StarterKit said in QTableView with custom model: color of disabled items:

          I think QTableView and probably other widgets should be improved.

          Feel free to provide a patch for it and find people who think it's a bug.

          Since a model doesn't know anything about enabled/disabled you would have to double all roles with a one for the disabled state. And when you're at it don't forget that there is a focused/non-focues state (see QPalette) too.

          One simple approach I may propose - to ignore colors provided by ForegroundRole/BackgroundRole if widget is disabled.

          My data is such important that it must have a red background even the widget is disabled.

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

          S 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @StarterKit said in QTableView with custom model: color of disabled items:

            I think QTableView and probably other widgets should be improved.

            Feel free to provide a patch for it and find people who think it's a bug.

            Since a model doesn't know anything about enabled/disabled you would have to double all roles with a one for the disabled state. And when you're at it don't forget that there is a focused/non-focues state (see QPalette) too.

            One simple approach I may propose - to ignore colors provided by ForegroundRole/BackgroundRole if widget is disabled.

            My data is such important that it must have a red background even the widget is disabled.

            S Offline
            S Offline
            StarterKit
            wrote on last edited by
            #19

            @Christian-Ehrlicher ok, your point is clear and I agree it might be the case sometimes.
            But from my side - I'm quite satisfied with options provided by Foreground and Background roles of model's data() method and I'm not happy to go back creating a lot of delegates here and there just to make a somewhat good-looking UI. And a big part of Qt is about UI so I disagree that this dull job should be handled this way. This is not to agrue, just to supply another point of view.

            Saying this I'm thinking about anything in between - if I subclass QTableView can I adjust common colors somewhere before painting? will paintEvent help me with it? I see in manual that it is possible to call setBackgroundRole() and setPalette() - how do you think, is it possible to substitute colors in paintEvent handler? Or I need to go deeper and modify some of parent's methods? Any ideas?

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

              @StarterKit said in QTableView with custom model: color of disabled items:

              Any ideas?

              Use a delegate or a QIdentityProxyModel.

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

              S 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @StarterKit said in QTableView with custom model: color of disabled items:

                Any ideas?

                Use a delegate or a QIdentityProxyModel.

                S Offline
                S Offline
                StarterKit
                wrote on last edited by
                #21

                @Christian-Ehrlicher pardon, but I don't understand how QIdentityProxyModel can help for my case. From what I I read I see it returns the same values from data() method as any other model.

                With regards to delegates - I already commented.

                Christian EhrlicherC JonBJ 2 Replies Last reply
                0
                • S StarterKit

                  @Christian-Ehrlicher pardon, but I don't understand how QIdentityProxyModel can help for my case. From what I I read I see it returns the same values from data() method as any other model.

                  With regards to delegates - I already commented.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #22

                  @StarterKit said in QTableView with custom model: color of disabled items:

                  With regards to delegates - I already commented.

                  But you want to fiddle around in QTableView? Wow...

                  QIdentityProxyyModel

                  This function class is there to allow to modify values returned from data() without affecting the base model. So tell this model the current widget state and return the right colors from there.

                  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
                  • S StarterKit

                    @Christian-Ehrlicher pardon, but I don't understand how QIdentityProxyModel can help for my case. From what I I read I see it returns the same values from data() method as any other model.

                    With regards to delegates - I already commented.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #23

                    @StarterKit
                    I replied earlier stating the only two (reasonable) choices are a delegate or a proxy model.

                    Since you do not wish to write a delegate, I suggested:

                    If the OP really does not wish to create a delegate for the QTableView, an alternative is to create a QAbstractProxyModel local to the QTableView. Interpose that between the view and the original source model, and do the required data() alteration there (where it can access the view to see if it is disabled). At least then he is sure the model returning the "manipulated" value for the color is totally local to the individual view.

                    So "do the required data() alteration there". A QIdentityProxyModel starts out "doing nothing" other than mapping straight through to the source model unchanged. You should derive from that to create your own sub-class. Then override and alter its data() method only (nothing else) to respond to the ForegroundRole by testing the view's "disablement" status and returning your desired color in that case. For all other cases in data() return the base QIdentityProxyModel's value.

                    1 Reply Last reply
                    3
                    • S Offline
                      S Offline
                      StarterKit
                      wrote on last edited by
                      #24

                      Ok, guys. Thank your for this discussion. I'll go and change my models...

                      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