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 simply change the background colour of a cell inside a TableView
Forum Updated to NodeBB v4.3 + New Features

How to simply change the background colour of a cell inside a TableView

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 3 Posters 10.6k 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.
  • G4banditG Offline
    G4banditG Offline
    G4bandit
    wrote on last edited by G4bandit
    #7

    @VRonin said in How to simply change the background colour of a cell inside a TableView:

    Are you setting the model on the view? view->setModel(proxyModel);

    I have changed the code to this:

       QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
       proxyModel->setSourceModel(TblModel);
       ui->TV_DBOut->setModel(proxyModel);
    
          TblModel->setData(proxyModel->index(0,0),QBrush(Qt::red),Qt::BackgroundRole);
    

    But I guess strongly that it is not what you have meant or at all that it is more complex.

    Like you said in your post before:

    1. Add this proxy between your model and your view
      I can not follow you, - I am a bloody newbie.
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #8

      The main question is: what type is TblModel?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      G4banditG 1 Reply Last reply
      0
      • VRoninV VRonin

        The main question is: what type is TblModel?

        G4banditG Offline
        G4banditG Offline
        G4bandit
        wrote on last edited by
        #9

        @VRonin said in How to simply change the background colour of a cell inside a TableView:

        The main question is: what type is TblModel?

        Sry, yes it is of QSqlTableModel.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #10

          Then TblModel->setData will return false for every role except Qt::EditRole so basically TblModel->setData(proxyModel->index(0,0),QBrush(Qt::red),Qt::BackgroundRole); will do absolutely nothing.

          You need something that handles the other roles that QSqlTableModel ignores. Enter https://pastebin.com/gmNCuUFk

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0
          • G4banditG Offline
            G4banditG Offline
            G4bandit
            wrote on last edited by G4bandit
            #11

            @VRonin

            I agree with you TblModel->setData returns false.

            But my main issue still that I am not able to combine all your information/hints/code parts to one working unit.

            1:
            I have created a header file with the content of "https://pastebin.com/gmNCuUFk"
            Do not know what to do with it.

            2:
            and I also tried to run your code posted on
            VRonin 15. Dez. 2017, 10:31

            3:
            And this point
            VRonin: 1.Add this proxy between your model and your view
            Do not know how to

            VRoninV 1 Reply Last reply
            0
            • G4banditG G4bandit

              @VRonin

              I agree with you TblModel->setData returns false.

              But my main issue still that I am not able to combine all your information/hints/code parts to one working unit.

              1:
              I have created a header file with the content of "https://pastebin.com/gmNCuUFk"
              Do not know what to do with it.

              2:
              and I also tried to run your code posted on
              VRonin 15. Dez. 2017, 10:31

              3:
              And this point
              VRonin: 1.Add this proxy between your model and your view
              Do not know how to

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #12

              @G4bandit said in How to simply change the background colour of a cell inside a TableView:

              Do not know how to

              It's the same as

              QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
                 proxyModel->setSourceModel(TblModel);
                 ui->TV_DBOut->setModel(proxyModel);
              

              but instead of QSortFilterProxyModel use ExtraRolesProxyModel

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • G4banditG Offline
                G4banditG Offline
                G4bandit
                wrote on last edited by
                #13

                I got it!!!

                SUCESS :D

                0_1518084519758_85794cbf-3637-47b4-972a-c6e5d8c48e10-grafik.png

                Thanks a lot

                P 1 Reply Last reply
                0
                • G4banditG Offline
                  G4banditG Offline
                  G4bandit
                  wrote on last edited by G4bandit
                  #14

                  If someone ask how to change the color of headers,- here is a helpful link:

                  https://stackoverflow.com/questions/32147213/how-to-change-the-background-color-from-the-headerhorizontal-vertical-qtable#32147945

                  tview->model()->setHeaderData(0,Qt::Horizontal,QBrush(QColor("red")),Qt::BackgroundRole);
                  tview->show();
                  
                  QApplication::setStyle(QStyleFactory::create("Fusion"));
                  

                  The last line was on my PC (Win) necessary to get the effect.

                  1 Reply Last reply
                  0
                  • G4banditG G4bandit

                    I got it!!!

                    SUCESS :D

                    0_1518084519758_85794cbf-3637-47b4-972a-c6e5d8c48e10-grafik.png

                    Thanks a lot

                    P Offline
                    P Offline
                    paul_espinosa
                    wrote on last edited by
                    #15

                    @G4banditi need to do somo simililar can help me please , i cant understand

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #16

                      I now implemented a generic proxy for this, I called it Role Mask Proxy Model. I have an example that changes the background, it uses QStringListModel but it's exactly the same with other base models

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      P 1 Reply Last reply
                      1
                      • VRoninV VRonin

                        I now implemented a generic proxy for this, I called it Role Mask Proxy Model. I have an example that changes the background, it uses QStringListModel but it's exactly the same with other base models

                        P Offline
                        P Offline
                        paul_espinosa
                        wrote on last edited by
                        #17

                        @VRonin so mucth thanks , wonderfull library, i can do

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          paul_espinosa
                          wrote on last edited by
                          #18

                          if i use a QSqlRelationalTableModel, Role Mask Proxy Model works????

                          VRoninV 1 Reply Last reply
                          0
                          • P paul_espinosa

                            if i use a QSqlRelationalTableModel, Role Mask Proxy Model works????

                            VRoninV Offline
                            VRoninV Offline
                            VRonin
                            wrote on last edited by
                            #19

                            Yes, works on any model

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            P 1 Reply Last reply
                            0
                            • VRoninV VRonin

                              Yes, works on any model

                              P Offline
                              P Offline
                              paul_espinosa
                              wrote on last edited by paul_espinosa
                              #20

                              @VRonin thanks for your help, now i have a problem, i can not save the data of my QSqlTableModel , i used the example exem_rolemaskeditablesql.cpp i can edit the data but i can not save,

                              1 Reply Last reply
                              0
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #21

                                If you call addMaskedRole passing either Qt::DisplayRole or Qt::EditRole any change will stay in the proxy and not be passed to the source model.

                                You should only mask the roles that you need, in this case Qt::BackgroundRole.

                                If it's still not clear, please post us the code using RoleMaskProxyModel and we'll be more specific on the changes to make

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                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