QHeadView的paintSection方法怎样设置表头背景色但是不能去掉表头的分割线
要设置QHeadView的表头背景色,可以在paintSection方法中使用QPainter的fillRect方法来绘制背景色。同时,为了保留表头的分割线,需要在paintSection方法中绘制分割线。
以下是一个示例代码:
void MyHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
// 绘制背景色
painter->fillRect(rect, QColor(255, 255, 0)); // 设置为黄色背景
// 绘制文本
painter->save();
painter->setPen(Qt::black);
painter->setFont(QFont("Arial", 10));
painter->drawText(rect, Qt::AlignCenter, model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString());
painter->restore();
// 绘制分割线
painter->save();
painter->setPen(Qt::gray);
painter->drawLine(rect.topRight(), rect.bottomRight());
painter->restore();
}
在上述代码中,我们首先使用fillRect方法绘制背景色,然后使用drawText方法绘制文本。最后,使用drawLine方法在表头的右边绘制分割线。
注意:上述代码是在继承自QHeaderView的自定义类中实现的,你需要将代码中的MyHeaderView替换为你自己的类名
原文地址: https://www.cveoy.top/t/topic/ihqk 著作权归作者所有。请勿转载和采集!