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. [SOLVED] QTableView, QAbstractTableModel, and QStyledItemDelegate: Row color update not working.
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTableView, QAbstractTableModel, and QStyledItemDelegate: Row color update not working.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.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
    ASxa86
    wrote on last edited by
    #1

    I am attempting to toggle a checkbox in my first column that will toggle the entire row's color accordingly.

    Let's start off with the basics.

    I'm using a QTableView as my view, with the default QStyledItemDelegate that the QTableView uses.

    I've inherited QAbstractTableModel and have overloaded the following.

    @
    bool MyModel::setData(const QModelIndex& index, const QVariant& value, int role)
    {
    Q_UNUSED(role);

     auto myClass = this->myClassList[index.row()];
    
     switch(index.column())
     {
          case Toggle:
               myClass->setToggle(value.toInd() == Qt::Checked);
               break;
     }
    
     this->dataChanged(index, index);
    
     return true;
    

    }

    QVariant MyModel::data(const QModelIndex& index, int role) const
    {
    QVariant value;

     auto myClass = this->myClassList[index.row()];
    
     if(role == Qt::DisplayRole || role == Qt::EditRole)
     {
          switch(index.column())
          {
               // Qt::ItemIsEditable columns
          }
     }
     else if(role == Qt::CheckStateRole)
     {
          switch(index.column())
          {
               case Toggle:
                    value = myClass->getToggle() ? Qt::Checked : Qt::Unchecked;
                    break;
          }
     }
     else if(role == Qt::BackgroundRole)
     {
           // Color the entire row red if checked
           value = myClass->getToggle() ? QColor(Qt::red) : QColor(Qt::white);
     }
    
     return value;
    

    }

    Qt::ItemFlags MyModel::flags(const QModelIndex& index) const
    {
    Qt::ItemFlags flags(Qt::ItemIsSelectabled | Qt::ItemIsEnabled);

     switch(index.column())
     {
          case Toggle:
               flags |= Qt::ItemIsCheckable;
               break;
     }
    
     return flags;
    

    }
    @

    The issue I am having is, when I check my box in the checkable column, the row doesn't change color immediately.
    When a window (any Windows window) covers up my table, the table will refresh and the row will be red. When i move the cell selector with an arrow key (the cell turns selection blue) the cell will turn red.

    Now, when I change the Checkbox column to an editable column, I can type "true" or "false" and when i hit enter, the entire row will turn red as expected. So the issue is occuring when I use the checkbox itself (not the textbox to the right of the checkbox)

    Looking at the source code for QStyledItemDelegate, I see that no editor widget is created for when checking the checkbox. This means no commitData() is ever called. That's the only difference I can see between using the checkbox and the textbox.

    I even tried connecting a slot to the dataChanged() signal to "update()" the QTableView, didn't work.

    Am I doing something wrong? Is there a workaround that I could use for this case?

    Edit: Added examples to better show what i'm talking about.

    Unchecked, default table:
    !http://i.imgur.com/T1rBAp4.png(No Check)!

    I checked the box and moved the selector over a couple of cells:
    !http://i.imgur.com/LepmSuN.png(Checked, with moved selection)!

    I checked the box and the row was colored AFTER I resized my window.
    !http://i.imgur.com/nCVVL6I.png(Checked, some event caused a refresh)!

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      Qt itemviews are optimized in a way that they only repaint what's necessary.
      In your case only YOU know whats necessary to be repainted because you don't tell the view that there is a logical connection between the checkbox and the whole row.

      You you either

      call view->update( QmodelIndex(..) )

      emit dataChanged() (from the model)

      for every index in the row QModelIndex(r,1) to QModelIndex(r,c) when the checkbox is toggeled.
      Usually you need to do this on the setData() method of your model ... like you already do but only for the affected index itself.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

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

        Thank you, I didn't realize the paints weren't constantly being called. I tried calling an update on dataChanged but it didn't appear to work at the time.

        I'll see what I can come up with.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          emitting dataChanged will automatically result in an repaint of the index by the view.

          So in your setData() method you should emit the dataChanged signal like this (for a table view):
          @
          emit dataChanged( index(index.row(),0), index(index.row(), columnCount()-1) );
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ASxa86
            wrote on last edited by
            #5

            This was the case. I was only telling the view to update the cell that changed, but as I needed to update the entire row's color, I had to emit the dataChanged on the entire row.

            @
            // Tell the view to update the entire row
            auto topLeft = this->createIndex(index.row(), 0);
            auto bottomLeft = this->createIndex(index.row(), index.column() - 1);
            this->dataChanged(topLeft, bottomLeft);
            @

            Thank you.

            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