编辑模式下QTreeView委托中的持久文本
Persistent text in a QTreeView delegate during edit mode
我使用带有默认委托的QTreeView
来显示可编辑的模型数据。当我双击或按F2键在要更改的字段上时,我会得到文本编辑框,但当编辑器出现时,现有文本会被删除。我希望保留现有文本,但将其选中。Qt文档中的"可编辑树模型"示例正是具有这种行为,但是我一辈子都不知道它是如何实现的。据我所知,该示例没有使用自定义委托,也找不到与委托行为相关的调用。这可以在没有自定义委托的情况下完成吗?
编辑:这是我重新实现的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();
}
您的模型应该通过Qt::EditRole
返回您希望在ediitor中看到的数据。若数据是无效的(QVariant::isValid() == false)
,则编辑器将通过Qt::DisplayRole
请求数据。
相关文章:
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 用户定义的文本运算符(在原始模式下)存在问题
- 模式匹配文本并提取C++中的数据
- C++将特定的数据模式从RFID阅读器写入文本文件
- 二进制模式 + 格式化文本操作或文本模式 + 二进制数据操作 - 有意义吗?
- Linux绘制文本模式(无GUI,桌面)
- 我想将CSTRING数据存储到二进制中(不是在文本模式下,而是统一),但无法存储它
- C 将写入VGA文本模式存储器在屏幕上不可见
- 如何在 c++ "not whole screen" 的文本模式下清除特定文本?
- 二进制与文本模式下的文件操作 -- 性能问题
- 在文本模式下使用 seekg()
- 从字符串中提取特定的文本模式
- 用于显示文本模式菜单的库
- 如何在c++中将流从文本模式更改为二进制模式
- 移动文本模式光标不工作
- 我将如何执行此文本模式匹配
- 如何在文本模式下安全地混合 std::ifstream 的 tellg、seekg 和 read(*,n) 方法
- 奇怪的文本模式文件输出行为
- 用户界面-GUI和文本模式C++设计,以消除冗余(可选参数?功能过载?)
- 以文本模式将消息写入给定文件