PDA

View Full Version : QPixmap Error with Qt5



ahmed.khaled
7th February 2013, 17:43
I have migrated my solution from Qt4.8 to Qt5.0.1, after fixing the compilation and linking errors i got a run time error as follows

QPixmap: Must construct a QApplication before a QPaintDevice


8688

taking in to consideration that the application was running before migrating to Qt5.0.1 and i am sure that i am constructing the QApplication in the first line in the main method as follows
int main(argc, argv)
{
std::unique_ptr<QApplication> app(new QApplication(argc, argv));
.
.
.
.
std::unique_ptr<MyMainForm> form(new MyMainForm(NULL));
.
.
.

return app.exec();
}

I tried to reproduce the problem in a smaller solution but it didn't appear, any suggestions will be welcomed.
I debugged the ui_MyMainForm.h file and the code that causes the error is
iconLabel->setPixmap(QPixmap(QString::fromUtf8(":/icons/icons/image.png")));

working environment
Qt5.0.1 with MSVisual studio 2010 x86
Windows 7 x64

d_stranz
7th February 2013, 18:25
That's certainly a strange way to write a main() function. There is no need whatsoever to wrap either the QApplication instance or the MyMainForm instance in std::unique_ptr wrappers or to create them on the heap. If you construct your QApplication and MyMainForm on the stack, they are unique and will be destructed automatically when main() exits.

Not only that, but the code you've provided won't compile (two declarations of "app") so how are we know what your code really looks like? The best we can do with this is to start throwing out random guesses as to why it fails. The fact that you tried a smaller solution means that you are doing something wrong in this one.

Please use "CODE" tags when posting source code.

ahmed.khaled
7th February 2013, 20:30
Thanks for the reply
I am sorry for not checking the code i wrote before posting it
the code is
int main(argc, argv)
{
std::unique_ptr<QApplication> app(new QApplication(argc, argv));
.
.
.
.
std::unique_ptr<MyMainForm> form(new MyMainForm(NULL));
.
.
.

return app.exec();
}

I also tried to create on the stack but in vain;
int main(argc, argv)
{
QApplication app(argc, argv);
.
.
.
.
MyMainForm form(NULL);
.
.
.

return app.exec();
}
still doesnt work
I think you didnt notice that I told the project was runing well with Qt4.8 and the problem happened only after migration to 5.0.1, ireturned back to work with Qt4.8 and it worked.

d_stranz
7th February 2013, 21:21
You know, it's all the stuff where you have put ". . ." that probably makes all the difference. If you want help in solving a problem, you have to give us a little more than 4 lines of pseudo code.

The fact that it works in 4.8 and not 5.0 is probably due to a bug fix or other change made in 5.0 that fixes the loophole in Qt that allowed your original code to work. It still doesn't mean your original code was correct, it just happens to work. And if you can't reproduce the problem in a smaller example, it definitely means your original code is incorrect.

And please use CODE tags when posting source code. Click the "Go Advanced" button when making a post. You'll see a "#" tool button. Click it to insert the tags and paste your code in between.

ChrisW67
7th February 2013, 22:48
Your first example, using std::unique_ptr to wrap QApplication won't compile. The app object has no exec() function: you want the -> indirection operator. Neither example will compile because argc and argv have no type. To use your example we have to fix problems that are not related to the question. This is why we ask people to copy and paste actual compilable code.

Quite aside from that, QPixmap is complaining and nothing you have shown us has anything to do with pixmaps.

Got any classes being instantiated as global static instances? Any of those manipulate QPixmaps? These initialisation occur before main(). You could also be experiencing static initialization order issues.