Vgambale
Vgambale

Reputation: 1

QT declare Mainwindow out of the main class

I have this two class in c++

GUI.cpp

#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"

GUI::GUI() {
}

void GUI::startGUI(){
    int c=1;
    char *array[10];
    char** v = &array[0];
    QApplication qa(c,v);
    w.show();
    qa.exec();
}
void GUI::notifyAlert(){

}

GUI::~GUI() {
    // TODO Auto-generated destructor stub
}

GUI.h

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "mainwindow.h"

#ifndef GUI_H_
#define GUI_H_

class GUI {
public:
    GUI();
    virtual ~GUI();
    void startGUI();
    void notifyAlert();

private:
    MainWindow w;
};

#endif 

But when i run this program i have the error: QWidget: Must construct a QApplication before a QPaintDevice

How can I declare MainWindow w in gui.h in such a way that I don't receive this error

Upvotes: 0

Views: 566

Answers (2)

drescherjm
drescherjm

Reputation: 10827

I would have made w a pointer used a forward declaration for MainWindow and removed all the includes (including the 2 includes for mainwindow.h) from GUI.h. Then like the answer from Sebastian says construct the QApplication first.

AL_GUI.h

#ifndef GUI_H_
#define GUI_H_

class MainWindow;

class GUI {
public:
    GUI();
    virtual ~GUI();
    void startGUI();
    void notifyAlert();

private:
    MainWindow* w;
};

gui.cpp

#include "AL_GUI.h"
#include <QtGui/QApplication>
#include "mainwindow.h"

GUI::GUI()  : w(NULL)
{
}

void GUI::startGUI(){
    int c=1;
    char *array[10];
    char** v = &array[0];
    QApplication qa(c,v);
    w = new MainWindow;
    w->show();
    qa.exec();
}
void GUI::notifyAlert(){

}

GUI::~GUI() {
    delete w;
}

Upvotes: 0

Sebastian Redl
Sebastian Redl

Reputation: 71889

You can't (well, you can, but you shouldn't). The MainWindon declaration is right where it should be. The problem is that you attempt to create a GUI object before you create the QApplication.

Why not create the QApplication where you create the GUI object, just before it?

Upvotes: 3

Related Questions