Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

qt - How do I assign a border to a specific QTableWidgetItem or a row in a QTableWidget?

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

I do not want to select those cells and use the selection-color styles because different cells need to be selected/highlighted.

for ex. I have a table with 3 columns and 3 rows. All the cells have simple 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 is highlighted, a process in the background updates the values in the table and updates the value in the 3rd row to 4. Now I want to make the 3rd row have a red border around it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To change the border itself you'll probably need to create a custom delegate that does something along these lines:

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 QTableWidgetItem::setData() and the QModelIndex::data() functions to pass the necessary information back and forth between your table and the delegate

See the qt documentation for QItemDelegate


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...