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 can I change QTableView's color of the area that doesn't have cells on it?

How can I change QTableView's color of the area that doesn't have cells on it?

Scheduled Pinned Locked Moved General and Desktop
36 Posts 8 Posters 30.8k 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.
  • E Offline
    E Offline
    Edico
    wrote on last edited by
    #1

    How can be changed QTableView's color of the area that doesn't have cells on it?

    Thanks in advance!

    1 Reply Last reply
    0
    • O Offline
      O Offline
      octal
      wrote on last edited by
      #2

      Could you be more precise ? What do you want to change exactly ? The header's color ?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        Set the background color. You can do that either with setting the palette, or by using a style sheet.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          This snippet does the trick:

          @
          QTableWidget tw;
          QPalette p = tw.palette();
          p.setColor(QPalette::Base, QColor("yellow"));
          tw.setPalette(p);
          @

          Or set the respective color in the palette property in Qt Designer, if you're using .ui files.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Edico
            wrote on last edited by
            #5

            Volker, I've tried like that but it also does changing the cells color.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              [quote author="Edico" date="1314534038"]Volker, I've tried like that but it also does changing the cells color.[/quote]

              That's correct. The background color is behind the cells and behind the widget :-(
              What you could do is use a style sheet and set the background color of the table there and another background color for the cells.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Edico
                wrote on last edited by
                #7

                I didn't made it even with style sheets. I've reached doing something like that:

                @
                QTableView {
                alternate-background-color: blue;
                background: red
                }

                QTableView::item:selected {
                background-color: green
                }

                QTableView::item {
                background-color: white
                }
                @

                All items are white with no alternate row colors.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Are you using some kind of special, non-standard style?

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    Edico
                    wrote on last edited by
                    #9

                    Andre, no, I don't think so. With the above code I see al items white and the area that doesn't have cells on is red, but I see no alternating row colors (one white - one blue).

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sigrid
                      wrote on last edited by
                      #10

                      I can reproduce this problem here and it looks like a bug. When setting the background-color for the item in addition to the background -color and the alternate-background-color for the table, then the alternate-background-color will be ignored. I suggest you report this issue in "Jira":https://bugreports.qt.nokia.com//secure/Dashboard.jspa.

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        if you set the color for cells in general and also alternate background color, then the cell wins, that makes sense. and where else will you see the alternate background color?

                        Nokia Certified Qt Specialist.
                        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          Edico
                          wrote on last edited by
                          #12

                          Is there a way to have a color for the outside area of the cells, another for cells and other color for alternate background color?

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            giesbert
                            wrote on last edited by
                            #13

                            you will not have alternate color if you set cells color via style sheet. alternate color is a cell color.

                            Nokia Certified Qt Specialist.
                            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                            1 Reply Last reply
                            0
                            • E Offline
                              E Offline
                              Edico
                              wrote on last edited by
                              #14

                              So, the only way is to subclass QSqlRelationalTableModel (the model I use to access data)? That looks like a big pain.

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                giesbert
                                wrote on last edited by
                                #15

                                if you want alternate row colors and a different cell background then empty background? I think yes. Or use a proxy model instead of a subclass, that's much less work.

                                Nokia Certified Qt Specialist.
                                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                1 Reply Last reply
                                0
                                • E Offline
                                  E Offline
                                  Edico
                                  wrote on last edited by
                                  #16

                                  I can't figure how to use a proxy model for this job.

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    giesbert
                                    wrote on last edited by
                                    #17

                                    if you use a proxy model, the proxy models data method is called for each data query instead of the methods of the original model. This then calls the original (source) model. So you can do everything you want there, like changing backroundColor via the Qt::BackgroundRole. IMHO ProxyModels are easier then derived SQL models ;-)

                                    Nokia Certified Qt Specialist.
                                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                                    1 Reply Last reply
                                    0
                                    • E Offline
                                      E Offline
                                      Edico
                                      wrote on last edited by
                                      #18

                                      Gerolf, this mean that I must subclass QSortFilterProxyModel and reimplement data() method?

                                      1 Reply Last reply
                                      0
                                      • O Offline
                                        O Offline
                                        octal
                                        wrote on last edited by
                                        #19

                                        Yes, you can create your custom ProxyModel by sublcassing QSortFilterProxyModel and reimplementing the data() method. From there, you can do everything you want, like Gerolf said.

                                        Finally, you just set the sourceModel of your ProxyModel, and it should work :)

                                        1 Reply Last reply
                                        0
                                        • A Offline
                                          A Offline
                                          andre
                                          wrote on last edited by
                                          #20

                                          Yes. If you were using 4.8, I would suggest you use -QIdentityModel- [[doc:QIdentityProxyModel]] as your base class, but if you are still on 4.7, use [[doc:QSortFilterProxyModel]] instead. You only need to reimplement the data() method. In that method, you only need to implement the case where the Qt::BackgroundRole is requested. Based on the row number (odd or even), you can return a different color. For all other cases, you get the parent QModelIndex by calling mapToSource() on the model index, and then return the result of calling the data() method on the source model with the same role and the model index you just mapped.

                                          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