gpt4 book ai didi

python - 如何从多选 QComboBox 中获取选中的项目?

转载 作者:太空宇宙 更新时间:2023-11-03 21:16:07 24 4
gpt4 key购买 nike

我能够使用 QComboBox 和 QStandardItemModel 实现多选,但无法使用 selectedItems() 或 checkItems() 选项获取所选项目,该选项给出的错误消息显示为:

AttributeError: 'QComboBox' object has no attribute 'selectedItems'               
AttributeError: 'QComboBox' object has no attribute 'checkedItems'

有没有办法获取 QComboBox 的选定项目列表?下面是我的代码的示例实现

class CheckableComboBox(QComboBox):
def __init__(self):
super(CheckableComboBox, self).__init__()
self.view().pressed.connect(self.handleItemPressed)
self.setModel(QtGui.QStandardItemModel(self))

def handleItemPressed(self, index):
item = self.model().itemFromIndex(index)
if item.checkState() == QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Unchecked)
else:
item.setCheckState(QtCore.Qt.Checked)

class Dialog_01(QMainWindow):
def __init__(self):
super(QMainWindow,self).__init__()
years = ["2017", "2018", "2019", "2020"]
self.comboBoxv = QComboBox(self)
self.model = QtGui.QStandardItemModel(self)
firstItem = QtGui.QStandardItem("Select Years")
firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
firstItem.setSelectable(False)
self.model.setItem(0, 0, firstItem)
for i,area in enumerate(years):
item = QtGui.QStandardItem(area)
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
item.setData(Qt.Unchecked, Qt.CheckStateRole)
self.model.setItem(i+1, 0, item)
self.comboBoxv.setModel(self.model)
self.comboBoxv.installEventFilter(self)

if __name__ == '__main__':
app = QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

最佳答案

checkedItems 方法不存在,因此您会收到该错误。解决方案是迭代模型并验证是否已检查。

from PyQt5 import QtCore, QtGui, QtWidgets

class CheckableComboBox(QtWidgets.QComboBox):
closedPopup = QtCore.pyqtSignal()

def __init__(self, parent=None):
super(CheckableComboBox, self).__init__(parent)
self.setView(QtWidgets.QListView(self))
self.view().pressed.connect(self.handleItemPressed)
self.setModel(QtGui.QStandardItemModel(self))

firstItem = QtGui.QStandardItem("Select Years")
firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
firstItem.setSelectable(False)
self.model().appendRow(firstItem)

@QtCore.pyqtSlot(QtCore.QModelIndex)
def handleItemPressed(self, index):
item = self.model().itemFromIndex(index)
if item.checkState() == QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Unchecked)
else:
item.setCheckState(QtCore.Qt.Checked)

def checkedItems(self):
l = []
for i in range(self.model().rowCount()):
it = self.model().item(i)
if it.checkState() == QtCore.Qt.Checked:
l.append(it.text())
return l

def hidePopup(self):
self.closedPopup.emit()
super(CheckableComboBox, self).hidePopup()
QtCore.QTimer.singleShot(0, lambda: self.setCurrentIndex(0))

class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super(Dialog_01,self).__init__()
years = ["2017", "2018", "2019", "2020"]
self.comboBoxv = CheckableComboBox()
self.comboBoxv.closedPopup.connect(self.on_closedPopup)
for i, area in enumerate(years):
item = QtGui.QStandardItem(area)
item.setCheckable(True)
item.setCheckState(QtCore.Qt.Unchecked)
self.comboBoxv.model().appendRow(item)

central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(self.comboBoxv)
lay.addStretch()

@QtCore.pyqtSlot()
def on_closedPopup(self):
print(self.comboBoxv.checkedItems())

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())

关于python - 如何从多选 QComboBox 中获取选中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54699119/

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