Results 1 to 17 of 17

Thread: Must construct QApplication before QPaintDevice

  1. #1
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Must construct QApplication before QPaintDevice

    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:

    Qt Code:
    1. int main(int argc, char ** argv)
    2. {
    3. QApplication app( argc, argv );
    4. MainWindowImpl win;
    5. win.show();
    6. app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by jpn; 1st June 2008 at 22:00. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Quote Originally Posted by sekatsim View Post
    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?

  3. #3
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    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.

    Qt Code:
    1. #ifndef MAINWINDOWIMPL_H
    2. #define MAINWINDOWIMPL_H
    3. //
    4. #include <QMainWindow>
    5. #include "ui_mainwindow.h"
    6. //
    7.  
    8.  
    9. class MainWindowImpl : public QMainWindow, public Ui::MainWindow
    10. {
    11. Q_OBJECT
    12. public:
    13. MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
    14. void refreshDisplay(int,long double curValue[36]);
    15. private:
    16.  
    17. bool bLatch;
    18. int maxValue[36];
    19. bool bCheckable[36][2];
    20. private slots:
    21. public slots:
    22. void bumpUp1();
    23. void bumpUpRETURN1();
    24. void bumpDn1();
    25. void bumpDnRETURN1();
    26. void linkLx1();
    27. void slideLx1(int);
    28.  
    29. void bumpUp2();
    30. void bumpUpRETURN2();
    31. void bumpDn2();
    32. void bumpDnRETURN2();
    33. void linkLx2();
    34. void slideLx2(int);
    35.  
    36. void bumpUp3();
    37. void bumpUpRETURN3();
    38. void bumpDn3();
    39. void bumpDnRETURN3();
    40. void linkLx3();
    41. void slideLx3(int);
    42.  
    43. void bumpUp4();
    44. void bumpUpRETURN4();
    45. void bumpDn4();
    46. void bumpDnRETURN4();
    47. void linkLx4();
    48. void slideLx4(int);
    49.  
    50. void bumpUp5();
    51. void bumpUpRETURN5();
    52. void bumpDn5();
    53. void bumpDnRETURN5();
    54. void linkLx5();
    55. void slideLx5(int);
    56.  
    57. void bumpUp6();
    58. void bumpUpRETURN6();
    59. void bumpDn6();
    60. void bumpDnRETURN6();
    61. void linkLx6();
    62. void slideLx6(int);
    63.  
    64. void bumpUp7();
    65. void bumpUpRETURN7();
    66. void bumpDn7();
    67. void bumpDnRETURN7();
    68. void linkLx7();
    69. void slideLx7(int);
    70.  
    71. void bumpUp8();
    72. void bumpUpRETURN8();
    73. void bumpDn8();
    74. void bumpDnRETURN8();
    75. void linkLx8();
    76. void slideLx8(int);
    77.  
    78. void pressGM();
    79. void slideGM(int);
    80. void checkLatch();
    81.  
    82. void options();
    83.  
    84. };
    85.  
    86. class linkSlide
    87. {
    88. private:
    89. void slideLink(int,int);
    90.  
    91. public:
    92. void slideFunct(int,int,int);
    93. void linkFunct(int);
    94. void slideGM(int);
    95. };
    96.  
    97. class BumpLatch
    98. {
    99.  
    100. public:
    101. bool checkable;
    102. int bumpUp(int,int);
    103. int bumpUpRETURN(int);
    104. int bumpDn(int,int);
    105. int bumpDnRETURN(int);
    106. void latch(bool);
    107.  
    108. };
    109.  
    110.  
    111. #endif
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Posts
    25
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    I don't see anything interesting there... What is Ui::MainWindow?

  5. #5
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Did you check if you have static keyword anywhere in your code?

  7. #7
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Quote Originally Posted by sekatsim View Post
    Was it a poor choice to declare my other classes within MainWindowImpl.h?
    No, it shouldn't be a problem.

    Quote Originally Posted by sekatsim View Post
    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?

  9. #9
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    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.

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Must construct QApplication before QPaintDevice

    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.
    J-P Nurmi

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Quote Originally Posted by jpn View Post
    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.

  12. #12
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    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?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Quote Originally Posted by sekatsim View Post
    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:
    Qt Code:
    1. MainWindowImpl mainwin;
    2. QTimer::singleShot( 0, & mainwin, "refreshDisplay" );
    To copy to clipboard, switch view to plain text mode 
    (provided that refreshDisplay() is a slot)?

  14. #14
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    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.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Quote Originally Posted by sekatsim View Post
    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?

  16. #16
    Join Date
    Jun 2008
    Posts
    29
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Must construct QApplication before QPaintDevice

    Exactly. I dont understand why. But at least I know to avoid it now

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Must construct QApplication before QPaintDevice

    Quote Originally Posted by sekatsim View Post
    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?

Similar Threads

  1. Replies: 16
    Last Post: 12th December 2014, 21:22
  2. Replies: 15
    Last Post: 21st April 2007, 18:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.