0

I created a ui window using Qt Designer and converted it to python file.

the result of conversion from .ui to .py:

from PyQt6 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1211, 700)
        MainWindow.setMinimumSize(QtCore.QSize(1211, 700))
        MainWindow.setMaximumSize(QtCore.QSize(1211, 700))
        self.Title = QtWidgets.QLabel(parent=MainWindow)
        self.Title.setGeometry(QtCore.QRect(0, 10, 1211, 41))

        self.uploadCerts = QtWidgets.QPushButton(parent=MainWindow)
        self.uploadCerts.setGeometry(QtCore.QRect(1060, 90, 131, 24))
        self.uploadCerts.setObjectName("uploadCerts")
        self.uploadCerts.setText("Upload no Certificates")

        self.retranslateUi(MainWindow)
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Form"))
        self.Title.setText(_translate("MainWindow", "Main window"))
        self.uploadCerts.setText(_translate("MainWindow", "Upload Certificates"))
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QWidget()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec())

now I want to make a controller class to this View and make changes to the View using the controller.

the controller I've created:

from PyQt6 import QtWidgets
import sys
import MainWindow

class MainWindowController(MainWindow.Ui_MainWindow):
    def __init__(self):
        super().__init__(MainWindow.Ui_MainWindow)
        self.setupUi(MainApp)
        self.uploadCerts.clicked.connect(self.test)

    def test(self):
         print("Hello")
         self.workingPath.setText("Hello")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainApp = QtWidgets.QWidget()
    ui = MainWindowController()
    ui.setupUi(MainApp)
    MainApp.show()
    sys.exit(app.exec())

and this is not working however when I try to setText() to a label it works fine but not working for any input component.

Xender
  • 1
  • Don't subclass the Ui class alone. Subclass from QWidget *and* the Ui class, then in the `if __name__` just create an instance of your subclass. See the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html). Your issue is caused by the fact that you're practically calling `setupUi()` twice, so you're *recreating* the Ui one more once, but the connection was created with the *first* occurrence, and that's one that gets changed, only that's not the one you actually see. – musicamante Jul 31 '23 at 10:37
  • I removed ui.setupUi(MainApp) from the if __name__ in the controller and it worked. Thanks alot – Xender Jul 31 '23 at 16:10
  • Ok. Note that subclassing the Ui classes alone is discouraged (as it is editing pyuic files), and it practically doesn't have any benefit. Proper subclassing in Qt happens by inheriting from the base QtWidget type (QWidget, QMainWindow, etc), since it provides a correct object type and also allows using specific Qt features, like signals, properties and slots, which can only be used with QObject based classes. – musicamante Jul 31 '23 at 17:16

0 Answers0