Is there an easy way to change the background color of a row in a QTableWidget?

I know that you can scroll through QTableWidgetItems and change their colors, but what if I used setCellWidget and I have cells that are not QTableWidgetItems. I can not find a simple setRowColor method. It looks like it should be, since there are methods for alternating the colors of the lines and something else. Is there an easy way to do this without sub-classifying the table delegate?

The rhetorical question: I just want to change the color of the string, do I really need a new class for this?

+5
source share
2 answers

You can add a stylesheet to your QTableWidget like this:

QTableWidget::item {
    background-color: rgb(255, 85, 127);
}

You can install this code in the same way as the following:

QString _CustomStyle = QString(
      "QTableWidget::item {"
      "background-color: rgba(162, 186, 60);"
      "}";
tableWidget->setStyleSheet(_CustomStyle);

RGB ().

+3

, QTableWidget setData, , .

for (int column=0; column<4; column++)
{
    for (int row = 0; row<5; row++)
    {
        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg((row+1)*(column+1)));
        newItem->setData(Qt::BackgroundRole, (row%2)>0 ? Qt::red : Qt::blue);
        ui->tableWidget->setItem(row, column, newItem);
    }
}

, QTableView, ( , - QStandardItemModel) . setRowColor / , .

, ,

+5

All Articles