C++ 在QItemDelegate重新实现的绘制功能中,setFont不工作
我已经重新实现了QTreeWidget的绘制功能,我想以粗体显示第二列的数据,但它不起作用 我怎样才能修好它C++ 在QItemDelegate重新实现的绘制功能中,setFont不工作,c++,qt,qtreeview,qitemdelegate,C++,Qt,Qtreeview,Qitemdelegate,我已经重新实现了QTreeWidget的绘制功能,我想以粗体显示第二列的数据,但它不起作用 我怎样才能修好它 void extendedQItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) cons
void extendedQItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QString txt = index.data().toString();
painter->save();
QFont painterFont;
if (index.column() == 1) {
painterFont.setBold(true);
painterFont.setStretch(20);
}
painter->setFont(painterFont);
drawDisplay(painter, option, rect, txt);
painter->restore();
}
我附上了问题的屏幕截图,下半部分应该是粗体的
您忘记通过成员函数将extendedQItemDelegate添加到QTreeView/QTreeWidget对象
void extendedQItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QString txt = index.data().toString();
painter->save();
QStyleOptionViewItem optCopy = option; // make a copy to modify
if (index.column() == 1) {
optCopy.font.setBold(true); // set attributes on the copy
optCopy.font.setStretch(20);
}
drawDisplay(painter, optCopy, rect, txt); // use copy to paint with
painter->restore();
}
例如:
QTreeWidget* tree_view = ...;
extendedQItemDelegate* extended_item_delegate = ...;
tree_view->setItemDelegate(extended_item_delegate);
您需要复制const QStyleOptionViewItem&选项,将字体更改应用于该副本,然后使用副本而不是传递给函数的原始选项进行绘制
void extendedQItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
const QString txt = index.data().toString();
painter->save();
QStyleOptionViewItem optCopy = option; // make a copy to modify
if (index.column() == 1) {
optCopy.font.setBold(true); // set attributes on the copy
optCopy.font.setStretch(20);
}
drawDisplay(painter, optCopy, rect, txt); // use copy to paint with
painter->restore();
}
刚意识到这是一个老问题,但它已经跳到了qt标签的顶部。请经常解释它是如何不起作用的。如果是视觉问题,提供屏幕截图可能更好;treeEvents->setItemDelegatedelg;我在谷歌上搜索了我的问题,发现我应该更改常量QStyleOptionViewItem&option字体,但因为它是常量,所以我无法更改它,我尝试更改它的副本,但它没有显示任何内容