Results 1 to 13 of 13

Thread: To get the program execution back to main gui ?

  1. #1
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question To get the program execution back to main gui ?

    Hello Friends,
    I am working in windows.In my gui am calling a function say do_something(); which is implemented in the outer class. Now sometimes its implementation takes too much time so i want the user to stop that outer execution, i mean i want the program execution back to my gui so that user can do few other things.

    I tried it but its not returning back until the function implementation is not over. Like i tired to click some other buttons in my gui but those slots are implemented after program execution returns back !!

    Can anyone please help me out in this? I will be thankful.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To get the program execution back to main gui ?

    You can either use QCoreApplication::processEvents() or redesign your function to consist of many small steps that will be called from the event loop one after another.

  3. #3
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To get the program execution back to main gui ?

    Thank you Wysota Sir,
    I went through QCoreApplication documentation from where i came to know that QCoreApplication object has to be created in main() of Non-GUI application. Also as QCoreApplication:: processEvents() function is static.

    Ok now for informing i have sample.cpp where all my gui slots are implemented. Now i called do_something(); in this as: -
    Qt Code:
    1. ///////////Sample.cpp/////////
    2. ....
    3. ......
    4. void sample::do_something()
    5. {
    6. ....
    7. .....
    8. Do::do_something(); // This is function in Do class which is Non-gui & doesn't have main() function.
    9. }
    10. ..
    11. ...
    12. ....
    To copy to clipboard, switch view to plain text mode 
    I think i can't use QCoreApplication:: processEvents(proper_flags) just after Do::do_something();OR before it, right?
    Because even after trying it its not working.

    So am i doing it wrong or will i have to drop the plan of stopping its execution & let it run till it finishes. And go with your 2nd option i.e. to create small functions so that i can from time to time get back to gui ?

    Thanks again.
    Last edited by Krish; 8th April 2008 at 11:43.

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To get the program execution back to main gui ?

    When you need to call another lib/app where you've no control of, I would recommend to move this call into an own thread.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To get the program execution back to main gui ?

    You see... in C++ we have a concept called "inheritance" which among other things says that objects from classes inheriting other classes have their methods available. Now QApplication inherits QCoreApplication. Does that ring a bell?

  6. #6
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To get the program execution back to main gui ?

    Thanks Christian for taking out time & suggesting me QThread, I will look into that .

    Thanks Wysota, sorry i just missed to see the inheritance property between them. Yes sir I know that we can use the functions of inherited class. But Sir i tried to call the function but the execution is not coming back at user's request. Can you please tell me like from where to call QCoreApplication:: processEvents(proper_flags) function. I mean i know that it allows to process the events of only the calling class/thread, but from where like before calling do_something(); or ..

    Thanks again.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To get the program execution back to main gui ?

    Qt Code:
    1. void mylongfunction(){
    2. for(int i=0;i<1000000000;i++){
    3. doSomething();
    4. qApp->processEvents();
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To get the program execution back to main gui ?

    Thanks Wysota,
    As i can't use qapp->processEvents(); because qapp is declared in main.cpp and am calling do_something(); from sample.cpp.
    So as processEvents() is static i used it as: -
    Qt Code:
    1. /////Sample.cpp//////
    2. void sample::Do() // This is the slot which gets connected when user click Start pushbutton.
    3. {
    4. Start->setEnabled(FALSE);
    5. sqr.do_something();// This is the function of outer class.
    6. QCoreApplication::processEvents ();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Now below is the function implementation: -
    Qt Code:
    1. /////Square.cpp/////
    2. void Square::do_something()
    3. {
    4. for(int i=0;i<1000000000;i++){
    5. ...
    6. ....}
    7. }
    To copy to clipboard, switch view to plain text mode 

    By this am getting the executable but i can't execute any other gui slot before execution doesn't return from Square.cpp. Please help.

    Thanks again.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To get the program execution back to main gui ?

    Quote Originally Posted by Krish View Post
    Thanks Wysota,
    As i can't use qapp->processEvents(); because qapp is declared in main.cpp and am calling do_something(); from sample.cpp.
    Yes you can, qApp is a global pointer to the application instance.

    Qt Code:
    1. /////Sample.cpp//////
    2. void sample::Do() // This is the slot which gets connected when user click Start pushbutton.
    3. {
    4. Start->setEnabled(FALSE);
    5. sqr.do_something();// This is the function of outer class.
    6. QCoreApplication::processEvents ();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Now below is the function implementation: -
    Qt Code:
    1. /////Square.cpp/////
    2. void Square::do_something()
    3. {
    4. for(int i=0;i<1000000000;i++){
    5. ...
    6. ....}
    7. }
    To copy to clipboard, switch view to plain text mode 
    It won't work that way. processEvents() has to be called inside your do_something function inside the loop at each (well... not each as in every but as in sometimes) iteration.

    There is no magic here. processEvents() performs a one time scan over pending events and processes them. You have to call it every time you want pending events to be processed.

  10. #10
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To get the program execution back to main gui ?

    Thanks Sir,
    Quote Originally Posted by wysota View Post
    Yes you can, qApp is a global pointer to the application instance.
    I have tried this i mean with qapp->processEvents(); but it showed me error that qapp is not declared in this scope.

    Quote Originally Posted by wysota View Post
    It won't work that way. processEvents() has to be called inside your do_something function inside the loop at each (well... not each as in every but as in sometimes) iteration.
    There is no magic here. processEvents() performs a one time scan over pending events and processes them. You have to call it every time you want pending events to be processed.
    Thanks now i came to know what actually happens when this function gets called. It checks for any pending event during the execution in do_something(); so that if user clicks any button or does something it will also be executed as:-
    Qt Code:
    1. Void Square::do_something()
    2. {
    3. for(i=o;i<100000000;i++) {
    4. qapp->processEvents();
    5. ...
    6. ....}
    7. }
    To copy to clipboard, switch view to plain text mode 

    But sir how can i use qapp created in main.cpp in the outer .cpp class? Those are separate files. I have only included its header in sample.cpp where all slots are implemented. qapp is not even detected in Sample.cpp, giving same error. Or do i have to include any file in Square.cpp?
    Qt Code:
    1. ////main.cpp////
    2. #include <QApplication>
    3. #include <QtGui>
    4. #include "sample.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication *qapp(argc, argv);
    9. sample *dialog = new sample;
    10. dialog->show();
    11. qapp->connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Thanks again & sorry for disturbing you so much.
    Last edited by Krish; 8th April 2008 at 17:52.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To get the program execution back to main gui ?

    Quote Originally Posted by Krish View Post
    I have tried this i mean with qapp->processEvents(); but it showed me error that qapp is not declared in this scope.
    You forgot to #include <QApplication>


    Thanks now i came to know what actually happens when this function gets called. It checks for any pending event during the execution in do_something(); so that if user clicks any button or does something it will also be executed as:-
    Qt Code:
    1. Void Square::do_something()
    2. {
    3. for(i=o;i<100000000;i++) {
    4. qapp->processEvents();
    5. ...
    6. ....}
    7. }
    To copy to clipboard, switch view to plain text mode 
    Correct.

    But sir how can i use qapp created in main.cpp in the outer .cpp class? Those are separate files. I have only included its header in sample.cpp where all slots are implemented. qapp is not even detected in Sample.cpp, giving same error. Or do i have to include any file in Square.cpp?
    As you already noticed processEvents is a static method, so you can use it anywhere you want, just include the class' headers.

  12. The following user says thank you to wysota for this useful post:

    Krish (9th April 2008)

  13. #12
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: To get the program execution back to main gui ?

    Hey! Thanks Wysota sir its working now

    Ok but i have other question/ doubt. As now i can execute all other slots even if execution is busy, can't i stop the busy execution if takes really long time say more than 10- 20 mins?

    Or do i have to use QThreads in which i can in one thread keep GUI alive and in other thread call this busy function. And if it exceeds the waiting limit of user he can stop it with void QThread::terminate (); as we kill the QProcess with kill(); ?

    Thanks again in advance.
    Last edited by Krish; 9th April 2008 at 17:37.

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: To get the program execution back to main gui ?

    If you want to have total control, you should use threads or implement your functionality based on events (divide whole process into smaller chunks, etc.).

  15. The following user says thank you to wysota for this useful post:

    Krish (9th April 2008)

Similar Threads

  1. QProcess problem (Main program hangs)
    By sincnarf in forum Qt Programming
    Replies: 5
    Last Post: 11th October 2007, 09:26
  2. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  3. Replies: 5
    Last Post: 4th August 2006, 23:44

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.