Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. F : QWidget: Must construct a QApplication before a QWidget
Forum Updated to NodeBB v4.3 + New Features

F : QWidget: Must construct a QApplication before a QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
qt-android
21 Posts 3 Posters 4.6k 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by mrjj
    #2

    Hi
    How does your main.cpp look like ?
    The code shown for mainwindow should not give that error as MainWin is normally created after
    QApplication in main.cpp

    It can also happen if you use global variables or using static but it should not say that for
    static QDesktopWidget dw;
    since it lives in Calculations and you first allocated that in mainWin.

    1 Reply Last reply
    1
    • R Offline
      R Offline
      rktech
      wrote on last edited by
      #3
      #include "mainwindow.h"
      #include <calculations.h>
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
          return a.exec();
      }
      

      Should the calculations.h be included after QApp?

      mrjjM 1 Reply Last reply
      0
      • R rktech
        #include "mainwindow.h"
        #include <calculations.h>
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
            return a.exec();
        }
        

        Should the calculations.h be included after QApp?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @rktech
        Nope, includes dont matter unless you used a global variable there.

        And you main looks fine.

        so not sure where it comes from.

        Did you at any time had some global variable so it could be compile left overs ?

        R 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @rktech said in F : QWidget: Must construct a QApplication before a QWidget:

          static QDesktopWidget dw;

          This is absolutely not needed at all. Neither static nor as value nor as member.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          mrjjM 1 Reply Last reply
          2
          • mrjjM mrjj

            @rktech
            Nope, includes dont matter unless you used a global variable there.

            And you main looks fine.

            so not sure where it comes from.

            Did you at any time had some global variable so it could be compile left overs ?

            R Offline
            R Offline
            rktech
            wrote on last edited by
            #6

            Now, it doesn't fall on Windows, but it still falls on Android.

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @rktech said in F : QWidget: Must construct a QApplication before a QWidget:

              static QDesktopWidget dw;

              This is absolutely not needed at all. Neither static nor as value nor as member.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #7

              @Christian-Ehrlicher
              Yes i agree but do you think its the reason for the "QApplication before a QWidget" ?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                rktech
                wrote on last edited by
                #8

                @Christian-Ehrlicher should I remove the whole definition in Calculations.h? If so, it doesn't have any effect

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @mrjj said in F : QWidget: Must construct a QApplication before a QWidget:

                  Yes i agree but do you think its the reason for the "QApplication before a QWidget" ?

                  It's static so it must be initialized somewhere which is executed before main, yes.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  R mrjjM 2 Replies Last reply
                  1
                  • Christian EhrlicherC Christian Ehrlicher

                    @mrjj said in F : QWidget: Must construct a QApplication before a QWidget:

                    Yes i agree but do you think its the reason for the "QApplication before a QWidget" ?

                    It's static so it must be initialized somewhere which is executed before main, yes.

                    R Offline
                    R Offline
                    rktech
                    wrote on last edited by rktech
                    #10

                    @Christian-Ehrlicher
                    I have removed the definition from the header.
                    And removed static from QdesktopWidget dw in .cpp
                    And still nothing

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @rktech said in F : QWidget: Must construct a QApplication before a QWidget:

                      And still nothing

                      Not a really useful error description...

                      Please show us your actual, minimal code to reproduce the issue. I would guess there are more statics around...

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        rktech
                        wrote on last edited by rktech
                        #12

                        Changed files from begining(note, running on android, on desktop it runs correctly):
                        Calculations.cpp

                        #include "calculations.h"
                        
                        Calculations::Calculations()
                        {
                        
                        }
                        int Calculations::Percent(int source, int percent){
                            return source*percent/100;
                        }
                        void Calculations::row(QPushButton *p, QProgressBar *b){
                        QDesktopWidget dw;
                        p->setGeometry(Percent(dw.width(),75),Percent(dw.height(),c),Percent(dw.width(),25),Percent(dw.height(),4));
                        b->setGeometry(0,Percent(dw.height(),c),Percent(dw.width(),75),Percent(dw.height(),4));
                        c+=4;
                        return;
                        }
                        
                        

                        Calculations.h

                        #ifndef CALCULATIONS_H
                        #define CALCULATIONS_H
                        #include <QApplication>
                        #include <QPushButton>
                        #include <QProgressBar>
                        #include <QDesktopWidget>
                        class Calculations
                        {
                        public:
                            Calculations();
                            static int Percent(int source, int percent);
                            void row(QPushButton *p, QProgressBar *b);
                        private:
                            int c = 28;
                           // QDesktopWidget dw;
                        };
                        
                        #endif // CALCULATIONS_H
                        
                        
                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #13

                          @rktech said in F : QWidget: Must construct a QApplication before a QWidget:

                          QDesktopWidget dw;

                          Please read the documentation...: https://doc.qt.io/qt-5/qdesktopwidget.html#obtaining-a-desktop-widget

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          R 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            @rktech said in F : QWidget: Must construct a QApplication before a QWidget:

                            QDesktopWidget dw;

                            Please read the documentation...: https://doc.qt.io/qt-5/qdesktopwidget.html#obtaining-a-desktop-widget

                            R Offline
                            R Offline
                            rktech
                            wrote on last edited by
                            #14

                            @Christian-Ehrlicher I have read it, but I can't figure how to do it better. I have only one screen on my phone. So how can I define the dw better?

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @mrjj said in F : QWidget: Must construct a QApplication before a QWidget:

                              Yes i agree but do you think its the reason for the "QApplication before a QWidget" ?

                              It's static so it must be initialized somewhere which is executed before main, yes.

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by mrjj
                              #15

                              Hi

                              It's static so it must be initialized somewhere which is executed before main, yes.

                              Ok. Im asking as the instance is first created in MainWindow and i cannot
                              reproduce it here using a static in a class which first is instantiated after QApp in main.

                              Im well aware that global/ global static is initilized before main but i did not know also it goes for
                              statics inside class that is created later than QApp.

                              Live and lean, i guess :)

                              Christian EhrlicherC R 2 Replies Last reply
                              0
                              • mrjjM mrjj

                                Hi

                                It's static so it must be initialized somewhere which is executed before main, yes.

                                Ok. Im asking as the instance is first created in MainWindow and i cannot
                                reproduce it here using a static in a class which first is instantiated after QApp in main.

                                Im well aware that global/ global static is initilized before main but i did not know also it goes for
                                statics inside class that is created later than QApp.

                                Live and lean, i guess :)

                                Christian EhrlicherC Online
                                Christian EhrlicherC Online
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #16

                                @mrjj I meant the static in the header in the first post - I wondered if it really linked (I would guess no since the static dw was not initialized anywhere) therefore my guess is there is more than we see here.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                1 Reply Last reply
                                1
                                • mrjjM mrjj

                                  Hi

                                  It's static so it must be initialized somewhere which is executed before main, yes.

                                  Ok. Im asking as the instance is first created in MainWindow and i cannot
                                  reproduce it here using a static in a class which first is instantiated after QApp in main.

                                  Im well aware that global/ global static is initilized before main but i did not know also it goes for
                                  statics inside class that is created later than QApp.

                                  Live and lean, i guess :)

                                  R Offline
                                  R Offline
                                  rktech
                                  wrote on last edited by
                                  #17

                                  @mrjj Anything that comes to me by this? I am sorry, I am a beginner.

                                  mrjjM 1 Reply Last reply
                                  0
                                  • R rktech

                                    @mrjj Anything that comes to me by this? I am sorry, I am a beginner.

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #18

                                    @rktech
                                    Sorry no new ideas.

                                    After you removed all use of static.
                                    then clean the build folder (delete all files)
                                    so we are sure its all recompiled.
                                    Then rebuild all.

                                    If you still get that warning then there must be a static or global var in some other file not shown.

                                    Also using the code shown, i dont get this warning on desktop.

                                    But you still get it on Android but not Desktop ?
                                    And you are 100% sure it was totallt recompiled after you removed static ?

                                    When you say "And removed static from QdesktopWidget dw in .cpp"
                                    that was for the one inside a function, correct ?
                                    Not something outside class or function ?

                                    1 Reply Last reply
                                    0
                                    • R Offline
                                      R Offline
                                      rktech
                                      wrote on last edited by
                                      #19

                                      Cleaned, remaked and now it worked

                                      mrjjM 1 Reply Last reply
                                      1
                                      • Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #20

                                        @rktech said in F : QWidget: Must construct a QApplication before a QWidget:

                                        Cleaned, remaked and now it worked

                                        Then please mark this topic as solved, thx.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        1 Reply Last reply
                                        1
                                        • R rktech

                                          Cleaned, remaked and now it worked

                                          mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #21

                                          @rktech
                                          Super. So it was the statics.
                                          So i also learned something new :)

                                          1 Reply Last reply
                                          0

                                          • Login

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