Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap"
Forum Updated to NodeBB v4.3 + New Features

How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap"

Scheduled Pinned Locked Moved Unsolved 3rd Party Software
11 Posts 4 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • laozeng1982L laozeng1982

    Hi experts,

    I am using some 3rd part dlls, wll I start the module which using these dlls, it say "QPixmap: Must construct a QGuiApplication before a QPixmap" and the program crashed.

    Enviorment:
    OS: Widows 11.
    Qt version: 5.15.2
    Complier: Qt MSVC 2019 x64

    I did some digging and reading https://forum.qt.io/topic/130352/how-stop-the-qpixmap-must-construct-a-qguiapplication-before-a-qpixmap-in-a-dll, I will provide some information I think it might be useful.

    main.cpp

    #include <QApplication>
    #include <QDesktopServices>
    #include <QGuiApplication>
    #include <QDesktopWidget>
    #include <QScreen>
    #include <QTextCodec>
    #include <QDebug>
    #include <DesignAndDebugger.h>
    #include <QLibrary>
    
    // this is application I worte
    #include "EnhancedTools.h"
    
    int main(int argc, char* argv[])
    {
        // for function test!
        bool design_and_test = false;
        //    bool design_and_test = true;
    
        if(design_and_test) {
            DesignAndDebugger::runTest();
            return 0;
        }
    
        QTextCodec* codec = QTextCodec::codecForName("UTF-8");
        QTextCodec::setCodecForLocale(codec);
    
        qDebug() << "Main application, before qApp:" << QCoreApplication::instance();
    
        QApplication app(argc, argv);
    
        qDebug() << "Main application, after qApp:" << QCoreApplication::instance();
        qDebug() << "DLL, before loading icons:" << QCoreApplication::instance();
        
        // for test load libray, with or without it, same error
        QLibrary mylib("CarnacPlotQt5.dll");
        if(mylib.load()) {
            qDebug() << "CarnacPlotQt5.dll loaded";
        }
    
        qDebug() << "DLL, after loading icons:" << QCoreApplication::instance();
    
        EnhancedTools window;
        // get the screen number where window create.
        int currentScreen = app.desktop()->screenNumber(&window);
    
        // get the geometry of the screen
        QRect rect = QGuiApplication::screens().at(currentScreen)->geometry();
    
        // move to the center and top
        window.move((rect.width() - window.width()) / 2, 0);
    
        window.show();
        return app.exec();
    }
    

    EnhancedTools.pro, these are 3rd part libs I used.

    INCLUDEPATH += C:/INT/Carnac/include
    DEPENDPATH += C:/INT/Carnac/include
    
    INCLUDEPATH += C:/INT/Carnac/src
    
    LIBS += -LC:/INT/Carnac/lib/ -lCarnac
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacCgm
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacChartQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacContour
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacContourChartQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacContourQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacDeviatedWellQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacDriverQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacGauges
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacGaugesQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacMapQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacMultiWellQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacPlot
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacPlotQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacPluginQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacPs
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacSeisLogQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacSeismic
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacSeismicQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacStylesQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacW32
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellBoreDataQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellLog
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellLogQt5
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellSchematic
    LIBS += -LC:/INT/Carnac/lib/ -lCarnacWellSchematicQt5
    

    I use debug model and get message like.

    85101d0d-acbd-4695-a3e2-f35ada4a04a4-image.png

    console output:
    e255521a-7d98-47df-b03b-234ead86a72d-image.png

    Any help will be appreciated!

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @laozeng1982 said in How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap":

    QLibrary mylib("CarnacPlotQt5.dll");
    if(mylib.load())

    Why are you doing this?!
    Your application already links against this DLL, there is no need to dynamically load it...

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    laozeng1982L 1 Reply Last reply
    2
    • jsulmJ jsulm

      @laozeng1982 said in How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap":

      QLibrary mylib("CarnacPlotQt5.dll");
      if(mylib.load())

      Why are you doing this?!
      Your application already links against this DLL, there is no need to dynamically load it...

      laozeng1982L Offline
      laozeng1982L Offline
      laozeng1982
      wrote on last edited by
      #3

      @jsulm Yes, I just test it, but without this test, it still raise the same error.

      J.HilkJ 1 Reply Last reply
      0
      • laozeng1982L laozeng1982

        @jsulm Yes, I just test it, but without this test, it still raise the same error.

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #4

        @laozeng1982 assuming you don't have any static QPixmaps or so,

        move QApplication app(argc, argv); to the top of your main function


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • laozeng1982L Offline
          laozeng1982L Offline
          laozeng1982
          wrote on last edited by
          #5

          @J-Hilk Hi, I move the "QApplication app(argc, argv);" to the top and remove QLibrary statement.

          but It still the same error.

          J.HilkJ 1 Reply Last reply
          0
          • laozeng1982L laozeng1982

            @J-Hilk Hi, I move the "QApplication app(argc, argv);" to the top and remove QLibrary statement.

            but It still the same error.

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #6

            @laozeng1982 than you have probably somewhere a static QObject


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            laozeng1982L JonBJ 2 Replies Last reply
            1
            • J.HilkJ J.Hilk

              @laozeng1982 than you have probably somewhere a static QObject

              laozeng1982L Offline
              laozeng1982L Offline
              laozeng1982
              wrote on last edited by
              #7

              @J-Hilk So, how Can I found where is the static Object?

              If I don't start the module using the 3rd dlls, it will not raise the error, according to the debugger information, it was in the 3rd part dlls, even I found it, I can't modify it.

              J.HilkJ 1 Reply Last reply
              0
              • laozeng1982L laozeng1982

                @J-Hilk So, how Can I found where is the static Object?

                If I don't start the module using the 3rd dlls, it will not raise the error, according to the debugger information, it was in the 3rd part dlls, even I found it, I can't modify it.

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #8

                @laozeng1982 its a dll, it's dynamically loaded -> load it after your QApplication instance is up and running.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                laozeng1982L 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @laozeng1982 its a dll, it's dynamically loaded -> load it after your QApplication instance is up and running.

                  laozeng1982L Offline
                  laozeng1982L Offline
                  laozeng1982
                  wrote on last edited by
                  #9

                  @J-Hilk Yes, I want to do this, but I am not familiar with QT, I don't know how to do this.

                  Second thing, do I need to modify my .pro file and remove static libs?

                  1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @laozeng1982 than you have probably somewhere a static QObject

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #10

                    @J-Hilk said in How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap":

                    @laozeng1982 than you have probably somewhere a static QObject

                    That would have been my initial comment too. But when I looked at the first post in this thread I see

                    • A stack trace showing me code called from some action/checkbox; and
                    • Console output showing code running as far as the app.exec().

                    Assuming the OP means the error message about "no application" comes at this point how can it stem from a static QObject? That would happen literally at start up.

                    J.HilkJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @J-Hilk said in How to deal with "QPixmap: Must construct a QGuiApplication before a QPixmap":

                      @laozeng1982 than you have probably somewhere a static QObject

                      That would have been my initial comment too. But when I looked at the first post in this thread I see

                      • A stack trace showing me code called from some action/checkbox; and
                      • Console output showing code running as far as the app.exec().

                      Assuming the OP means the error message about "no application" comes at this point how can it stem from a static QObject? That would happen literally at start up.

                      J.HilkJ Online
                      J.HilkJ Online
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #11

                      @JonB you are correct, maybe its in a different thread

                      very strange everything hard to tell from afar


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved