How to Update QTableWidget Row Color as per Row Index Value in C++?
-
Hi Team,
i am new in QT development, i want to change the Row Background color as per row index, like on click on button i want to change the row background color of third number row.
Please help me how can i do this? -
Hi,
thanks for reply.
we tried the links you mentioned but it is not reflecting on our UI.
Here, I this code, m_grid is QTablewidget, m_selectedFlangeShapes is a vector.
we have tried many options like setBackground color and setData methods also but nothing is getting reflected here.
Just to explain what we want is that when user click on import button for a single selection or selects multiple rows pressing ctrl button then all the selected row should turn to a color of our preference.
Please let me know if some other information is also required. Many thanks in advance to get me out of this blocker in advance. Actually I have been stuck on this tasks since one week but I am not able to find its solution. Quick help will be more than life saver :-) . thanks.
-
we have tried many options like setBackground color and setData methods also but nothing is getting reflected here.
@jsulm told you to to use
QTableWidgetItem::setBackground()
to set item background color.The code you have chosen to show has nothing to do with
QTableWidgetItem
or evenQTableWidget
. Please show what you tried to follow his advice. And when you do so kindly post code as text (inside```
at start & end), not as screenshot. -
Hi
Please notice that as long as the items are still selected
you cannot see the new color you assign as its covered by the selection color (blue/grey)!So while you color them you could also deselect them
QList<QTableWidgetItem *> selList = ui->tableWidget->selectedItems(); for (QTableWidgetItem *item : selList) { item->setBackgroundColor(Qt::red); item->setSelected(false); }
-
Hi, Thank you for the reply.
It worked at our end and now the new color is reflecting as below
But, Now I am facing a new issue as below:
Below is the smalll section of code in style sheet which is already applied for QTable widget.
"QTableWidget::item {" "border-top : 2px solid rgb(231,233,234);" "padding: 5px;" "margin: 0px;" "text-align: left;" "}"
Below is the image for old table when I was not getting row color changed. It am showing this to see the border lines of rows whihc is visible here.
Problem is that when I remove Qtablwwidget section of code of style sheet then separating lines of rows disappears but row color works fine when import button is clicked. but when I keep that section of code in the style sheet then if I click on import button it does not reflect although line of rows borders show perfectly fine.
What I need is, both row border lines and as well as rows color change when I click on import button.
void ShapePopup::importSelectedClicked() { QModelIndex index = m_grid->currentIndex(); QItemSelectionModel * select = m_grid->selectionModel(); QModelIndexList selectedList = select->selectedRows(); for(int i = 0; i < selectedList.length(); i++) { if(m_filterApplied) m_selectedFlangeShapes.push_back(m_filteredFlangeShapes.at(selectedList.at(i).row())); else m_selectedFlangeShapes.push_back(m_flangeShapes.at(selectedList.at(i).row())); for (int itr=0;itr<5;itr++) { m_grid->item(index.row()+i,index.column()+itr)->setBackground(QColor(250,226,209)); m_grid->item(index.row()+i,index.column()+itr)->setSelected(false); } } m_imported = true; }
Please help.
-
Hi
You cannot use setBackground and a stylesheet at the same time.
The stylesheet will win.So i think you need a delegate to color the rows while using a stylesheet.
Try this#include <QPainter> #include <QStyledItemDelegate> class HighLightDelegate : public QStyledItemDelegate { public: HighLightDelegate(QObject *poParent = nullptr) : QStyledItemDelegate(poParent) {} public: virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QColor color = index.data(Qt::UserRole).value<QColor>(); if (color.isValid()) { painter->fillRect(option.rect, color); } QStyledItemDelegate::paint(painter, option, index); } };
and then for use.
in ctor, do it once only ui->tableWidget->setItemDelegate(new HighLightDelegate); -- then in your ---importSelectedClicked--- m_grid->item(index.row()+i,index.column()+itr)->setData(Qt::UserRole, QColor(250,226,209));
-
Thanks mrjj for the correct solution, i am able to implement.