Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. CellsColor in the QTableView
QtWS25 Last Chance

CellsColor in the QTableView

Scheduled Pinned Locked Moved Qt Creator and other tools
10 Posts 4 Posters 6.6k 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.
  • M Offline
    M Offline
    MasterMatrix
    wrote on last edited by
    #1

    Hello!

    It’s warking on Qt 4.5.3 and QtCreator 1.2.1

    I try to change color of a background in a cell of model QTableView.
    The code is compiled, but color of a background doesn't change. Help, please. A code:

    @ modelUI = new QSqlTableModel(this);
    modelUI->setTable("test");
    modelUI->setData(modelUI->index(3,3),
    qVariantFromValue(QColor(150, 50, 50)),
    Qt::BackgroundColorRole);
    modelUI->setEditStrategy(QSqlTableModel::OnManualSubmit);
    if (!modelUI->select()) {qDebug() << "Error select";}
    ui->tableView->setModel(modelUI);@

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

      You must set the background after the select!

      Reading from the database with select destroys the previously read data (== indexes) and inserts new ones.

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MasterMatrix
        wrote on last edited by
        #3

        Dear Volker!

        You mean this?
        @
        if (!modelUI->select()) {qDebug() << "Error select";}
        modelUI->setData(modelUI->index(3,3), QBrush(Qt::red), Qt::BackgroundColorRole);
        ui->tableView->setModel(modelUI);@

        I'm sorry, but it doesnt work

        If you mean something else will you please show me some simple example?

                                                                                            Thanks!
        
        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          I doubt it is going to work at all, but I may be mistaken.
          To me, it sounds strange to set data on a QSqlTableModel that will not and can not actually be stored in the underlying data store. At least; I assume your database does not actually store the color, does it?

          Still, perhaps QSqlTableModel is intelligent enough to cache this data for you.

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

            MasterMatrix, it's possible that the SQL models use lazy loading and the model is still not filled when you set the background.

            Better way to achieve your goal would be to subclass "QStyledItemDelegate":http://doc.qt.nokia.com/stable/qstyleditemdelegate.html, reimplement the paint method, set the background there and call the base class' implementation for the rest.

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

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

              Or create a Proxy model, only overwrite the data method and there change the 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
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                [quote author="Gerolf" date="1295355643"]Or create a Proxy model, only overwrite the data method and there change the color.[/quote]

                That's even better!

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

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

                  I use the last way too. I like the separation of concerns that you get here:

                  • base model contains the actual data
                  • proxy model handles decorating the data, such as changing colors
                  • view handles actual displaying of the data

                  I think I have a proxy model lying around somewhere that can be used for this. If I find it, I'll let you know. I think it was limited to table-like base models (no trees), but that should not matter as the base model was not a tree anyway.

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

                    You could just use the QSortFilterProxyModel and overwrite the data method. No need to create a complete custom proxy model which handles persistent model indexes and all that stuff...

                    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
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Of course. QSFPM is a proxy model as well, right?
                      If you need a complicated proxy model, depends on what you want to achieve exactly. If you want to color a complete row or column, then that is easy to do with QSFPM. If you want to be able to manually color cells, you need a bit more than that.

                      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