1
我有QTableView
派生视图的QStyledItemDelegate
派生对象。我根据模型索引数据类型进一步委托绘画和编辑器创建。对于bool
s,我想通过复选框来表示状态 - 但复选框从不出现。无法在QStyledItemDelegate中绘制复选框
这是基础的委托漆功能:
void Sy_QtPropertyDelegate::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
painter->save();
if (index.column() == 0) {
...
} else {
QVariant var = index.data();
bool modified = index.data(Sy_QtPropertyModel::ModifiedRole).toBool();
// If the data type is one of our delegates, then push the work onto
// that.
auto it = delegateMap_.find(var.type());
if (it != delegateMap_.end()) {
(*it)->paint(painter, option, index);
} else if (var.type() != QVariant::UserType) {
...
} else {
...
}
}
painter->restore();
}
而且bool
子委托漆功能:
void Sy_boolPD::paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
painter->save();
bool checked = index.data().toBool();
bool modified = index.data(Sy_QtPropertyModel::ModifiedRole).toBool();
QStyle* style = Sy_application::style();
if (modified) {
QStyleOptionViewItemV4 bgOpt(option);
bgOpt.backgroundBrush = QBrush(Sy_QtPropertyDelegate::ModifiedColour);
style->drawControl(QStyle::CE_ItemViewItem, &bgOpt, painter);
}
QStyleOption butOpt(option);
butOpt.state = QStyle::State_Enabled;
butOpt.state |= checked ? QStyle::State_On : QStyle::State_Off;
style->drawControl(QStyle::CE_CheckBox, &butOpt, painter);
painter->restore();
}
如果我强迫modified
是真实的,背景是表的颜色cout
ing butOpt
的rect
和state
成员显示他们是正确的 - 但没有显示复选框!将QStyle::CE_CheckBox
设置为任何其他类型也会导致无法呈现。
我已经和Qt的MVC框架合作过很多次,但是我看不出这里出了什么问题。