c++ - QListView 样式表不适用于委托(delegate)

标签 c++ qt delegates qlistview

我有带有自定义委托(delegate)的 QListView

自定义委托(delegate)绘制方法:

QStyleOptionViewItemV4 opt = option;

initStyleOption(&opt,index);

opt.decorationSize       = QSize(deco_size,deco_size);
opt.decorationAlignment |= Qt::AlignCenter;
opt.displayAlignment    |= Qt::AlignCenter;
opt.decorationPosition   = QStyleOptionViewItem::Top;
opt.font.setBold(true);

const QWidget *widget = opt.widget;

QStyle *style = widget ? widget->style() : QApplication::style();

style->drawControl(QStyle::CE_ItemViewItem,&opt,painter);

我的问题是,如果我为 QListView::item 设置样式表 例如:

#lv::item:selected:active  { background: red; }

这行不通!

如果我使用内部,没有自定义委托(delegate),一切都很好。

2. 我只使用自定义委托(delegate)将装饰图标放在文本顶部,是否有样式表选项强制图标显示在顶部?

最佳答案

我错过了 QStyle::drawControl 函数“widget”的第四个参数:

style->drawControl(QStyle::CE_ItemViewItem,&opt,painter,widget);

这里,QStyledItemDelegate源代码,paint方法:

https://qt.gitorious.org/qt/webkit/source/435bbd4be73768f617e4a4083a345d1d8d62daa3:src/gui/itemviews/qstyleditemdelegate.cpp#L444

关于c++ - QListView 样式表不适用于委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635281/

相关文章:

c# - 使用委托(delegate)的条件赋值

c++ - 通过 OpenGL Assimp 加载的对象有时会反转法线

c++ - 创建玩家 vector 并向每个玩家添加信息

qt - 我们如何连接带有不同参数的信号和插槽?

ios - ABPeoplePickerNavigationController 委托(delegate)不工作

c# - 有没有办法通过委托(delegate)直接调用属性 setter ?

c++ - 代码块断点忽略范围

c++ - 'int' 之前的预期不合格 ID? Arduino库

qt - 我如何着手在 QT 中创建功能区组件?

c++ - QWebView : print problems