Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python separate Qt ui from signals

Tags:

python

pyqt

pyqt5

I want to start building UI applications with Qt Designer and Python3. I successfully designed a very basic window with a button and converted the .ui file into a .py file with a call of PyQt5.uic.pyuic.

Within the workflow, the design (.ui file) will likely be changing frequently. The .py output file states accordingly: 'WARNING! All changes made in this file will be lost!'

Fine. So I decided that I would import the .py file with the GUI definition into another python file and start adding the functionality there (signals and slots). Thus, GUI definition and functionality would be cleanly separated from one another.

But now I am stuck already since obviously one cannot simply define new signal/slot connections outside the class definition of the GUI.

I do attach my attempts on GUI and functionality (click -> Exit program) in the hope that someone can give me a hint. My search upon the topic reveald some more advanced multithreading related question but is far too complicated. This has to be much simpler?!

Thanks so much!

----> THE GUI DEFINITION (UI_simple.py) <----

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 111, 23))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Click to close!"))

----> THE (not working) FUNCTIONALITY <----

import UI_simple
from PyQt5 import QtWidgets, QtCore
import sys


app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = UI_simple.Ui_MainWindow()
ui.setupUi(MainWindow)

# This does NOT work...
# QtCore.QObject.connect(ui.pushButton, QtCore.SIGNAL("clicked()"),
#                        app, QtCore.SLOT("quit()"))

MainWindow.show()
sys.exit(app.exec_())
like image 819
Fab Avatar asked Oct 16 '25 22:10

Fab


1 Answers

Do not use the old connection syntax, if you are following a tutorial that uses it I recommend you look for another, on the other hand PyQt recommend creating a class that inherits from a widget and use the class provided by Qt Designer to fill it:

import UI_simple
from PyQt5 import QtWidgets, QtCore
import sys


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = UI_simple.Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(QtWidgets.QApplication.quit)

if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
like image 121
eyllanesc Avatar answered Oct 19 '25 13:10

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!