就像标题中一样,我需要输入孩子的名字。但是,模板来自Qt Creator,并且所有对象都在程序中创建。我不知道如何对它们进行子类化(因为我不应该更改生成的文件),所以调用后的所有对象都将返回self.sender()
。还有其他办法吗?
编辑:
我正在提供代码以重现该问题。这是生成的文件:
from PyQt5 import QtCore, QtGui, QtWidgets
class UiBazaDanych(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
self.gridLayout.setContentsMargins(11, 11, 11, 11)
self.gridLayout.setSpacing(6)
self.gridLayout.setObjectName("gridLayout")
self.comboBox = QtWidgets.QComboBox(self.centralWidget)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.gridLayout.addWidget(self.comboBox, 0, 2, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(self.centralWidget)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1)
self.dateEdit = QtWidgets.QDateEdit(self.centralWidget)
self.dateEdit.setObjectName("dateEdit")
self.gridLayout.addWidget(self.dateEdit, 1, 2, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralWidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 1, 1, 1, 1)
self.label = QtWidgets.QLabel(self.centralWidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralWidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 2, 1, 1)
self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.centralWidget)
self.doubleSpinBox.setButtonSymbols(QtWidgets.QAbstractSpinBox.NoButtons)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.gridLayout.addWidget(self.doubleSpinBox, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 21))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.comboBox.setItemText(0, _translate("MainWindow", "Agnieszka"))
self.comboBox.setItemText(1, _translate("MainWindow", "Monika"))
self.comboBox.setItemText(2, _translate("MainWindow", "Zenek"))
self.label_2.setText(_translate("MainWindow", "zl"))
self.label.setText(_translate("MainWindow", "Kasjer"))
self.pushButton.setText(_translate("MainWindow", "WYŚLIJ"))
而我的代码:
from PyQt5 import QtWidgets, QtCore, QtGui, QtPrintSupport
from Ui_Baza_Danych import UiBazaDanych
class InputWindow(QtWidgets.QMainWindow, UiBazaDanych):
def __init__(self):
super().__init__()
self.setupUi(self)
self.show()
QtWidgets.QShortcut(QtCore.Qt.Key_PageDown, self, self.pagedown)
def pagedown(self):
self.focusNextPrevChild(True)
# here should be a method to return selected child's name
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
show_all = InputWindow()
sys.exit(app.exec_())
最佳答案
如果要获取具有焦点的窗口小部件,则必须使用QApplication的focusWidget方法:
def pagedown(self):
self.focusNextPrevChild(True)
widget = QtWidgets.QApplication.focusWidget()
print(widget)
关于python - 如何获得具有焦点的小部件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56065990/