【问题标题】:QTableWidget style per QTableWidgetItem每个 QTableWidgetItem 的 QTableWidget 样式
【发布时间】:2017-08-09 06:32:33
【问题描述】:

我正在使用一个简单的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。我只想为“some number”和“some text 2”项目绘制边框。

是否可以在坚持使用QTableWidgetQTableWisgetItems 的同时这样做?我不能使用QObject::setProperty 设置一些属性来识别样式表中的项目,因为QTableWidgetItems 不是QObjects ...

【问题讨论】:

    标签: css qt qt5 qtablewidget qtablewidgetitem


    【解决方案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));
    

    【讨论】:

    • 就是这样!非常感谢:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多