PyQT — How can you make a QTreeview uneditable but also selectable?

元气小坏坏 提交于 2019-12-04 22:51:23

问题


I just switched from wxPython to PyQT and I am having some trouble with the QTreeview. I have a QTreeview that will display data categorized into sections that are expandable, but the data in this TreeView should not be editable, but I need to be able to have the user select the data (doubleclicking is going to execute another method). I am not certain how to make it readonly but also selectable. I am using the QStandardItemModel with the QStandardItem to hold the data.

Any help would be much appreciated.


回答1:


You can set individual items to be uneditable by doing this when you create the QSandardItem

item = QStandardItem('my_item_text')
item.setEditable(False)

You can disable editing for the entire treeview by calling

my_treeview.setEditTriggers(QAbstractItemView.NoEditTriggers)

By default the treeview should allow you to select items, but if you want to change the default behaviour you will want to look at the setSelectionMode() and setSelectionBehavior() methods of the treeview (well they are for QAbstractItemView which QTreeView inherits from). c++ documentation for these methods can be found here which I generally use over the PyQt documentation as it is often more complete, and it isn't too difficult to translate into Python code. Just replace all instances of :: with .)



来源:https://stackoverflow.com/questions/23305320/pyqt-how-can-you-make-a-qtreeview-uneditable-but-also-selectable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!