gpt4 book ai didi

python - 如何使 QListWidgetItem 不是三态?

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:31 26 4
gpt4 key购买 nike

我有一个带有 QListWidget 的表单,我在其中反复添加新项目。一切都完美无瑕,除了一件事:无论我传递给它们什么标志,这些项目都是三态的。因此,必须单击该项目两次才能选中/取消选中它们。我该怎么做才能让它们正常双态?

小部件是这样创建的:

def _locationDetails(self):
self.locationDetails = QListWidget()
self.locationDetails.setFixedHeight(50)
return self.locationDetails

最后添加的项目如下:

def addLocationDetail(self, text, checked = True):
item = QListWidgetItem(text)
item.setFlags(QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsSelectable |
QtCore.Qt.ItemIsEnabled)
item.setCheckState(checked)
self.locationDetails.addItem(item)

我调用添加新项目的代码如下:

    # resolve location:
waypoint.getLocationDetails()
self.locationDetails.clear()
self.addLocationDetail("location=%s" % waypoint.location)
self.addLocationDetail("department=%s" % waypoint.department)
self.addLocationDetail("country=%s" % waypoint.country)

最佳答案

该问题是由于 setCheckState() 引起的函数需要 Qt::CheckState 中的值枚举:

enum Qt::CheckState

This enum describes the state of checkable items, controls, and widgets.

Constant Value Description

Qt::Unchecked 0 The item is unchecked.

Qt::PartiallyChecked 1 The item is partially checked. Items in hierarchical models may be partially checked if some, but not all, of their children are checked.

Qt::Checked 2 The item is checked.

由于您默认向其传递 True 值,因此它会转换为 1 ,对应于 Qt::PartiallyChecked

一个可能的解决方案是使用 bool 值作为 Qt::CheckState 类型的适当值:

def addLocationDetail(self, text, checked=True):
item = QListWidgetItem(text)
item.setFlags(QtCore.Qt.ItemIsUserCheckable |
QtCore.Qt.ItemIsSelectable |
QtCore.Qt.ItemIsEnabled)
item.setCheckState(QtCore.Qt.Checked if checked else QtCore.Qt.Unchecked)
self.locationDetails.addItem(item)

关于python - 如何使 QListWidgetItem 不是三态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48400593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com