Results 1 to 9 of 9

Thread: Problem with return a.exec(). I need a solution here...

  1. #1
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Problem with return a.exec(). I need a solution here...

    Hello developers!



    My application is both console and GUI, depending on the arguments it takes.

    So, the code is as follows:

    Qt Code:
    1. #include.....
    2.  
    3. //..
    4.  
    5. class EarthTimer (bla bla, making a subclass of QTimer....
    6.  
    7. ..
    8. //here we connect the timer with the timers_slot() function
    9. }
    10.  
    11. int main(int argc, char *argv[])
    12. {
    13. QApplication a(argc, argv);
    14. EarthTimer earthtimer;
    15. earthtimer.start(1800000); //30 min
    16. return a.exec(); //we have started the timer but we have to call this otherwise the application will immediately close
    17.  
    18.  
    19. }
    20.  
    21. void timers_slot(){
    22. //doing some specific job
    23. ..//code
    24. ..
    25. ..
    26. if(something_is_true){
    27. MainWindow w;
    28. w.show();
    29. /*here I am trying to start the GUI, what it does is to go through the constructor
    30. of the MainWindow class and then exits, without showing the GUI, I guess I have
    31. to call return a.exec() again, but a is not declared here and cannot be global :(*/
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    Ι think the comments are pretty specific, but if someone didn't get it:
    I call return a.exec() after starting the timer so as to avoid program's quit, and then if something is true in the timers_slot() I am trying to open the GUI. It goes through the constructor of the GUI but it does not show anything. I assume I have to call return a.exec() again there but it is declared in main() function and cannot be global.
    May I create another QApplication and return it there?
    Last edited by hakermania; 3rd July 2011 at 20:29.
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with return a.exec(). I need a solution here...

    It is a little odd that you want an application that does nothing visible at all for 30 minutes.

    The way your code is above there is no way that the free function timers_slot() is actually a slot, cannot be the target of a connect(), and therefore never called after 30 minutes. However, assuming that timers_slot() is actually a member of EarthTimer and declared a slot then there doesn't seem anything grossly wrong in your code.

  3. The following user says thank you to ChrisW67 for this useful post:

    hakermania (4th July 2011)

  4. #3
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with return a.exec(). I need a solution here...

    Quote Originally Posted by ChrisW67 View Post
    It is a little odd that you want an application that does nothing visible at all for 30 minutes.

    The way your code is above there is no way that the free function timers_slot() is actually a slot, cannot be the target of a connect(), and therefore never called after 30 minutes. However, assuming that timers_slot() is actually a member of EarthTimer and declared a slot then there doesn't seem anything grossly wrong in your code.
    Thanks Chris for your answer. The above code is example code ofcourse. Yes, the timers_slot() is a 'public slot' of EarthTimer in the normal code and runs OK.
    Additionally, the program displays info before starting the timer, so its work is to do a job every 30 min, nothing irrational

    I know that my code hasn't something grossy, but I don't know how to call w.show() so as to show the GUI, because the GUI doesn't come up, it only goes through its constructor and exits!
    See the last comments of my code in the 1st post for more info
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with return a.exec(). I need a solution here...

    Actually, on a post-coffee viewing the problem is obvious. You allocate your main window on the stack (line 27) rather than the heap. The result is that the object is destroyed at the closing brace of the if statement. It doesn't show because it doesn't exist.

  6. The following user says thank you to ChrisW67 for this useful post:

    hakermania (4th July 2011)

  7. #5
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with return a.exec(). I need a solution here...

    Thanks, the problem is known

    Can you suggest a solution?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  8. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with return a.exec(). I need a solution here...

    Quote Originally Posted by hakermania View Post
    Can you suggest a solution?
    Chris already gave you the solution, maybe he didn't state it clear enough:

    create your main window on the heap instead on the stack.

  9. The following user says thank you to FelixB for this useful post:

    hakermania (5th July 2011)

  10. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with return a.exec(). I need a solution here...

    ... and that means with operator new

  11. The following user says thank you to stampede for this useful post:

    hakermania (5th July 2011)

  12. #8
    Join Date
    Jul 2010
    Location
    /home/hakermania/
    Posts
    233
    Thanks
    129
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with return a.exec(). I need a solution here...

    Thanks This was an easy solution
    Can I find a link somewhere to understand better why was this done?
    When you 're trying to help somebody in the newbie section, don't forget that he is a newbie. Be specific and give examples.

  13. #9
    Join Date
    Jul 2009
    Posts
    74
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with return a.exec(). I need a solution here...

    Stack variables exist only as long as the function or block they were created in remains in scope
    Qt Code:
    1. void foo()
    2. {
    3. int a;
    4.  
    5. {
    6. int b;
    7. } // b is destroyed
    8. } // a is destroyed
    To copy to clipboard, switch view to plain text mode 

    When you read your code, the MainWindow w is destroyed once you leave your if-block.

    Variables on the heap are destroyed explicitly using the "delete" statement - either you have to take care of that yourself, or use helper classes (e.g. the QObject tree can be used to automatically destroy children of QObjects)

    That is, they exist until you tell them to go away.

    Hope that helps

    Robert
    Last edited by Asperamanca; 6th July 2011 at 11:11. Reason: reformatted to look better

  14. The following user says thank you to Asperamanca for this useful post:

    hakermania (6th July 2011)

Similar Threads

  1. QDialog problem with exec()
    By nomadscarecrow in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2010, 18:40
  2. How to make QMenu.exec() return a QWidgetAction
    By gregsan in forum Qt Programming
    Replies: 5
    Last Post: 15th April 2010, 00:04
  3. Replies: 0
    Last Post: 16th March 2010, 12:24
  4. QProcess and kfmclient exec problem
    By ramazangirgin in forum Qt Programming
    Replies: 5
    Last Post: 6th June 2008, 08:31
  5. Problem and solution about QSA 1.2.0 compilation
    By Dwarf007 in forum Installation and Deployment
    Replies: 5
    Last Post: 7th March 2006, 16:52

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.