不可编辑的QListView
我有一个显示项目列表的 QListView,但我不希望编辑这些项目(目前双击该项目允许您编辑它们)。
这是我的代码:
self.listView = QListView()
self.model = QStringListModel([ "item1" , "item2" , "item3" ])
self.listView.setModel( self.model )
self.layout = QGridLayout()
self.layout.addWidget(self.listView, 0 , 0 )
self.setLayout(self.layout)
I have a QListView displaying a list of items but I don't want the items to be edited (Currently a double click on the item allows you to edit them).
This is my Code:
self.listView = QListView()
self.model = QStringListModel([ "item1" , "item2" , "item3" ])
self.listView.setModel( self.model )
self.layout = QGridLayout()
self.layout.addWidget(self.listView, 0 , 0 )
self.setLayout(self.layout)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
添加行:
应该可以为您解决问题。
QListView
继承QAbstractItemView
,它具有方法setEditTriggers()
。setEditTriggers
可能的值 > 可在文档中找到。Adding the line:
should fix things for you.
QListView
inheritsQAbstractItemView
which has the methodsetEditTriggers()
. Other possible values forsetEditTriggers
are available in the docs.感谢您的回复。我最终选择了 QListWidget,因为它默认不可编辑。
虽然我还发现如果您为 QListView 提供鼠标双击事件并将其设置为执行除编辑 QListView 之外的其他操作,它会覆盖编辑功能,因此也可以工作。
Thanks for the responses. I ended up going with a
QListWidget
instead as it is not editable by default.Though I also found if you give the
QListView
a mouse Double clicked event and set it to do something other than edit theQListView
, it overrides the edit function so that works too.如果
model
将附加到多个视图,并且您不希望其中任何一个视图都可以对其进行编辑,则可以子类化QStringListModel
并重写flags()< /code>:
现在用户将无法从任何视图编辑
模型
。If
model
will be attached to multiple views and you don't want it to be editable by any of them, you can subclassQStringListModel
and overrideflags()
:Now the user will not be able to edit
model
from any view.QStringListModel
根据定义是可编辑的。您应该子类化并提供适当的 标志QStringListModel
is by definition editable. You should subclass and provide the appropriate flags在 PySide6 中,
In PySide6,