問題描述
我在單擊 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 時出現(xiàn)問題.我在 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)
最后開發(fā)了函數(shù)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 對象轉(zhuǎn)換為 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模板網(wǎng)!