[英]QListView & QStandardItemModel check text before editing row
我想在用戶編輯之前檢查QListView
的文本。 如果它不適合圖案,我不想接受它。
目前我有一個QListView
和QStandardItemModel
。 我可以通過QStandardItemModel
輕松添加和刪除項目。 我還設置了列表視圖的模型。
列表或模型上是否有一些delegates
或event
功能可供編輯?
您可以從QStandardItemModel
重載data()
和setData()
函數,然后當用戶嘗試編輯項時,您將使用Qt::EditRole
調用setData
,您可以在那里進行處理。
http://qt-project.org/doc/qt-5.0/qtcore/qabstractitemmodel.html#setData
如果我理解正確,您想在用戶嘗試進入編輯模式時檢查項目的值嗎?
使用委托應該相當好:
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.