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

后端 未结 2 812
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 11:07

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 th

相关标签:
2条回答
  • 2020-12-18 11:56

    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

    0 讨论(0)
  • 2020-12-18 12:09

    AFAIK, you can highlight the cell with a different color. I don't see any option that changes only the border of the cell.

    0 讨论(0)
提交回复
热议问题