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

流过昼夜 提交于 2019-12-03 13:43:42

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 .)

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