css - 每个 QTableWidgetItem 的 QTableWidget 样式

标签 css qt qt5 qtablewidget qtablewidgetitem

我正在使用一个简单的 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”元素绘制边框。

是否可以在坚持使用 QTableWidgetQTableWisgetItem 的同时这样做?我不能使用 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/

相关文章:

javascript - hide() 和 show() 导致页面偶尔跳转

c++ - 如何使用线程创建图像缩略图

c++ - 使用 Poppler Qt4 c++

c++ - 如何在 Qt 和 C++ 中使用 "Interface Class"来通信两个线程

c++ - 更改 QBarSet 中单个 barr 的颜色

jquery - 鼠标悬停和隐藏某些元素的问题

html - 如何在输入表单顶部对齐标签?

c++ - 如何从数据库文件创建 QByteArray

c++ - 从 C++ 显示 qml 与 qmlscene 显示的不同

css - 跨浏览器按钮填充问题