SingleApplication icon indicating copy to clipboard operation
SingleApplication copied to clipboard

QWidget: Cannot create a QWidget without QApplication

Open andr1972 opened this issue 3 years ago • 3 comments

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

andr1972 avatar Aug 13 '22 18:08 andr1972

Likely the same solution to #164 will also apply here.

On Sat, 13 Aug 2022 at 19:31, Andrzej Borucki @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/itay-grudev/SingleApplication/issues/165, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQGP55WU5G54E7BR6H5ZRLVY7SYFANCNFSM56OQVWIQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Kind Regards, Itay Grudev

@: @.*** in: https://www.linkedin.com/in/itaygrudev W: https://itay.grudev.com

itay-grudev avatar Aug 13 '22 23:08 itay-grudev

#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.

andr1972 avatar Aug 14 '22 05:08 andr1972

I just checked to be sure. QWidgets require QApplication instead of QGuiApplication, so that is what you need to specify in your macro. If I remember correctly, primaryScreen() is available in QApplication. That call to primary screen should be changed to QApplication instead the Gui alternative. Make sure you're consistent in your QApplication class usage and see if that solves your problem. I have an app with exactly the same setup and it works ok. There is no bug here.

On Sun, 14 Aug 2022 at 08:19, Andrzej Borucki @.***> wrote:

#164 https://github.com/itay-grudev/SingleApplication/issues/164 macro helps. #165 https://github.com/itay-grudev/SingleApplication/issues/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");

— Reply to this email directly, view it on GitHub https://github.com/itay-grudev/SingleApplication/issues/165#issuecomment-1214288050, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQGP563CS4IX3WIQKFYAGLVZB6WRANCNFSM56OQVWIQ . You are receiving this because you commented.Message ID: @.***>

-- Kind Regards, Itay Grudev

@: @.*** in: https://www.linkedin.com/in/itaygrudev W: https://itay.grudev.com

itay-grudev avatar Aug 14 '22 09:08 itay-grudev

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.

DonaldDuck313 avatar Aug 16 '22 10:08 DonaldDuck313