【发布时间】:2013-09-02 08:22:45
【问题描述】:
我正在制作一个表格控件,该控件显示除了其模型的 DisplayRole 中的其他文本数据之外的一些其他文本数据。在所有其他方面,文本和单元格显示应该相同。我遇到的问题是正确显示突出显示的单元格。
我目前正在使用以下代码:
void MatchDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
painter->save();
QString str = qvariant_cast<QString>(index.data())+ "\n";
str += QString::number(qvariant_cast<float>(index.data(Qt::UserRole)));
if (option.state & QStyle::State_Selected)
painter->setBrush(option.palette.highlightedText());
else
painter->setBrush(qvariant_cast<QBrush>(index.data(Qt::ForegroundRole)));
painter->drawText(option.rect, qvariant_cast<int>(index.data(Qt::TextAlignmentRole)), str);
painter->restore();
}
但是,结果如下所示:
文本颜色错误,单元格周围没有虚线,当控件失去焦点时,单元格保持蓝色而不是像默认单元格那样变成浅灰色。
应该如何更改绘画代码来解决这些问题?
【问题讨论】:
-
看来,您没有分别设置 QPen .(Pen color and style) white 和 Qt::DashLine。
-
如何根据调色板设置笔?
-
调用基类paint..QStyledItemDelegate::paint(painter, option, index);" 可以解决你的问题
-
我希望重新实现该部分,而不仅仅是使用它。
-
默认情况下,当您选择一个项目委托时,它将用您的背景突出显示虚线边框和高亮文本
标签: c++ qt user-interface