1. <legend id='ydoOI'><style id='ydoOI'><dir id='ydoOI'><q id='ydoOI'></q></dir></style></legend>

    <i id='ydoOI'><tr id='ydoOI'><dt id='ydoOI'><q id='ydoOI'><span id='ydoOI'><b id='ydoOI'><form id='ydoOI'><ins id='ydoOI'></ins><ul id='ydoOI'></ul><sub id='ydoOI'></sub></form><legend id='ydoOI'></legend><bdo id='ydoOI'><pre id='ydoOI'><center id='ydoOI'></center></pre></bdo></b><th id='ydoOI'></th></span></q></dt></tr></i><div id='ydoOI'><tfoot id='ydoOI'></tfoot><dl id='ydoOI'><fieldset id='ydoOI'></fieldset></dl></div>

  2. <small id='ydoOI'></small><noframes id='ydoOI'>

      <tfoot id='ydoOI'></tfoot>
        <bdo id='ydoOI'></bdo><ul id='ydoOI'></ul>

    1. 如何从 qcombobox 获取 itemdata?

      时间:2023-08-04

        • <bdo id='1zMgi'></bdo><ul id='1zMgi'></ul>
        • <i id='1zMgi'><tr id='1zMgi'><dt id='1zMgi'><q id='1zMgi'><span id='1zMgi'><b id='1zMgi'><form id='1zMgi'><ins id='1zMgi'></ins><ul id='1zMgi'></ul><sub id='1zMgi'></sub></form><legend id='1zMgi'></legend><bdo id='1zMgi'><pre id='1zMgi'><center id='1zMgi'></center></pre></bdo></b><th id='1zMgi'></th></span></q></dt></tr></i><div id='1zMgi'><tfoot id='1zMgi'></tfoot><dl id='1zMgi'><fieldset id='1zMgi'></fieldset></dl></div>
          1. <tfoot id='1zMgi'></tfoot>

            <small id='1zMgi'></small><noframes id='1zMgi'>

              <legend id='1zMgi'><style id='1zMgi'><dir id='1zMgi'><q id='1zMgi'></q></dir></style></legend>
                <tbody id='1zMgi'></tbody>

                本文介绍了如何从 qcombobox 获取 itemdata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                I have a problem when I clicked in a QtWidgets.QPushButton to show the itemData from a QtWidgets.QComboBox. I fill my ComboBox with this code:

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

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

                It filled the QtWidgets.QComboBox, however the problem appears when I set the QtWidgets.QPushButton. I added this in the setupUi:

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

                最后开发了函数showId:

                And finally developed the function showId:

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

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

                When I clicked my button, the windows closes, What was the problem? I share all the code of my 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_())
                

                提前致谢.

                推荐答案

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

                It seems to me that you are using an outdated tutorial, in PyQt4 you had to convert the PyQt object to python native object using the toPyObject() method, but in PyQt5 it is no longer necessary:

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

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

                As a recommendation, it uses the terminal or CMD to obtain the error messages since the IDEs have problems with handling the PyQt error messages.

                这篇关于如何从 qcombobox 获取 itemdata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:带有 FlowLayout 小部件的 QScrollArea 未正确调整大小 下一篇:如何获取 QTableView 右键单击​​索引

                相关文章

                最新文章

              • <legend id='jMRMK'><style id='jMRMK'><dir id='jMRMK'><q id='jMRMK'></q></dir></style></legend>
                <i id='jMRMK'><tr id='jMRMK'><dt id='jMRMK'><q id='jMRMK'><span id='jMRMK'><b id='jMRMK'><form id='jMRMK'><ins id='jMRMK'></ins><ul id='jMRMK'></ul><sub id='jMRMK'></sub></form><legend id='jMRMK'></legend><bdo id='jMRMK'><pre id='jMRMK'><center id='jMRMK'></center></pre></bdo></b><th id='jMRMK'></th></span></q></dt></tr></i><div id='jMRMK'><tfoot id='jMRMK'></tfoot><dl id='jMRMK'><fieldset id='jMRMK'></fieldset></dl></div>

                  <small id='jMRMK'></small><noframes id='jMRMK'>

                  1. <tfoot id='jMRMK'></tfoot>

                      <bdo id='jMRMK'></bdo><ul id='jMRMK'></ul>