2017-08-08 32 views
0

我用一个简单的QTableWidget显示一些QTableWidgetItems,它看起来像这样:QTableWidget的风格每QTableWidgetItem

+-------------+-------------+ 
|    | 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”项目绘制边框。

坚持使用QTableWidgetQTableWisgetItem s是否可以这样做?我不能使用QObject::setProperty设置某些属性,以确定在样式表中的项目,因为QTableWidgetItem s为没有QObject小号...

回答

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)); 
+0

这就是它!非常感谢 :-) –

相关问题