我正在使用一个简单的 QTableWidget
来显示一些 QTableWidgetItems
,如下所示:
+-------------+-------------+
| | some text 1 |
| some number +-------------+
| | some text 2 |
+-------------+-------------+
| | some text 1 |
| some number +-------------+
| | some text 2 |
+-------------+-------------+
我知道我可以通过为 QTableWidget
设置样式表来围绕 QTableWidgetItems
绘制边框
QTableView::item {
border-bottom: 1px solid black;
}
但这适用于所有QTableWidgetItems
。我只想为“一些数字”和“一些文本 2”元素绘制边框。
是否可以在坚持使用 QTableWidget
和 QTableWisgetItem
的同时这样做?我不能使用 QObject::setProperty
设置一些属性来标识样式表中的元素,因为 QTableWidgetItem
不是 QObject
......
最佳答案
使用委托(delegate),例子
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));
关于css - 每个 QTableWidgetItem 的 QTableWidget 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45577434/