Prx001 / QSwitchControl

An easy-to-use and modern toggle switch for Qt Python binding PyQt (PyQt5) :package:
MIT License
16 stars 4 forks source link

Cant run App Error: QWidget: Must construct a QApplication before a QWidget #6

Open Proktologe opened 2 years ago

Proktologe commented 2 years ago

I currently start the programm like so:

class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

     def initUI(self):
        print("UI inited: MainWindow")
        self.SaveButton.clicked.connect(self.OnBttClicked_Save)
        switch_control = SwitchControl()
        hbox = QHBoxLayout()
        hbox.addWidget(switch_control, Qt.AlignCenter, Qt.AlignCenter)     
        self.setLayout(hbox)

if __name__ == "__main__":
    app = QApplication([])

    form_MainWindow = MainWindow() ###<-- in here a try to create the SwitchControl - like in your example
    form_MainWindow.show()

    sys.exit(app.exec())

Ui_MainWindow is a Window made in QT Designer:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
           if not MainWindow.objectName():
                   MainWindow.setObjectName(u"MainWindow")
           MainWindow.resize(823, 656)
           .
           . #Design code goes here
           .

I allways get the Exception: QWidget: Must construct a QApplication before a QWidget

Prx001 commented 2 years ago

Hey there Proktologe!

I checked your code and it runs well..

The exception you specified happens when you create QWidget objects before making a QApplication one.

You can send your project files to my email for further assistance.

Redoblue commented 3 months ago

@Proktologe You should use PyQt5, PySide series are not supported according to the codes. Or you can try to modify it, just rename some identifiers.