【问题标题】:How can I get itemdata from qcombobox?如何从 qcombobox 获取 itemdata?
【发布时间】:2019-08-25 13:48:33
【问题描述】:

我在单击 QtWidgets.QPushButton 以显示 QtWidgets.QComboBox 中的 itemData 时遇到问题。我用这段代码填充我的 ComboBox:

self.comboBox.addItem("Sandro",1)
self.comboBox.addItem("Daniel",2)
self.comboBox.addItem("Pedro",3)

它填充了 QtWidgets.QComboBox,但是当我设置 QtWidgets.QPushButton 时出现问题。我在 setupUi 中添加了这个:

self.pushButton.clicked.connect(self.showId)

最后开发了函数showId:

id_us = self.comboBox.itemData(self.comboBox.currentIndex())
print('VAL ',id_us)

当我点击我的按钮时,窗口关闭了,这是什么问题? 我分享了我的 accessForm.py 的所有代码:

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(598, 245)
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setGeometry(QtCore.QRect(10, 20, 541, 201))
        self.groupBox.setObjectName("groupBox")
        self.pushButton = QtWidgets.QPushButton(self.groupBox)
        self.pushButton.setGeometry(QtCore.QRect(50, 150, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(40, 30, 47, 13))
        self.label.setObjectName("label")
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setGeometry(QtCore.QRect(110, 30, 111, 22))
        self.comboBox.setObjectName("comboBox")
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        self.fillCombo()
        self.pushButton.clicked.connect(self.showId)
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.groupBox.setTitle(_translate("Form", "Datos"))
        self.pushButton.setText(_translate("Form", "Inicio"))
        self.label.setText(_translate("Form", "TextLabel"))
    def showId(self):
        id_us = self.comboBox.itemData(self.comboBox.currentIndex()).toPyObject()
        print('VAL ',id_us)
    def fillCombo(self):
        self.comboBox.addItem("Sandro",1)
        self.comboBox.addItem("Daniel",2)
        self.comboBox.addItem("Pedro",3)
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

提前致谢。

【问题讨论】:

  • 当我在 Linux 上的控制台中运行它时,我得到AttributeError: 'int' object has no attribute 'toPyObject'。当我删除 .toPyObject() 时它可以正常工作
  • 哦,是的。你没事。我正在从 PyQt4 迁移并使用 toPyObject 运行。所以我在你的帮助下做到了,非常感谢。

标签: python python-3.x pyqt pyqt5 qcombobox


【解决方案1】:

在我看来,您使用的是过时的教程,在 PyQt4 中,您必须使用 toPyObject() 方法将 PyQt 对象转换为 python 本机对象,但在 PyQt5 中不再需要:

def showId(self):
    id_us = self.comboBox.itemData(self.comboBox.currentIndex()) # .toPyObject()
    print('VAL ',id_us)

作为建议,它使用终端或 CMD 来获取错误消息,因为 IDE 在处理 PyQt 错误消息方面存在问题。

【讨论】:

  • 非常感谢。是的,我正在从 pyqt4 迁移
  • @GSandro_Strongs 在 PyQt5 中是最简单易读的东西,因为 PyQt 使用 python 的原生类型
猜你喜欢
  • 1970-01-01
  • 2011-01-04
  • 2013-08-27
  • 1970-01-01
  • 2015-11-11
  • 1970-01-01
  • 2019-07-08
  • 2021-05-31
  • 2013-09-17
相关资源
最近更新 更多