我已经使用QTableview和QAbstractTableModel创建了一个表。我已经使用QHeaderView添加了一些垂直标题。在其中一个标题单元格中,我想使用委托..
我正在使用委托,但它没有任何影响..
我哪里做错了?
发布于 2012-08-10 14:56:03
我自己也有这个问题。来自Qt documentation的答案既简单又令人恼火:
注意:每个标头呈现每个部分本身的数据,并且不依赖于委托。因此,调用头的setItemDelegate()函数将不起作用。
换句话说,您不能将委托与QHeaderView一起使用。
发布于 2018-06-06 22:10:16
对于记录,如果你想设置一个QHeaderView部分的样式,你必须通过头数据模型(改变Qt::FontRole等)来实现。或者派生您自己的QHeaderView (别忘了用“setVerticalHeader()”将它传递给您的表),并覆盖它的paintSection()-function。即:
void YourCustomHeaderView::paintSection(QPainter* in_p_painter, const QRect& in_rect, int in_section) const
{
if (nullptr == in_p_painter)
return;
// Paint default sections
in_p_painter->save();
QHeaderView::paintSection(in_p_painter, in_rect, in_section);
in_p_painter->restore();
// Paint your custom section content OVER a specific, finished
// default section (identified by index in this case)
if (m_your_custom_section_index == in_section)
{
QPen pen = in_p_painter->pen();
pen.setWidthF(5.5);
pen.setColor(QColor(m_separator_color));
in_p_painter->setPen(pen);
in_p_painter->drawLine(in_rect.right(), in_rect.top(), in_rect.right(), in_rect.bottom());
}
}这个简化的示例当然可以通过样式表轻松完成,但理论上您可以使用此方法绘制您喜欢的任何内容。
https://stackoverflow.com/questions/11895388
复制相似问题