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 change row or cell color in TableView with SqlQueryModel

How to change row or cell color in TableView with SqlQueryModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 2.2k Views 2 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.
  • MucipM Mucip

    Hi,
    I have SqlQueryModel and read rows from query. I load this on TableView and want to change some rows or cells in TableView.?
    There are many sample about Item Model but I couldn't see one about SqlQueryModel.
    My code is below:

    QSqlQueryModel *modelTableIhtiyac;
    
    this->modelTableIhtiyac = new QSqlQueryModel(this); // ,db
    
    
    modelTableIhtiyac->setQuery("select stok_kod, stok_ad, stok_tip, stok_miktar, stok_birim, birim_fiyat, toplam_fiyat, birim_agirlik, toplam_agirlik FROM uretim_listesi_2 WHERE gorunsun=1 ORDER BY stok_tip, stok_kod;",baglanti::mdb2());
    
    //TvDetay is TableView...
        ui->tVDetay->setModel(modelTableIhtiyac);
    

    How can I change some rows or cells in above code?...

    Regards,
    Mucip:)

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

    @Mucip
    In addition to what @SGaist has suggested, do you actually want to change values in cells, or rather color shown as per your topic title?

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

      And to add to @JonB, if it's cosmetic change that won't alter the database content, QIdentityProxyModel is likely what your looking for.

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

      JonBJ 2 Replies Last reply
      2
      • SGaistS SGaist

        And to add to @JonB, if it's cosmetic change that won't alter the database content, QIdentityProxyModel is likely what your looking for.

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

        @SGaist That sounds more like it! :)

        1 Reply Last reply
        0
        • SGaistS SGaist

          And to add to @JonB, if it's cosmetic change that won't alter the database content, QIdentityProxyModel is likely what your looking for.

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

          @SGaist
          Without changing to a table or other model, can't @Mucip change cell color just by overriding QSqlQueryModel::data() and dealing with Qt::BackgroundRole case with desired code, assuming that is indeed what he wants?

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

            That's another possibility yes. One thing to take into account is that if these customisation were to be "view specific", changing the base model would "propagate" them to all views showing the model data. The QIdentityProxyModel allows for more fine grained control in that regard.

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

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              QSqlQueryModel is read-only. Why not use QSqlTableModel ?

              MucipM Offline
              MucipM Offline
              Mucip
              wrote on last edited by
              #8

              Dear @SGaist ,
              I just want to show data not to modify. This is enaugh for me...

              Regards,
              Mucip:)

              1 Reply Last reply
              0
              • JonBJ JonB

                @Mucip
                In addition to what @SGaist has suggested, do you actually want to change values in cells, or rather color shown as per your topic title?

                MucipM Offline
                MucipM Offline
                Mucip
                wrote on last edited by
                #9

                Hi @JonB,
                Yes. Just want to show data and change color of the desired rows.

                Regards,
                Mucip:)

                JonBJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @SGaist
                  Without changing to a table or other model, can't @Mucip change cell color just by overriding QSqlQueryModel::data() and dealing with Qt::BackgroundRole case with desired code, assuming that is indeed what he wants?

                  MucipM Offline
                  MucipM Offline
                  Mucip
                  wrote on last edited by
                  #10

                  Dear @JonB,
                  Sonds good... Any sample about this?...

                  Regards,
                  Mucip:)

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

                    What kind of sample are you looking for ?

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

                    MucipM 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      What kind of sample are you looking for ?

                      MucipM Offline
                      MucipM Offline
                      Mucip
                      wrote on last edited by
                      #12

                      Dear @SGaist ,
                      You suggested QIdentityProxyModel. Is there any explenation or sample how to use this?
                      Regards,
                      Mucip:)

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

                        Right in the documentation for the class that I already linked to earlier.

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

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

                          You can use the RoleMaskProxyModel of this library.
                          I'm working on merging this into Qt 5.13: https://codereview.qt-project.org/245572 (at the time of writing it's marked as draft so private)

                          "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
                          2
                          • MucipM Mucip

                            Hi @JonB,
                            Yes. Just want to show data and change color of the desired rows.

                            Regards,
                            Mucip:)

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

                            @Mucip said in How to change row or cell color in TableView with SqlQueryModel:

                            Hi @JonB,
                            Yes. Just want to show data and change color of the desired rows.

                            Regards,
                            Mucip:)

                            Sorry, I have been away for a few days. As I wrote earlier, if all you want to do is set background color of certain rows you could skip proxy models and just override QSqlQueryModel::data() to deal with Qt::BackgroundRole. It depends whether you can figure the color you want for whatever rows very simply in code, or whether you need something more sophisticated which a proxy model could provide.

                            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