每个QTableWidgetItem的QTableWidget样式

时间:2017-08-08 20:25:11

标签: css qt qt5 qtablewidget qtablewidgetitem

我使用简单的QTableWidget来显示一些QTableWidgetItems,如下所示:

+-------------+-------------+
|             | some text 1 |
| some number +-------------+
|             | some text 2 |
+-------------+-------------+
|             | some text 1 |
| some number +-------------+
|             | some text 2 |
+-------------+-------------+

我知道我可以通过设置QTableWidgetItems的样式表来在QTableWidget周围绘制边框

QTableView::item {
    border-bottom: 1px solid black;
}

但这适用于所有QTableWidgetItems。我只想为"某些数字"绘制边框。和#34;一些文字2"项目

是否可以在坚持使用QTableWidgetQTableWisgetItem时这样做?我无法使用QObject::setProperty设置一些属性来标识样式表中的项目,因为QTableWidgetItem s不是QObject s ...

1 个答案:

答案 0 :(得分:1)

使用委托,示例

class MyDelegate : public QItemDelegate
{
  public:
    MyDelegate( QObject *parent ) : QItemDelegate( parent ) { }
    void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
};

void MyDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    QItemDelegate::paint( painter, option, index );
    painter->setPen( Qt::red );
    painter->drawLine( option.rect.topLeft(), option.rect.bottomLeft() );
   // What line should you draw
   // painter->drawLine( option.rect.topLeft(), option.rect.topRight() );
   // painter->drawLine( option.rect.topLeft(), option.rect.bottomLeft() );
}
...

        m_TableWidgetClass->setItemDelegateForRow(row, new MyDelegate( this));
        //m_TableWidgetClass->setItemDelegateForColumn(column, new MyDelegate( this));