我使用简单的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"项目
是否可以在坚持使用QTableWidget
和QTableWisgetItem
时这样做?我无法使用QObject::setProperty
设置一些属性来标识样式表中的项目,因为QTableWidgetItem
s不是QObject
s ...
答案 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));