I want to implement a User editable checkbox in QTableView
which is created using QAbstractModel. I am able to assign a checked and unchecked Checkbox but unable to make it editable.
flag is set to QItemIsUserCheckable
.
相关问题
- QML: Cannot read property 'xxx' of undefin
- QTextEdit.find() doesn't work in Python
- QT Layouts, how to make widgets in horizontal layo
- QT Layouts, how to make widgets in horizontal layo
- QT GUI freezes even though Im running in separate
相关文章
- ubuntu20.4中c#通过c++库调用python脚本
- Qt槽函数自动执行多遍
- Is there a non-java, cross platform way to launch
- How to get a settings storage path in a cross-plat
- How to set the font size of the label on pushbutto
- Why doesn't valgrind detect a memory leak in m
- QTreeView remove decoration/expand button for all
- qt界面拥挤
after i went through a bunch of forums trying to figure out how to do this and nothing worked i ended up finding a note in http://doc.qt.io/qt-4.8/modelview.html
At the end of 2.2 Extending the Read Only Example with Roles the texts notes "Now we need to determine how using a separated model impacts the application's performance, so let's trace how often the view calls the data() method. In order to track how often the view calls the model, we have put a debug statement in the data() method, which logs onto the error output stream. In our small example, data() will be called 42 times. Each time you hover the cursor over the field, data() will be called again — 7 times for each cell. That's why it is important to make sure that your data is available when data() is invoked and expensive lookup operations are cached."
This made me come to the realize that "yourModal::data()" is called continuously, however, for extra update i added a time in the setup to run checks for the selection. Mine has been a little trickier because i am actually opening a groupBox pop-up window from my MainWindow, enjoy the extra code.
So basically this is my code i came up with and it runs great:
You can do it easily by implementing model's
setData()
method like this:And don't forget about your model's
data()
method:What you want to implement is a custom delegate. Take a look at the QAbstractItemDelegat class for more information on the actual implementation.
First things first.
A QAbstractItemModel is a good choice for a model only if you're creating a tree model, while in most of other cases it is better to use a QAbstractTableModel or even a QAbstractListModel, since they save you the work of implementing virtual methods for the specific model type (table or list).
The documentation on these models is quite thorough and tells you which functions to implement for the model to be editable. I'm going to go with the table model for this short explanation. The main functions are:
The others I will ignore for this case.
All the Qt views work the same way - when they are shown, they populate themselves with the data from the model, their elements are editable/selectable etc. according to the value returned by flags() for their index. When they are edited, the value is passed to the model via the setData() function.
What you seem to be missing is the Qt::ItemIsEditable flag in the flags() method.