[sovled]QT - QWidget: Must construct a QApplication before a QWidget

Mar 1, 2022 at 12:28am
In trying to make a QLabel a static member of a class I am getting the message:
QWidget: Must construct a QApplication before a QWidget

after which the program forcefully exits.

this is the relevant portion of the header file (class MainWindowII)
1
2
3
4
5
6
7
public:
    explicit MainWindowII(QWidget *parent = nullptr);
    ~MainWindowII();

    void setGame(Game *g);
    Game *_pGame;                               // pointer to the game object
    static QLabel *titleLabel; 

and this is the implementation mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "mainwindowii.h"
#include "ui_mainwindowii.h"
#include "game.h"
#include <QDebug>
//#include <QLabel>

QLabel * MainWindowII::titleLabel = new QLabel(); // this is the line that gives me trouble 

MainWindowII::MainWindowII(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowII)
{
    ui->setupUi(this);
    move(QGuiApplication::screens().at(0)->geometry().center() - frameGeometry().center());
//    MainWindowII::titleLabel->setParent(this);
    //MainWindowII::titleLabel->setParent(this);
//    QLabel *l = MainWindowII::titleLabel;
    //QLabel *l = new QLabel(this);
//    QPixmap pixmap(":/images/title2.png");
//    l->setPixmap(pixmap);
//    l->setAlignment(Qt::AlignCenter);
//    QVBoxLayout * bl = new QVBoxLayout(ui->centralwidget);
//    bl->addWidget(l);
    //MainWindowII::titleLabel = l;
    initTitle();
}

Last edited on Mar 1, 2022 at 1:22am
Mar 1, 2022 at 1:12am
Global objects are created before main runs, but the QApplication is constucted in main and you're not allowed to create Qt objects without it being previously constructed.

So change the titleLabel definition to:
 
QLabel * MainWindowII::titleLabel;

And add this to the MainWindowII constructor:
 
titleLabel = new QLabel();

Last edited on Mar 1, 2022 at 1:13am
Mar 1, 2022 at 1:19am
Also, making a widget (e.g. QLabel) a static member is asking for trouble. Multiple instances (objects) of your MainWindowII class could be created, even if you do not usually expect this. Then all instances would be sharing the same static widget. But you cannot have the same widget in multiple places! If there is only one instance of it, then it lives in only one single location! Why not make it a "normal" non-static member ???
Last edited on Mar 1, 2022 at 1:24am
Mar 1, 2022 at 1:22am
Ok, perfect that worked.
Thank you very much.
Mar 1, 2022 at 1:40am
DizzyDon wrote:
And add this to the MainWindowII constructor:
titleLabel = new QLabel();

And don't forget to add this to the destructor of your class, or you'll get a memory leak!

1
2
3
4
MainWindowII::~MainWindowII
{
    delete titleLabel;
}


Either that, or even better use a QScopedPointer like this, for automatic memory clean-up:

QScopedPointer<QLabel> MainWindowII::titleLabel;

1
2
3
4
5
6
MainWindowII::MainWindowII(QWidget *parent) :
    /*...*/
    titleLabel(new QLabel())
{
    /*... */
}

Last edited on Mar 1, 2022 at 1:47am
Topic archived. No new replies allowed.