PyQT-如何使QTreeview不可编辑但也可以选择? [英] PyQT -- How can you make a QTreeview uneditable but also selectable?

查看:601
本文介绍了PyQT-如何使QTreeview不可编辑但也可以选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从wxPython切换到PyQT,而QTreeview遇到了一些麻烦.我有一个QTreeview,它将显示归类为可扩展部分的数据,但是该TreeView中的数据不应是可编辑的,但是我需要能够让用户选择数据(双击将执行另一种方法).我不确定如何使其变为只读但也可以选择.我使用带有QStandardItem的QStandardItemModel来保存数据.

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.

推荐答案

您可以在创建QSandardItem

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

您可以通过调用

my_treeview.setEditTriggers(QAbstractItemView.NoEditTriggers)

默认情况下,树视图应该允许您选择项目,但是如果要更改默认行为,则需要查看树视图的setSelectionMode()setSelectionBehavior()方法(它们分别用于QAbstractItemView QTreeView继承自).这些方法的C ++文档可以在此处中找到通常在PyQt文档上使用,因为它通常更完整,并且翻译成Python代码也不太困难.只需将::的所有实例替换为.)

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

这篇关于PyQT-如何使QTreeview不可编辑但也可以选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆