编辑模式下QTreeView委托中的持久文本
问题描述:
我正在使用QTreeView
与默认委托来显示可编辑模型数据。当我双击或在要更改的字段上按F2时,我会看到文本编辑框,但编辑器出现时现有文本会被删除。我希望现有的文本保持不变,但已被选中。 Qt文档中的“可编辑树模型”示例恰好具有这种行为,但是我不知道它是如何实现的。就我所知,该示例不使用自定义委托,并且没有与我可以找到的委托行为相关的调用。这可以在没有自定义委托的情况下完成吗?编辑模式下QTreeView委托中的持久文本
编辑:这是我的代码,重新实现QAbstractItemModel::data()
:
QVariant projectModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
node* item = static_cast<node*>(index.internalPointer());
if (role == Qt::DisplayRole)
return QVariant(item->data(index.column()).c_str());
else if (role == Qt::ForegroundRole)
return item->text_color(index.column());
else if (role == Qt::BackgroundRole)
return item->background_color(index.column());
else if (role == Qt::CheckStateRole)
return item->check_state(index.column());
else if (role == Qt::DecorationRole)
return item->icon(index.column());
else if (role == Qt::TextAlignmentRole)
return item->text_alignment(index.column());
else
return QVariant();
}
答
你的模型应该返回数据,要在ediitor看到,通过Qt::EditRole
。如果数据无效(QVariant::isValid() == false)
则编辑器将通过Qt::DisplayRole
请求数据。
检查你的模型返回为'Qt :: EditRole' – 2014-10-20 20:02:58
不太确定你的意思。 Qt :: EditRole在我的调试窗口中评估为“0n2”,但它应该是一个常量值,不依赖于我的模型。 – Carlton 2014-10-20 20:12:03
你的模型应该通过'Qt :: EditRole'返回一个数据,你想在ediitor中看到。如果数据无效('QVariant :: isValid()== false'),那么编辑器将通过'Qt :: DisplayRole'请求数据。 – 2014-10-21 08:03:07