问题
What I'm trying to do is when a user (via a touchscreen) clicks on an editable QEditLine I want it to show the Matchbox-Keyboard for user input. When it is not clicked do not show the keyboard.
I've gone through the C documentation, and a few C examples, but I'm lost as too make the jump to Python. I see people mentioning setting the "focus" can someone explain this too me?
import sys
import os
from PyQt5.QtWidgets import QApplication, QFileDialog, QSlider, QComboBox, QCheckBox, QWidget, QMainWindow, QPushButton, QLabel, QGridLayout, QGroupBox, QRadioButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot, Qt
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'GUI TESTS'
self.left = 0
self.top = 0
self.width = 800
self.height = 400
self.statusBarMessage = "GUI TEST"
self.currentSprite = 'TEST.png'
self.btn1Active = False
self.btn2Active = False
self.btn3Active = False
self.btn4Active = False
self.btn5Active = False
self.btn6Active = False
self.btn7Active = False
self.btn8Active = False
self.saveLocationDir = ""
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar().showMessage(self.statusBarMessage)
self.userNameLabel = QLabel(self)
self.userNameLabel.move(0,125)
self.userNameLabel.setText("What is your name?")
self.userNameLabel.resize(120,20)
self.nameInput = QLineEdit(self)
self.nameInput.move(0,145)
self.nameInput.resize(200,32)
self.nameInput.setEchoMode(0)
@pyqtSlot()
def showKeyboard(self):
command = "matchbox-keyboard"
os.system(command)
回答1:
It is not recommended to override the events method by assigning a function self.nameInput.mousePressEvent = self.showKeyboard
since the tasks of the mousePressEvent
of the QLineEdit
are lost and could cause unexpected events.
Also, mousePressEvent
is not the appropriate event since you can press the QLineEdit
many times and it would be called back to the keyboard.
A better option is to launch it in focusInEvent
and delete it in focusOutEvent
:
import sys
import subprocess
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MatchBoxLineEdit(QLineEdit):
def focusInEvent(self, e):
try:
subprocess.Popen(["matchbox-keyboard"])
except FileNotFoundError:
pass
def focusOutEvent(self,e):
subprocess.Popen(["killall","matchbox-keyboard"])
class App(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('GUI TESTS')
widget = QWidget()
self.setCentralWidget(widget)
lay = QVBoxLayout(widget)
self.userNameLabel = QLabel("What is your name?")
self.nameInput = MatchBoxLineEdit()
lay.addWidget(self.userNameLabel)
lay.addWidget(self.nameInput)
self.setGeometry(
QStyle.alignedRect(
Qt.LeftToRight,
Qt.AlignCenter,self.sizeHint(),
qApp.desktop().availableGeometry()
)
)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = App()
w.show()
sys.exit(app.exec_())
回答2:
import sys
import os
from PyQt5.QtWidgets import QApplication, QFileDialog, QSlider, QComboBox, QCheckBox, QWidget, QMainWindow, QPushButton, QLabel, QGridLayout, QGroupBox, QRadioButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot, Qt
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'GUI TESTS'
self.left = 0
self.top = 0
self.width = 800
self.height = 400
self.statusBarMessage = "GUI TEST"
self.currentSprite = 'TEST.png'
self.btn1Active = False
self.btn2Active = False
self.btn3Active = False
self.btn4Active = False
self.btn5Active = False
self.btn6Active = False
self.btn7Active = False
self.btn8Active = False
self.saveLocationDir = ""
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.statusBar().showMessage(self.statusBarMessage)
self.userNameLabel = QLabel(self)
self.userNameLabel.move(0,125)
self.userNameLabel.setText("What is your name?")
self.userNameLabel.resize(120,20)
self.nameInput = QLineEdit(self)
self.nameInput.move(0,145)
self.nameInput.resize(200,32)
self.nameInput.setEchoMode(0)
self.nameInput.mousePressEvent=self.showKeyboard
@pyqtSlot()
def showKeyboard(self,event):
if event.button() == QtCore.Qt.LeftButton:
QtWidgets.QLineEdit.mousePressEvent(self, event)
command = "matchbox-keyboard"
os.system(command)
You can override mousePressEvent and achieve that functionality
来源:https://stackoverflow.com/questions/49306865/matchbox-keyboard-on-input-for-qlineedit-pyqt5