如何为当前项目制作 QCombobox 绘画项目委托? (Qt 4)
QCombobox 设置项目委托不为当前项目绘制。
我正在尝试创建一个显示不同线类型(实线、点线、短划线等)的组合框。 目前我正在为其内容设置项目委托 以便绘制/绘制线型而不是显示名称。 所有线条类型都正在正确绘制,但是一旦我从 组合框,组合框的当前索引仅显示行名称而不绘制它。 我怎样才能让它在当前的线型上绘制选定的线型 组合框索引?
QCombobox set Item delegate not painting for current Item..
I am trying to create a combo box showing different line types (Solid, Dotted, Dash etc). Currently i am setting item delegate for its content
so as to draw/paint line type instead of displaying names. All line types are drawing currectly but as soon as i am selecting any line type from the
combobox, the current index of combo box is displaying just the line name and not painting it. How can i make it paint the selected line type on the current
combo box index?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
委托在组合弹出窗口中绘制项目:
paintEvent 在组合中绘制当前项目。 当然,您可以手动绘制它,但是有一种简单的方法可以绘制组合框控件本身(如果您想要当前的箭头按钮或其他东西):
Delegate to paint items in combo popup:
paintEvent to paint current item in combo. You can, of course, paint it manually, but there is the simple way to draw combo box control itself (if you want an arrowbutton or smth in the current):
我想我以前遇到过这个问题,有一个委托在下拉菜单中正确显示该行,但在组合框本身中却没有。
文档 (http://doc.trolltech.com/4.4/qcombobox.html )指出:
“对于组合框标签中的文本和图标,使用具有 Qt::DisplayRole 和 Qt::DecorationRole 的模型中的数据。”
我怀疑涉及为 DecorationRole 返回合适数据的模型的方法可能有效,但让它按照您想要的方式运行可能会出现问题。
I think I've encountered this problem before, having a delegate that displays the line correctly in the drop-down menu but not in the combo box itself.
The documentation (http://doc.trolltech.com/4.4/qcombobox.html) states that:
"For the text and icon in the combobox label, the data in the model that has the Qt::DisplayRole and Qt::DecorationRole is used."
I suspect that an approach which involves a model that returns suitable data for the DecorationRole might work, but it could be problematic to get it to behave just the way you want it to.
您还可以将图像保存为图标并使用 QComboBox::setIconSize() 以避免缩放。
You can also save your images to icons and use QComboBox::setIconSize() to avoid scaling.
只需重写paintEvent。 这是一些草图代码:
Just override paintEvent. Here is some sketch code: