How to assign a border to a specific QTableWidgetItem or row in a QTableWidget?

I am trying to force certain cells in my QTableWidget to have different colored borders based on the information contained in an element (cell).

I do not want to select these cells and use color picker styles, because I need to select / select different cells.

for ex. I have a table with 3 columns and 3 rows. All cells have plain text in each of them.
[] [Name] [Value] [Units]
[1] [one] [1] [cm]
[2] [two] [2] [in]
[3] [three] [3] [m]

The 1st row is selected by the user and highlighted, the process in the background updates the values ​​in the table and updates the value in the third row to 4. Now I want the 3rd row to have a red border around it.

+2
source share
2 answers

To change the border, you probably need to create a custom delegate that does something in this direction:

class MyDelegate : public QItemDelegate { public: MyDelegate( QObject *parent ) : QItemDelegate( parent ) { } void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { QItemDelegate::paint( painter, option, index ); if( /* some condition */ ) { painter->setPen( Qt::red ); painter->drawRect( option.rect ); } } } 

Then you can call:

 myTableWidget->setItemDelegate( new MyDelegate(this) ); 

You can use the QTableWidgetItem::setData() and QModelIndex::data() functions to pass the necessary information between your table and the delegate

See qt documentation for QItemDelegate

+5
source

AFAIK, you can select a cell of a different color. I do not see any option that only changes the border of the cell.

0
source

Source: https://habr.com/ru/post/896836/


All Articles