gpt4 book ai didi

python - 如何获取具有焦点的小部件

转载 作者:行者123 更新时间:2023-12-01 00:58:11 25 4
gpt4 key购买 nike

如标题所示,我需要获取 child 的名字。但是,模板来自 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_())

最佳答案

如果你想获取具有焦点的小部件,你必须使用 QApplication 的 focusWidget 方法:

def pagedown(self):
self.focusNextPrevChild(True)
widget = QtWidgets.QApplication.focusWidget()
print(widget)

关于python - 如何获取具有焦点的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56065990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com