我有一个小的补充。
如果在Mac OS X上编译skyhisi的代码,则组合框(combobox)看起来不像原生复选框的组合框。您可以在截图中看到。
测试使用的是 qt-4.8.5 和 5.1.1。
看起来 Qt 是自己绘制这些控件。我们的团队无意中发现了以下解决方法。您可以子类化 QStyledItemDelegate
并以以下方式重新实现 paint()
:
void SubclassOfQStyledItemDelegate::paint(QPainter * painter_, const QStyleOptionViewItem & option_, const QModelIndex & index_) const
{
QStyleOptionViewItem & refToNonConstOption = const_cast<QStyleOptionViewItem &>(option_);
refToNonConstOption.showDecorationSelected = false;
QStyledItemDelegate::paint(painter_, refToNonConstOption, index_);
}
您可以通过将以下代码添加到skyhisi的代码中,将此委托设置为组合框:
SubclassOfQStyledItemDelegate *delegate = new SubclassOfQStyledItemDelegate(this);
combo->setItemDelegate(delegate);
此委托安装的comboBox外观如下:
在Windows上可能会出现不同的问题:复选框的文本具有固定的背景或项目周围的点状边框:
要更改此外观,可以在覆盖的 paint 函数中在 QStyledItemDelegate::paint(painter_, refToNonConstOption, index_) 之前添加以下行(在代码示例中,此行被注释):
refToNonConstOption.state &= ~QStyle::State_HasFocus & ~QStyle::State_MouseOver;
结果:
QAbstractListModel
并实现rowCount
、data
、flags
和setData
方法。确保模型处理Qt::CheckStateRole
角色。在setData
方法中,你可以触发信号,因为模型是QObject派生的。 - Silas Parker