Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QWidget: Cannot create a QWidget without QApplication #165

Closed
andr1972 opened this issue Aug 13, 2022 · 4 comments
Closed

QWidget: Cannot create a QWidget without QApplication #165

andr1972 opened this issue Aug 13, 2022 · 4 comments

Comments

@andr1972
Copy link

int main(int argc, char **argv)
{
    SingleApplication app(argc, argv);

    if( app.isSecondary() ) {
        QStringList arguments = app.arguments();
        app.sendMessage(  arguments.join(' ').toUtf8() );
        app.exit( 0 );
    }

    MainWindow mainWindow; //<--------------------------------HERE ERROR
    mainWindow.resize(800, 600);
    mainWindow.show();
    return app.exec();
}

Error "QWidget: Cannot create a QWidget without QApplication" is on creation MainWindow
I use after https://stackoverflow.com/questions/17937618/how-to-use-qtsingleapplication, but not QtSingleApplication but just SingleApplication

@itay-grudev
Copy link
Owner

itay-grudev commented Aug 13, 2022 via email

@andr1972
Copy link
Author

andr1972 commented Aug 14, 2022

#164 macro helps.
#165 in my Qt version 5.15.2 (or 5.15.3 in my Mint21 installed) still is error:
"QWidget: Cannot create a QWidget without QApplication"
In Qt sources in qwidget.cpp:

void QWidgetPrivate::init(QWidget *parentWidget, Qt::WindowFlags f)
{
    Q_Q(QWidget);
    Q_ASSERT_X(q != parentWidget, Q_FUNC_INFO, "Cannot parent a QWidget to itself");

    if (Q_UNLIKELY(!qobject_cast<QApplication *>(QCoreApplication::instance())))
        qFatal("QWidget: Cannot create a QWidget without QApplication");

But Calculator example works fine => maybe I have other invisible error.

@itay-grudev
Copy link
Owner

itay-grudev commented Aug 14, 2022 via email

@DonaldDuck313
Copy link

You need to set QAPPLICATION_CLASS to QApplication in your .pro file (not in main.cpp):

DEFINES += QAPPLICATION_CLASS=QApplication

I got this error because I defined it in main.cpp, which didn't set it in singleapplication.cpp. So even though SingleApplication inherited QApplication as it should in main.cpp, it inherited the default QCoreApplication instead in singleapplication.cpp where the constructor is defined, so the QApplication constructor was never called.

The solution to this is to define it in the .pro file instead, which defines it for the whole project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants