Issue
I have subclassed a qtextedit in my code with typical subclasing but somehow even if my signals works fine, event.ignore wont work.Goal is to catch enter key and click a button but not commit enter on the qtextedit.
My code is here:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QAction
from PyQt5.QtGui import QPalette
from PyQt5.QtCore import QThread , pyqtSignal
class Text_Edit(QtWidgets.QTextEdit):
keyPressed = QtCore.pyqtSignal(QtCore.QEvent)
keyPressed2 = pyqtSignal()
def __init__(self, parent=None):
super(Text_Edit, self).__init__(parent)
self.setGeometry(QtCore.QRect(30, 100, 331, 71))
self.setStyleSheet("font: 10pt \"MS Shell Dlg 2\";")
self.raise_()
self.setFocus()
self.setPlaceholderText("Set 4 character ICAO Locator here , divided by spaces...")
self.keyPressed.connect(self.on_key)
def keyPressEvent(self,event):
super(Text_Edit, self).keyPressEvent(event)
self.keyPressed.emit(event)
def on_key(self, event):
if event.key() == QtCore.Qt.Key_Return or event.key() == QtCore.Qt.Key_Enter:
event.ignore()
self.keyPressed2.emit()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1371, 924)
MainWindow.setAcceptDrops(True)
self.T_aerodromes_input = Text_Edit(self.centralWidget)
self.T_aerodromes_input.keyPressed2.connect(self.B_aerodrome_data.click)
self.B_aerodrome_data = QtWidgets.QPushButton(self.centralWidget)
self.B_aerodrome_data.setGeometry(QtCore.QRect(670, 100, 271, 41))
self.B_aerodrome_data.setObjectName("B_aerodrome_data")
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_())
Solution
If you only want to filter the enter or return key then just verify that they are not those keys, if they are not those keys then call the father's method otherwise, do not do it.
def keyPressEvent(self,event):
if event.key() not in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
super(Text_Edit, self).keyPressEvent(event)
else:
self.keyPressed.emit(event)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.