PDA

View Full Version : Please instantiate the QApplication object first



zorro68
24th October 2007, 07:25
Now my application is finish (only some little modification) and I run in windows mode and works ok. I use Qt because i want to run in windows and linux mode. But when i run in linux mode, compiled and linked ok but run bad. I have this message in linux terminal windows:

QCoreApplication::applicationDirPath: Please instantiate the QApplication object first

My main.cpp is:



Q_INIT_RESOURCE(qtdam);

QApplication *app = new QApplication(argc, argv);
QSplashScreen *splash = new QSplashScreen;
QString path=app->applicationDirPath();
lang.setfile_idioma(path+"/languages.lng");
if (lang.idioma_seleccionado=="Español")
splash->setPixmap(QPixmap(":/images/splash_espagnol.png"));
else
splash->setPixmap(QPixmap(":/images/splash.png"));
splash->show();
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(lang.leer_idioma("1"),topRight, Qt::white);
MainWindow mainWin;
mainWin.show();
splash->finish(&mainWin);
delete splash;
return app->exec();


the splash screen (*.png) works and read the language.lng ok because it puts the correct png, but it stop there. I have to do Ctrl+C to stop the application.

What's happend???

PS: I have Mandriva 2007

marcel
24th October 2007, 08:08
Do you instantiate any QObjects, like some static objects before the QApplication?

zorro68
24th October 2007, 09:33
#include <QSplashScreen>
#include <QApplication>

#include "mainwindow.h"
#include "languages.h"

IDIOMA lang; //-> this is a class

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(qtdam);
...........

marcel
24th October 2007, 10:17
What is the class declaration?
If it contains any QObjects, then that's your error.

You define lang as file static, so it will get instantiated before everything you define in main.

With Qt, no QObject can be instantiated before QApplication. QApplication sets up the proper environment.

jpn
24th October 2007, 10:25
What does IDIOMA constructor do? Do you have other global/static objects?

zorro68
24th October 2007, 10:50
I have changed my main.cpp code for this one:



#include <QSplashScreen>
#include <QApplication>

#include "mainwindow.h"
#include "languages.h"

int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(qtdam);

QApplication app(argc, argv);
QSplashScreen *splash = new QSplashScreen;
QString path=app.applicationDirPath();
IDIOMA *lang = new IDIOMA();
lang->setfile_idioma(path+"/languages.lng");
if (lang->idioma_seleccionado=="Español")
splash->setPixmap(QPixmap(":/images/splash_espagnol.png"));
else
splash->setPixmap(QPixmap(":/images/splash.png"));
splash->show();
Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;
splash->showMessage(lang->leer_idioma("1"),topRight, Qt::white);
MainWindow mainWin;
mainWin.show();
splash->finish(&mainWin);
delete splash;
return app.exec();
}


and in windows works but in linux show the splash screen and dont say nothing, but the app not show????

zorro68
25th October 2007, 12:45
Nobody knows what to do???:confused:

jpn
25th October 2007, 13:20
To me it looks like it shouldn't even show a splash screen at all because you delete it before the application enters to the event loop. Please re-check QSplashScreen docs for how to use it correctly. Notice the example in detailed description.