[英]QListView & QStandardItemModel check text before editing row
I want to check the text of a row in QListView
before the user is editing it. 我想在用户编辑之前检查
QListView
的文本。 If it doesn't fit a pattern, I don't want to accept it. 如果它不适合图案,我不想接受它。
Currently I have a QListView
and QStandardItemModel
. 目前我有一个
QListView
和QStandardItemModel
。 I can easily add and remove items via the QStandardItemModel
. 我可以通过
QStandardItemModel
轻松添加和删除项目。 I also set the model of the list view. 我还设置了列表视图的模型。
Are there some delegates
or event
function(s) on the list or the model for editing? 列表或模型上是否有一些
delegates
或event
功能可供编辑?
you can overload data()
and setData()
functions from QStandardItemModel
, then when user tries to edit item your setData
will be called with Qt::EditRole
and there you can do your processing. 您可以从
QStandardItemModel
重载data()
和setData()
函数,然后当用户尝试编辑项时,您将使用Qt::EditRole
调用setData
,您可以在那里进行处理。
http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#setData http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#setData
If I understand you correctly, you want to check the value of an item at the time the user attempts to enter the edit mode? 如果我理解正确,您想在用户尝试进入编辑模式时检查项目的值吗?
Using a delegate should work for this fairly well: 使用委托应该相当好:
class MyItemDelegate : public QItemDelegate {
public:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
if(index.data() == /* do whatever check you want here */) {
return NULL; // Prevent editing
}
return QItemDelegate::createEditor(parent, option, index);
}
};
listView->setItemDelegate(new MyItemDelegate());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.