⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

how to hightlight certain rows in qt tablewidget?

  • 0_1567752387495_b606a84f-2b0f-4ff4-9775-616c7203d4b2-image.png
    hello, everyone.
    as showed in the above picture, now I want to hightlight certain lines in the table, meaning that after I click the down forward arrow in the scroll bar right in the picture, the hightlighted rows move up and when I click the up forward arrow in the scroll bar, the hightlighted rows shows again.
    how to do that? Thank in advance!

  • Lifetime Qt Champion

    and this purple line should be something other than the currently
    selected row ?
    Like a color code on a row. ?

  • @mrjj The purple line is not the selected row, it shows the mouse selecting behavior. The yellow lines is the selected rows, though just show the first column.

  • @mrjj It seems like that the tablewidget can only manipulate the current showing rows.

  • Lifetime Qt Champion

    so i assume you cannot just use the selection line for this.
    (it can be set to select whole row at once)

    Well you can color the items backgrounds. that also work even if
    the items are not currently shown.

  • @mrjj ```

        QTableWidgetItem *item1 = new QTableWidgetItem;
    it is the code, following your advice, however, no effect.

  • Lifetime Qt Champion

    Did you remember to adjust row/col count ?
    Work fine here

    alt text

        for (int c = 0; c < ui->tableWidget->rowCount(); c++) {
            QTableWidgetItem *item1 = new QTableWidgetItem;
            auto col = QColor( rand() % 255, rand() % 255, rand() % 255);
            item1->setBackgroundColor( col  );
            ui->tableWidget->setItem(c, 0, item1);

  • @mrjj Thanks a lot, the problem is solved!

Log in to reply