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.
  • R Offline
    R Offline
    rktech
    wrote on 25 Mar 2020, 14:51 last edited by rktech
    #1

    I am trying to create an android app(it is not better on pc) with qt, and when I run it, I am getting this error in run console:

    F         : QWidget: Must construct a QApplication before a QWidget
    

    Apparently the problem is, that I use QDesktopWidget in My Calculations class.
    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;
        static QDesktopWidget dw;
    };
    
    #endif // CALCULATIONS_H
    

    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){
    static 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;
    }
    
    

    MainWindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QDesktopWidget dw;
        dw.width();
        //   ui->Clickme->setGeometry(Calculations::Percent(dw.width(),15),dw.height()/8,Calculations::Percent(dw.width(),70),dw.height()/8);
        Calculations c;
        ui->Clickme->setGeometry(c.Percent(dw.width(),15),dw.height()/8,c.Percent(dw.width(),70),dw.height()/8);
        c.row(ui->pushButton,ui->progressBar);
        c.Percent(100,12);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    Calculations.h is included in MainWindow.h
    Can someone help me?

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 25 Mar 2020, 15:13 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

      M 1 Reply Last reply 25 Mar 2020, 15:14
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 25 Mar 2020, 14:56 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 25 Mar 2020, 15:02 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?

          M 1 Reply Last reply 25 Mar 2020, 15:05
          0
          • R rktech
            25 Mar 2020, 15:02
            #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?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 25 Mar 2020, 15:05 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 25 Mar 2020, 15:13
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 25 Mar 2020, 15:13 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

              M 1 Reply Last reply 25 Mar 2020, 15:14
              2
              • M mrjj
                25 Mar 2020, 15:05

                @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 25 Mar 2020, 15:13 last edited by
                #6

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

                1 Reply Last reply
                0
                • C Christian Ehrlicher
                  25 Mar 2020, 15:13

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 25 Mar 2020, 15:14 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 25 Mar 2020, 15:25 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
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 25 Mar 2020, 15:28 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 M 2 Replies Last reply 25 Mar 2020, 15:39
                      1
                      • C Christian Ehrlicher
                        25 Mar 2020, 15:28

                        @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 25 Mar 2020, 15:39 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
                        • C Online
                          C Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 25 Mar 2020, 15:46 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 25 Mar 2020, 15:51 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
                            • C Online
                              C Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 25 Mar 2020, 15:54 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 25 Mar 2020, 16:02
                              1
                              • C Christian Ehrlicher
                                25 Mar 2020, 15:54

                                @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 25 Mar 2020, 16:02 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
                                • C Christian Ehrlicher
                                  25 Mar 2020, 15:28

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

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 25 Mar 2020, 16:03 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 :)

                                  C R 2 Replies Last reply 25 Mar 2020, 16:06
                                  0
                                  • M mrjj
                                    25 Mar 2020, 16:03

                                    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 :)

                                    C Online
                                    C Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 25 Mar 2020, 16:06 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
                                    • M mrjj
                                      25 Mar 2020, 16:03

                                      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 25 Mar 2020, 16:13 last edited by
                                      #17

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

                                      M 1 Reply Last reply 25 Mar 2020, 16:21
                                      0
                                      • R rktech
                                        25 Mar 2020, 16:13

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

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 25 Mar 2020, 16:21 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 25 Mar 2020, 16:37 last edited by
                                          #19

                                          Cleaned, remaked and now it worked

                                          M 1 Reply Last reply 25 Mar 2020, 16:50
                                          1
                                          • C Online
                                            C Online
                                            Christian Ehrlicher
                                            Lifetime Qt Champion
                                            wrote on 25 Mar 2020, 16:38 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

                                            1/21

                                            25 Mar 2020, 14:51

                                            • Login

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