Qt QComboBox 每个项目具有不同的背景颜色?
有没有办法为 QComboBox 中的每个项目设置不同的背景颜色?
Is there a way to set a different background color for each item in a QComboBox
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想唯一的方法是编写自己的模型,继承
QAbstractListModel
,重新实现rowCount()
和data()
可以设置每个项目的背景颜色(使用BackgroundRole
角色)。然后,使用 QComboBox::setModel() 使 QComboBox 显示它。
这是一个简单的示例,我创建了自己的列表模型,继承了 QAbstractListModel :
现在可以轻松地将这个模型与组合框一起使用:
I guess the only way to do it would be to write your own model, inheriting
QAbstractListModel
, reimplementingrowCount()
anddata()
where you can set the background color for each item (using theBackgroundRole
role).Then, use
QComboBox::setModel()
to make theQComboBox
display it.Here is a simple example, where I created my own list model, inheriting
QAbstractListModel
:It is now easy to use this model with the combo box :