0

I'm trying to implement keyPressEvent in a subclass which I have in another file than the .ui-file that is generated by the Qt designer, as I want to keep the design and logic separated from each other in my GUI. I tried doing something like what I found in this link (QtDesigner changes will be lost after redesign User Interface) , but don't understand how to implement it in the design file.

Design file:


class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        [removed non relevant parts in the setup]

        self.searchButton = QtWidgets.QPushButton(self.centralwidget)
        self.searchButton.setGeometry(QtCore.QRect(30, 85, 110, 33))
        self.searchButton.setObjectName("searchButton")
        self.searchBox = QtWidgets.QLineEdit(self.centralwidget)

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.searchButton.setText(_translate("MainWindow", "Search"))

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_())

Logic file:

class Logic(QWidget, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.setupUi(self)

    def keyPressEvent(self, event):
        print ("something was pressed")

How can I make it so that keyPressEvent function in the logic file will execute when a key is pressed while in the GUI?

  • use `class Logic(QMainWindow, Ui_MainWindow):` and `QMainWindow.__init__(self, *args, **kwargs)` – eyllanesc Jun 26 '20 at 15:07
  • keyPressEvent is executed when a keyboard key is pressed, not when a button is pressed. The one that changes `def keyPressEvent(self, event):` for `def on_clicked(self)` and adds `self.pushButton.clicked.connect(self.on_clicked)` after `self.setupUi(self)` – eyllanesc Jun 26 '20 at 15:10
  • Yes I know it is run when a keyboard key is pressed, I wrote wrong. But the question was how do I initialize the Logic subclass when I start my GUI? And one of the links you put as the answer is one I had already referenced in my question and say I didn't understand how to implement. So it doesn't help to simply link it once again because I still don't know how to get initialize the logic class from the Ui_Mainwindow. @eyllanesc – Yellow shirt Jun 26 '20 at 15:17
  • What I mean is how do I import the logic file into the design file and how do I create an instance of the logic class in the design class? @eyllanesc – Yellow shirt Jun 26 '20 at 15:21
  • 1) I link my duplicate because there is a table that indicates what type of widget you should use. – eyllanesc Jun 26 '20 at 15:23
  • 2) if you make the change indicated in my first comment it is enough, but the keyPressEvent method is only invoked when the window has the focus (I recommend you click on the empty space of the window) and just press – eyllanesc Jun 26 '20 at 15:30
  • Okay I see. But how do I initialize the logic file? If I have one file design.py and one file logic.py, then run design.py it will not run the logic.py file. So how to import the logic file into the design.py file and initialize the logic class in the Ui_MainWindow class? @eyllanesc – Yellow shirt Jun 26 '20 at 15:42
  • add the following to logic.py: `if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) w = Logic() w.show() sys.exit(app.exec_())` and run logic.py – eyllanesc Jun 26 '20 at 15:45
  • Thanks a lot this solved it! @eyllanesc – Yellow shirt Jun 26 '20 at 19:49

0 Answers0