我有3個QLineEdit小部件,它們是隻讀的,可以監聽關鍵事件。我想讓用戶選擇他要使用的QLineEdit (因為它将是一個快捷方式設置窗口)。在隻讀模式下,該窗口小部件不可單擊時出現。有沒有辦法讓它變得可點擊,或者在不使用readonly的情況下做同樣的事情?
我在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
您需要确定哪個lineedit在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_())
總結
以上是真正的電腦專家為你收集整理的專注于隻讀QLineEdit小部件的PyQt5的全部内容,希望文章能夠幫你解決所遇到的問題。
如果覺得真正的電腦專家網站内容還不錯,歡迎将真正的電腦專家推薦給好友。
有話要說...