PDA

View Full Version : Must construct QApplication before QPaintDevice



sekatsim
1st June 2008, 20:00
I've read a dozen posts on this topic from several forums, and nothing has seemed to help me. My program was running fine, until I switched some functions around from one class to another to cut down on memory waste-age. Now I get


QWidget: Must construct a QApplication before a QPaintDevice

I dont think I have any static objects, or at least, I'm not entirely sure what a static object is, so I doubt that I'd inadvertently created one. My main looks like:


int main(int argc, char ** argv)
{
QApplication app( argc, argv );
MainWindowImpl win;
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}

So the first thing I do is create a QApplication, but the error message seems to come even before main() is reached. I ran Valgrind, after reading about another person with the same problem.. it returned a lot of information, but I couldnt make much sense of any of it.

I'd really appreciate some assistance in figuring this out, as this is a problem I've run into several times in the past, and I've always just had to delete portions of code until it started working again, which is a considerable waste of time and effort.

Thank you for any advice you can give.

jacek
1st June 2008, 20:10
I dont think I have any static objects, or at least, I'm not entirely sure what a static object is, so I doubt that I'd inadvertently created one.
Do you have any global variables? Is there a "static" keyword anywhere in your sources?

sekatsim
1st June 2008, 21:45
I had one global variable, declared before main:

long double curValue[36];

But removing it still left me with the same problem. The only thing that I can think of is that there is something in MainWindowImpl.h which is causing this problem, since thats the only thing that comes before main, as far as I can tell. I'm posting it here, if you wouldnt mind looking through it and telling me if it could be causing problems.


#ifndef MAINWINDOWIMPL_H
#define MAINWINDOWIMPL_H
//
#include <QMainWindow>
#include "ui_mainwindow.h"
//


class MainWindowImpl : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
void refreshDisplay(int,long double curValue[36]);
private:

bool bLatch;
int maxValue[36];
bool bCheckable[36][2];
private slots:
public slots:
void bumpUp1();
void bumpUpRETURN1();
void bumpDn1();
void bumpDnRETURN1();
void linkLx1();
void slideLx1(int);

void bumpUp2();
void bumpUpRETURN2();
void bumpDn2();
void bumpDnRETURN2();
void linkLx2();
void slideLx2(int);

void bumpUp3();
void bumpUpRETURN3();
void bumpDn3();
void bumpDnRETURN3();
void linkLx3();
void slideLx3(int);

void bumpUp4();
void bumpUpRETURN4();
void bumpDn4();
void bumpDnRETURN4();
void linkLx4();
void slideLx4(int);

void bumpUp5();
void bumpUpRETURN5();
void bumpDn5();
void bumpDnRETURN5();
void linkLx5();
void slideLx5(int);

void bumpUp6();
void bumpUpRETURN6();
void bumpDn6();
void bumpDnRETURN6();
void linkLx6();
void slideLx6(int);

void bumpUp7();
void bumpUpRETURN7();
void bumpDn7();
void bumpDnRETURN7();
void linkLx7();
void slideLx7(int);

void bumpUp8();
void bumpUpRETURN8();
void bumpDn8();
void bumpDnRETURN8();
void linkLx8();
void slideLx8(int);

void pressGM();
void slideGM(int);
void checkLatch();

void options();

};

class linkSlide
{
private:
void slideLink(int,int);

public:
void slideFunct(int,int,int);
void linkFunct(int);
void slideGM(int);
};

class BumpLatch
{

public:
bool checkable;
int bumpUp(int,int);
int bumpUpRETURN(int);
int bumpDn(int,int);
int bumpDnRETURN(int);
void latch(bool);

};


#endif

Aceman2000
1st June 2008, 21:59
I don't see anything interesting there... What is Ui::MainWindow?

sekatsim
1st June 2008, 22:09
That was created by QDevelop in the initial project creation. I believe it is the UI file from QT Designer, but I'm not sure.

jacek
1st June 2008, 22:16
Did you check if you have static keyword anywhere in your code?

sekatsim
1st June 2008, 22:20
Ahh, yes, sorry. I did a file search for any occurrence of "static", nothing turned up.

Was it a poor choice to declare my other classes within MainWindowImpl.h? I've noticed that if I remove all of the other classes, I can execute, but I suppose that could mean many things.

jacek
1st June 2008, 23:06
Was it a poor choice to declare my other classes within MainWindowImpl.h?
No, it shouldn't be a problem.


I've noticed that if I remove all of the other classes, I can execute, but I suppose that could mean many things.
This means that the problem is in one of those removed classes. Do you use threads?

sekatsim
1st June 2008, 23:27
I've got two versions of the project now. I got stuck on this QApplication problem, so I backed it up and went back to an earlier version, from before I rearranged the functions. I've added threading to that one, as per my other post. But no. No threading in this version.

I've removed those classes one by one, in a few different orders, to figure out what might be causing the problem. But it doesnt really seem to be resolved until I remove all of them. Most of those classes do make adjustments to QWidgets.. i.e. setting and restoring slider values, etc, but I see no reason why those classes would be run before main.

Is there any way I can prohibit all "QApplication" type activities until the window has been drawn? I could try making a unique .h file for main.cpp, that only has the MainWindowImpl class.. but I fear it may screw me up down the road.

jpn
2nd June 2008, 07:05
Is it all in a single project or do you have app + lib? If so, make sure both are compiled in same mode, release vs. debug.

jacek
2nd June 2008, 21:00
Is it all in a single project or do you have app + lib? If so, make sure both are compiled in same mode, release vs. debug.
Good point. Other possibility is that the application is compiled in debug mode and linked with the release version of Qt.

sekatsim
3rd June 2008, 12:21
It's all in one project, and unless I've been specifically trying to solve a problem, I've been compiling in release mode. I think I found the problem, but I'm not quite sure why it was the problem:

I moved many of my functions outside of the mainwindowimpl class, but when they were done functioning, I wanted them to call refreshDisplay within mainwindowimpl to update all of my sliders/buttons etc. So I had

MainWindowImpl mainwin;
mainwin.refreshDisplay();

As far as I can tell, this is whats causing the problem. Is there anyway I can get around this?

jacek
3rd June 2008, 23:11
MainWindowImpl mainwin;
mainwin.refreshDisplay();

As far as I can tell, this is whats causing the problem. Is there anyway I can get around this?
Do I understand correctly that you invoke refreshDisplay() before QApplication::exec()?

Maybe the error message is a bit misleading and you should try:
MainWindowImpl mainwin;
QTimer::singleShot( 0, & mainwin, "refreshDisplay" );
(provided that refreshDisplay() is a slot)?

sekatsim
6th June 2008, 01:43
No, thats the thing, I dont *execute it*, persay, before QApplication::exec(), but it occurs in classes that are defined before QApplication::exec(). It seems like just having an instance of that class causes it to evaluate it and return an error.

So I guess I would suggest to anyone else having this problem to remove any instance of MainWindowImpl (or whatever your main QWidget drawing class is) that occurs outside of itself.

jacek
6th June 2008, 02:15
No, thats the thing, I dont *execute it*, persay, before QApplication::exec(), but it occurs in classes that are defined before QApplication::exec(). It seems like just having an instance of that class causes it to evaluate it and return an error.
So if you put "MainWindowImpl mainwin;" in your main(), you get the error and if you comment it out the error disappears?

sekatsim
6th June 2008, 03:31
Exactly. I dont understand why. But at least I know to avoid it now

jacek
8th June 2008, 00:00
Exactly. I dont understand why. But at least I know to avoid it now
It shouldn't behave this way. Does the error appear when you comment out all of the code in MainWindowImpl constructor?