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. QTableWidget: repaint of specific cell
QtWS25 Last Chance

QTableWidget: repaint of specific cell

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 704 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.
  • sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on last edited by sitesv
    #1

    Hello!
    Is it possible to force a repaint of a specific cells?

    As I know,

    tableWidget->viewport()->update();
    

    repaints all cells.

    I have tried to do this:

    QRect rrr = ui->tableWidget->visualItemRect(ui->tableWidget->item(myRow, myCol));
    ui->tableWidget->update(rrr);
    

    But it is not working.

    This needs for color updating in 'paint' event of table delegate.

    It works if I update text only.

    Thank you!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      If you dont set any data/roles on the item, then view dont considered it "dirty"

      So how do you set this delegate color ?

      for test you can try

          auto model = qobject_cast<QAbstractTableModel *> ( ui->tableWidget->model() );
          if (!model) return;
          QModelIndex idx = model->index(myRow, myCol);
          if (idx.isValid())
              model->dataChanged(idx, idx,   QVector<int>({Qt::DisplayRole, Qt::EditRole}));
      
      

      with ->update(rrr);

      and see if its then repainted.

      sitesvS 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        If you dont set any data/roles on the item, then view dont considered it "dirty"

        So how do you set this delegate color ?

        for test you can try

            auto model = qobject_cast<QAbstractTableModel *> ( ui->tableWidget->model() );
            if (!model) return;
            QModelIndex idx = model->index(myRow, myCol);
            if (idx.isValid())
                model->dataChanged(idx, idx,   QVector<int>({Qt::DisplayRole, Qt::EditRole}));
        
        

        with ->update(rrr);

        and see if its then repainted.

        sitesvS Offline
        sitesvS Offline
        sitesv
        wrote on last edited by sitesv
        #3

        Hi!

        @mrjj said in QTableWidget: repaint of specific cell:

        So how do you set this delegate color?

        In paint section, I do color blinking of specific cells.
        The text is not changing.

        if(i == 0)
          brush = QBrush(QColor::yellow);
        else if(i == 1)
          brush = QBrush(QColor::red);
        painter->fillRect(rect, brush);
        painter->drawText(rect, Qt::AlignCenter, textCell);
        

        Something like this.

        Your code works also without executing 'update' method.
        Thank you!

        mrjjM 1 Reply Last reply
        0
        • sitesvS sitesv

          Hi!

          @mrjj said in QTableWidget: repaint of specific cell:

          So how do you set this delegate color?

          In paint section, I do color blinking of specific cells.
          The text is not changing.

          if(i == 0)
            brush = QBrush(QColor::yellow);
          else if(i == 1)
            brush = QBrush(QColor::red);
          painter->fillRect(rect, brush);
          painter->drawText(rect, Qt::AlignCenter, textCell);
          

          Something like this.

          Your code works also without executing 'update' method.
          Thank you!

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sitesv
          Hi
          Ahh so its only in the delegate so view doesn't know its changed.
          Im not sure the model way is the most compact way, but its how a model woul dinform the view
          data has changed so I think it should be ok.

          Do note its its what happens if you call item->setData on some role so
          that could also be used.

          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