我有3个QLineEdit小部件,这些小部件是只读的,并且可以监听按键事件。好吧,我想让用户选择他将要使用的QLineEdit(因为它将是一个快捷方式设置窗口)。在只读模式下,该窗口小部件不可单击时显示。有没有一种方法可以使它可点击或在没有只读的情况下做同样的事情?
我在Qt Designer中编写了代码。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent1
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent2
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent3
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent1(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit.setText(str(keyname))
def newOnkeyPressEvent2(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_2.setText(str(keyname))
def newOnkeyPressEvent3(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
self.lineEdit_3.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您需要用self.centralwidget.focusWidget()
确定哪个行编辑受到关注。这是完整的工作代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(392, 290)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(140, 70, 113, 21))
self.lineEdit.setReadOnly(True)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
MainWindow.keyPressEvent = self.newOnkeyPressEvent
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(140, 150, 113, 21))
self.lineEdit_2.setReadOnly(True)
self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(140, 220, 113, 21))
self.lineEdit_3.setReadOnly(True)
self.lineEdit_3.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit_3.setObjectName("lineEdit_3")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def newOnkeyPressEvent(self, event):
key = event.key()
modifiers = int(event.modifiers())
keyname = QtGui.QKeySequence(modifiers + key).toString()
print(keyname)
lineEdit = self.centralwidget.focusWidget()
lineEdit.setText(str(keyname))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())