PyQt 中复选框的 ListView

发布于 2024-07-19 05:51:57 字数 145 浏览 4 评论 0原文

我想显示一个 QListView,其中每个项目都是带有一些标签的复选框。 复选框应始终可见。 我能想到的一种方法是使用自定义委托和 QAbstractListModel。 有没有更简单的方法? 您能提供执行此操作的最简单的片段吗?

提前致谢

I want to display a QListView where each item is a checkbox with some label. The checkboxes should be visible at all times. One way I can think of is using a custom delegate and QAbstractListModel. Are there simpler ways? Can you provide the simplest snippet that does this?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最终使用了 PyQt 邮件列表中 David Boddie 提供的方法。 以下是基于他的代码的工作片段:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from random import randint


app = QApplication(sys.argv)

model = QStandardItemModel()

for n in range(10):                   
    item = QStandardItem('Item %s' % randint(1, 100))
    check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked
    item.setCheckState(check)
    item.setCheckable(True)
    model.appendRow(item)


view = QListView()
view.setModel(model)

view.show()
app.exec_()

注意:将具有检查角色的 setData 调用更改为 setCheckState 并使用 setCheckable 而不是标志。

I ended up using the method provided by David Boddie in the PyQt mailing list. Here's a working snippet based on his code:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from random import randint


app = QApplication(sys.argv)

model = QStandardItemModel()

for n in range(10):                   
    item = QStandardItem('Item %s' % randint(1, 100))
    check = Qt.Checked if randint(0, 1) == 1 else Qt.Unchecked
    item.setCheckState(check)
    item.setCheckable(True)
    model.appendRow(item)


view = QListView()
view.setModel(model)

view.show()
app.exec_()

Note: changed the call of setData with a check role to setCheckState and used setCheckable instead of flags.

慕烟庭风 2024-07-26 05:51:57

如果您正在编写自己的模型,只需包含 Qt.ItemIsUserCheckable
flags() 方法的返回值中标记,并确保返回
来自 data() 方法的 Qt.CheckStateRole 的有效值。

如果您使用QStandardItemModel类,请包含Qt.ItemIsUserCheckable
在传递给每个项目的 setFlags() 方法的标记中,并设置检查
Qt.CheckStateRole 及其 setData() 方法的状态。

在交互式 Python 会话中,键入以下内容:

from PyQt4.QtGui import *

model = QStandardItemModel()
item = QStandardItem("Item")
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(QVariant(Qt.Checked), Qt.CheckStateRole)
model.appendRow(item)

view = QListView()
view.setModel(model)
view.show()

If you are writing your own model, just include the Qt.ItemIsUserCheckable
flag in the return value from the flags() method, and ensure that you return
a valid value for the Qt.CheckStateRole from the data() method.

If you use the QStandardItemModel class, include the Qt.ItemIsUserCheckable
flag in those you pass to each item's setFlags() method, and set the check
state for the Qt.CheckStateRole with its setData() method.

In an interactive Python session, type the following:

from PyQt4.QtGui import *

model = QStandardItemModel()
item = QStandardItem("Item")
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(QVariant(Qt.Checked), Qt.CheckStateRole)
model.appendRow(item)

view = QListView()
view.setModel(model)
view.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文