Solved Changing the row colour depending on a bool value
-
I have a QTable and each row gets a bool value. Each row with the value "true" should be highlighted. The value depends on the users input.
Can anyone help me? Thank in advance -
Hint: don't code in German ;-) Or in any non-English language. All programming languages are in English, so when variables, classes and methods are in different language it is very hard to read. Plus, in global economy, there are high chances that your code will be read / modified by people from other countries.
Anyway, back to your problem: the best solution is to return proper colour for Qt::ItemDataRole::Background role. But as a temporary hack, this should do the trick:
auto lastItem = new QTableWidgetItem(favorit); lastItem->setBackground(QColor("#ffff00")); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 5, lastItem);
-
You have posted it in QML forum but mention "QTable"... are you using QtWidgets or QtQuick? Are you using a subclass of QAbstractItemModel to prepare your data?
-
@sierdzio oh yes sorry. Im using QTWidgets, still new to programming.
ui->tblLebensmittel->insertRow(ui->tblLebensmittel->rowCount()); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 0, new QTableWidgetItem(lebensmittel.mName)); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 1, new QTableWidgetItem(enumToString(lebensmittel.mKategorie))); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 2, new QTableWidgetItem(enumToString(lebensmittel.mEinheit))); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 3, new QTableWidgetItem(menge)); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 4, new QTableWidgetItem(vorrat)); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 5, new QTableWidgetItem(favorit)); if(favorit== "1") { ui->tblLebensmittel->setStyleSheet("QTableWidget::item {background-color: yellow}"); } else { return; } } }
In the last row i have "favorit" and this item has a bool value.
Right now the whole tablewidget is yellow. Thats not quite what I initially wanted;) -
Hint: don't code in German ;-) Or in any non-English language. All programming languages are in English, so when variables, classes and methods are in different language it is very hard to read. Plus, in global economy, there are high chances that your code will be read / modified by people from other countries.
Anyway, back to your problem: the best solution is to return proper colour for Qt::ItemDataRole::Background role. But as a temporary hack, this should do the trick:
auto lastItem = new QTableWidgetItem(favorit); lastItem->setBackground(QColor("#ffff00")); ui->tblLebensmittel->setItem(ui->tblLebensmittel->rowCount()-1, 5, lastItem);
-
Hi
Just as a note. we also had a look at coloring rows over here
https://forum.qt.io/topic/116882/how-to-update-qtablewidget-row-color-as-per-row-index-value-in-c -
-
@Paulus_rin said in Changing the row colour depending on a bool value:
Your code works, but right now the column favorit is coloured. The idea was to colour the row in which the bool value is true. The description may was a litte missleadiing.
Ah. Then you need to apply the same logic for the whole row and set / not set the background based on the row you are currently filling in.
-
@sierdzio
Thanks. Now I just have to make it work that the whole row is yellow, not just the "favorit" row. -
@Paulus_rin
Hi
If you variable favorit is true then just loop over the items and set its colorpseudo code.
for(int col =0; col < ui->tblLebensmittel->colCount(); col++ ) {
QTableWidgetItem *item = ui->tblLebensmittel->item( ui->tblLebensmittel->rowCount()-1, col);
item->setBackground(QColor("#ffff00"));
} -
The user can change the favorit value? If so, as mentioned before, I'd do this in your TableModel using the BackgroundRole.
Something like this:
QVariant YourTableModel::data(const QModelIndex& index, int role) const { if (role == Qt::BackgroundRole) { return QBrush(calcRowColour(index.row())); } return QVariant(); } QColor YourTableModel::calcRowColour(int row) const { if (m_things.at(row).isFavourite) { return QColor(255, 255, 153); } else { return QColor(255, 255, 255); } }
This will set the whole row to yellow wherever isFavourite == true.
Wherever you change the isFavourite value you have to emit the dataChanged signal with the BackgroundRole so that your view requests the new data.emit dataChanged(index(row, 0), index(row, 0), {Qt::BackgroundRole});
-
Thanks, now its working like i initially wanted it to.