Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Return a function if arrow up key pressed



  • I've try to create an event to detect if the arrow up key was pressed but it don't work. Someone know whats wrong if the script?

    File 1

    import sys
    from PySide2 import QtCore, QtGui, QtWidgets
    from podedeletar import Ui_MainWindow
    
    class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            self.setupUi(self)
            
        
    if __name__ == "__main__":
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    

    File 2

    from PySide2.QtCore import *
    from PySide2.QtGui import *
    from PySide2.QtWidgets import *
    from PySide2 import QtCore, QtWidgets
    
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            if not MainWindow.objectName():
                MainWindow.setObjectName(u"MainWindow")
            MainWindow.resize(800, 600)
            self.centralwidget = QWidget(MainWindow)
            self.centralwidget.setObjectName(u"centralwidget")
            self.lineEdit = QLineEdit(self.centralwidget)
            self.lineEdit.setObjectName(u"lineEdit")
            self.lineEdit.setGeometry(QRect(330, 250, 113, 20))
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QMenuBar(MainWindow)
            self.menubar.setObjectName(u"menubar")
            self.menubar.setGeometry(QRect(0, 0, 800, 21))
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QStatusBar(MainWindow)
            self.statusbar.setObjectName(u"statusbar")
            MainWindow.setStatusBar(self.statusbar)
    
            self.retranslateUi(MainWindow)
    
            QMetaObject.connectSlotsByName(MainWindow)
    
            lay = QtWidgets.QVBoxLayout(self)
            lay.addWidget(self.lineEdit)
            lay.addStretch()
    
            self.lineEdit.installEventFilter(self)
    
        def eventFilter(self, obj, event):
            if obj is self.lineedit and event.type() == QtCore.QEvent.KeyPress:
                if event.key() == QtCore.Qt.Key_Up:
                    self.call_sc()
            return super().eventFilter(obj, event)
    
        def call_sc(self):
            print("working")
    
        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
    

  • Lifetime Qt Champion

    Hi,

    Ui_MainWindow is not a QObject nor a QWidget. You should not modify it as it is generated code.

    You should implement your logic in MainWindow.


Log in to reply