F : QWidget: Must construct a QApplication before a QWidget
-
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? -
@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.
-
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.cppIt 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. -
@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.
-
@Christian-Ehrlicher
Yes i agree but do you think its the reason for the "QApplication before a QWidget" ? -
@Christian-Ehrlicher should I remove the whole definition in Calculations.h? If so, it doesn't have any effect
-
@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.
-
@Christian-Ehrlicher
I have removed the definition from the header.
And removed static from QdesktopWidget dw in .cpp
And still nothing -
@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...
-
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
-
@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
-
@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?
-
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 :)
-
@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.
-
@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 ? -
@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.