R
R
r1dddy4sv2021-09-04 16:11:00
PyQt
r1dddy4sv, 2021-09-04 16:11:00

QWidget: Must construct a QApplication before a QWidget?

main file:

import login
dialog = login.login_dialog()
dialog.exec()

login file:
from PyQt5 import QtCore, QtGui, QtWidgets

class login_dialog(QtWidgets.QDialog):
    def __init__(self,parent=None):
        super(login_dialog, self).__init__(parent)
        self.setObjectName("Dialog")
        self.resize(400, 300)
        #и так далее


I get an error:
QWidget: Must construct a QApplication before a QWidget

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-04
@r1dddy4sv

Well, everything is written correctly. You are not creating an instance of the QApplication class anywhere, to which all widgets should cling.
Google hello world on PyQT5 , it would immediately become clear.

from PyQt5.QtWidgets import QApplication, QLabel
app = QApplication([])

# This is a requirement of Qt: Every GUI app must have exactly one instance of QApplication. 
# Many parts of Qt don't work until you have executed the above line. 
# You will therefore need it in virtually every (Py)Qt app you write.
# The brackets [] in the above line represent the command line arguments passed to the application. 
# Because our app doesn't use any parameters, we leave the brackets empty.
label = QLabel('Hello World!')
label.show()
app.exec()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question