如何获取具有焦点的小部件
Posted
技术标签:
【中文标题】如何获取具有焦点的小部件【英文标题】:How to get the widget that has the focus 【发布时间】:2019-05-09 19:00:43 【问题描述】:如标题所示,我需要获取孩子的姓名。但是,模板来自 Qt Creator,所有对象都是在程序中创建的。我不知道如何对它们进行子类化(因为我不应该更改生成的文件),所以调用后的所有对象都将返回self.sender()
。有没有其他办法?
编辑:
我正在提供 mre 代码来重现该问题。这是生成的文件:
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_())
【问题讨论】:
【参考方案1】:如果你想得到有焦点的widget你必须使用QApplication的focusWidget方法:
def pagedown(self):
self.focusNextPrevChild(True)
widget = QtWidgets.QApplication.focusWidget()
print(widget)
【讨论】:
以上是关于如何获取具有焦点的小部件的主要内容,如果未能解决你的问题,请参考以下文章