Discussion Board

Results 1 to 11 of 11
  1. #1
    Registered User trandinhduy's Avatar
    Join Date
    Mar 2009
    Posts
    14
    Hi,

    In my .cpp file, I declare some global variables of type QPixmap. When I run the app, it crashes when the app is starting (even before the constructor of the class is called).

    Here is the code:

    Code:
    // Own
    #include "mainwindow.h"
    ...
    
    
    QPixmap defaultImage; //This causes runtime error
    QPixmap waitingImage; //This causes runtime error
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {      
        ui->setupUi(this);
    
        ....
    }
    Note that I still not use these variables elsewhere, just declare them and the app is crashed when starting.

    Anyone can help???

    Thanks.

  2. #2
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    It may be that you can't create complex Qt static objects, since QApplication had not yet run to initialize the runtime environment.

  3. #3
    Nokia Developer Moderator divanov's Avatar
    Join Date
    Oct 2009
    Posts
    4,326
    You must construct a QApplication before using QPaintDevice.

  4. #4
    Registered User trandinhduy's Avatar
    Join Date
    Mar 2009
    Posts
    14
    Quote Originally Posted by divanov View Post
    You must construct a QApplication before using QPaintDevice.
    Could you please make it more detail or give an example about this. I do not understand about what you mean?

    My app runs normally before I add these two variables (just adding, still not using it).

    Thank you.

  5. #5
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Basically we're saying you can't do what you want to do. In your main() you have to code a QApplication() as your very first Qt operation. This creates the Qt environment. Static initialization occurs before your main() is executed, so there is no Qt environment when the QPixmaps attempt to initialize themselves.

    I can't think of a way to reliably create a QApplication before other objects are statically inited. Even if you coded the QApplication as a static (and that actually worked, which is doubtful), you'd have no guarantee that it would occur before other static initializations.

  6. #6
    Registered User trandinhduy's Avatar
    Join Date
    Mar 2009
    Posts
    14
    Oh, I got your idea. Thanks

    Just clarify a little bit:

    I am using QMainWindow as a main class, so these global variables are init before my main class is init. This cause the error?

    Is there any way I can solve this problem????

    Thanks.

  7. #7
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Well, yes, but more importantly QApplication (look in your main.cpp) is inited before anything else, including your QMainWindow. But it's not inited before static objects are inited (due to the way C++ works), and that's the problem.

  8. #8
    Registered User trandinhduy's Avatar
    Join Date
    Mar 2009
    Posts
    14
    Ahhhh, finally solve it.

    I just declare global variables QPixmap as a pointer, then I init them in the constructor of MainWindow.

    Thank you guys.

  9. #9
    Nokia Developer Moderator divanov's Avatar
    Join Date
    Oct 2009
    Posts
    4,326
    Just note that using global variables is a sign of a bad design.

  10. #10
    Registered User trandinhduy's Avatar
    Join Date
    Mar 2009
    Posts
    14
    Quote Originally Posted by divanov View Post
    Just note that using global variables is a sign of a bad design.
    Oh really. .

    It would be great if you can help me to improve myself.

    In my case: I have some images to use for many widgets at the same time. For that reason, what should I do??? Should I use singleton by placing those images in the MainWindow and call get method to get them???

    Thank you.

  11. #11
    Nokia Developer Champion danhicksbyron's Avatar
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209
    Keep in mind that Qt does pixmap caching for you, so you don't have to go overboard in trying to avoid reloading QPixmaps.

Similar Threads

  1. Nokia Qt SDK missing QT Mobility - messaging
    By Zeddeh in forum [Archived] Qt SDKs and Tools
    Replies: 5
    Last Post: 2010-06-07, 08:46
  2. How to manage Access Point for qt?
    By KimWonChol in forum [Archived] Qt SDKs and Tools
    Replies: 1
    Last Post: 2010-01-22, 18:23
  3. error including vector
    By Lichtens in forum Open C/C++
    Replies: 5
    Last Post: 2009-04-03, 14:19
  4. 编译3rd Sample程序时的LINK错误
    By mapk in forum Symbian
    Replies: 9
    Last Post: 2008-06-17, 04:26
  5. problem with the XML Parser SyExpat
    By Vitaly_iva in forum Symbian C++
    Replies: 2
    Last Post: 2006-06-05, 11:09

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved