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. Set background color to a cell of a tableView
Qt 6.11 is out! See what's new in the release blog

Set background color to a cell of a tableView

Scheduled Pinned Locked Moved General and Desktop
54 Posts 5 Posters 54.8k 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #44

    I think what you are after, is that you need to convert the model index that you have for the proxy model to the model index that you need for the source model. QSortFilterProxyModel has mapping methods for both ways. You need mapToSource. This allows you to get the model index for the source data model that corresponds to the item that data() is currently called for in your proxy model.

    @
    // in your data reimplementation
    if (role == Qt::BackgroundRole ) {
    sourceIndex = mapToSource(proxyIndex);
    if (sourceIndex.data(Qt::EditRole).toInt() == 1) {
    return QVariant(Qt::blue);
    }
    }
    @

    Or something like that.

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ihtzudismqa
      wrote on last edited by
      #45

      I guess proxyIndex is equal to QModelIndex &index?

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

        [quote author="ihtzudismqa" date="1302861095"]I guess proxyIndex is equal to QModelIndex &index?[/quote]

        Yes, sorry.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          poporacer
          wrote on last edited by
          #47

          I have been reading to see if I can figure out how to get the table to be highlighted correctly after it is sorted but I have not been able to figure it out. I drew a blank with having four models as suggested but I did find something else that might work....but it isn't. I think I should be able to capture the signal from when the user clicks on the header and emit a signal to reload the table. Here is what I tried but when I debug, the signal does not get called.
          @QHeaderView *horizHeader;
          QSqlRelationalTableModel printModel;
          MyProxyModel
          proxy;

          printModel= new QSqlRelationalTableModel (this);
          printModel-> setTable (mTableName);
          printModel-> setRelation (2, QSqlRelation("name", "id", "LName"));

          printModel->select();
          proxy = new MyProxyModel(this);
          proxy->setSourceModel(printModel);
          ui->printView->setModel(proxy);
          horizHeader= ui->printView->horizontalHeader(); connect(horizHeader, SIGNAL(sortIndicatorChanged (int) ),
          proxy,SIGNAL(dataChanged ( ) ));
          @
          I am pretty sure it is because of the wrong syntax for the dataChanged signal, but I am not sure how to make it work. Will this work? Or am I going down the wrong path? Is the model method better? If so, how do I make it work? I have no clue on how to start.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            ihtzudismqa
            wrote on last edited by
            #48

            Hello,
            I implemented the data function, but my tableview stays empty. The Grid is shown, but without any data inside. Here is the code:

            data implementation
            @QVariant proxymodel::data(const QModelIndex &index, int role) const
            {
            QModelIndex sourceIndex;
            if (!index.isValid())
            return QVariant();

            if (role == Qt::BackgroundRole ) {
            sourceIndex = mapToSource(index);
            if (sourceIndex.data(Qt::EditRole).toInt() == 1) {
            return QVariant(Qt::blue);
            }
            }
            return QVariant();
            }@

            the filltable function
            @
            void Window::fillTable()
            {
            model = new QSqlRelationalTableModel( tableView );
            model->setTable("Protokoll");
            proxy = new proxymodel(tableView);

                model->setRelation(12,QSqlRelation("Status", "ID", "Status"));
                model->setEditStrategy(QSqlTableModel::OnFieldChange);
              
                tableView->setItemDelegate(new QSqlRelationalDelegate(tableView));
                proxy->setSourceModel(model);
            

            tableView->setModel(proxy);
            model->select();
            }
            @
            What is wrong?

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #49

              It looks to me as if you have not finished implementing the data() function in your proxy. You check for the role to see if it is the background role and correctly return blue if the data matches your criteria.

              However, you have not handled the case where the data does not match your criteria. You have also not handled any of the cases where the role is not the background role.

              I think it needs to look more like this:

              @
              QVariant proxymodel::data(const QModelIndex &index, int role) const
              {
              QModelIndex sourceIndex;
              if (!index.isValid())
              return QVariant();

              // We only wish to override the background role
              if (role == Qt::BackgroundRole ) {
                  sourceIndex = mapToSource(index);
                  if (sourceIndex.data(Qt::EditRole).toInt() == 1) {
                      return QVariant(Qt::blue);
                  } else {
                      return QSortFilterProxyModel::data( index, role );
                  }
              }
              
              // Let the base class handle all other cases
              return QSortFilterProxyModel::data( index, role );
              

              }
              @

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

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

                Indeed, which can be further simplified to:

                @
                QVariant proxymodel::data(const QModelIndex &index, int role) const
                {
                QModelIndex sourceIndex;
                if (!index.isValid())
                return QVariant();

                // We only wish to override the background role
                if (role == Qt::BackgroundRole ) {
                    sourceIndex = mapToSource(index);
                    if (sourceIndex.data(Qt::EditRole).toInt() == 1) {
                        return QVariant(Qt::blue);
                    } 
                }
                 
                // Let the base class handle all other cases
                return QSortFilterProxyModel::data( index, role );
                

                }
                @

                :-)

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  ZapB
                  wrote on last edited by
                  #51

                  Yeah - I've not had my second cup of tea yet this morning ;-)

                  Nokia Certified Qt Specialist
                  Interested in hearing about Qt related work

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ihtzudismqa
                    wrote on last edited by
                    #52

                    It works now! Thank you!!

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      ihtzudismqa
                      wrote on last edited by
                      #53

                      Sorry for double post, but I have a few questions remaining. One point is that the ItemDelegate does not work anymore.
                      @tableView->setItemDelegate(new QSqlRelationalDelegate(this));@

                      And last but not least I cant access every cell in my table - It's just not editable?!

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        poporacer
                        wrote on last edited by
                        #54

                        So any ideas on how to get the proper row highlighted AFTER the table is sorted? I have not had any luck with my attempts.

                        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