Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Wait for other functions to end before terminating aplication
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Wait for other functions to end before terminating aplication

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 4.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Code_ReaQtor
    wrote on 12 Jan 2013, 08:00 last edited by
    #1

    I have an application which have heavy processes within it. It has a function function1()

    function1() contains these "heavy processes" and QEventLoop to make things synchronous and at the same time GUI will still be responsive

    While function1() is still processing, user is so eager to close the application and hit the "Close window button on the top right (Windows)" to terminate the app..... then window closed

    .... but after a second or two the window shows again. If function1() is not processing then window closes normally.

    The question is how can I make my app wait for the function1() to end before closing? Data is so important that everything inside function1() should be terminated normally so that it won't corrupt. I tried to reimplement QCloseEvent with "event->accept()" but don't think it is right.

    Any help? Thanks!

    Please visit my open-source projects at https://github.com/Code-ReaQtor.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 12 Jan 2013, 09:51 last edited by
      #2

      I think "this property":http://qt-project.org/doc/qt-4.8/qapplication.html#quitOnLastWindowClosed-prop of qApp is worth looking at in your case. It's true by default, but it should help you if you switch to false and control exiting yourself.

      (Z(:^

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Code_ReaQtor
        wrote on 12 Jan 2013, 10:48 last edited by
        #3

        Any examples how to use that property?

        I tried to add it in my QMainWindow constructor:

        @setAttribute(Qt::WA_QuitOnClose);
        qApp->setQuitOnLastWindowClosed(true); @

        ...and behaviour still the same, Windows closes and reappear again. By the time I am using
        @void MainWindow::closeEvent(QCloseEvent *ev)
        {
        if(isProcessing) //"isProcessing" is some bool used by my function1() to inform it isnt finished yet
        {
        qApp->quit(); //forced quit! Will take some seconds for the app to be reexecuted since I use QSharedMemory to allow only 1 app to run at a time
        }
        else
        {
        ev->accept();
        }
        }@

        to force quit my application when the function1() is still processing [will remove this when I find a solution].

        Sorry but the documentation is a little "unhelpful". Thanks

        Please visit my open-source projects at https://github.com/Code-ReaQtor.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vidar
          wrote on 12 Jan 2013, 11:19 last edited by
          #4

          Well, you probably should turn your function to be executed in a dedicated QThread.

          You could then use a "volatile flag" (or emit a signal) that tells the function to stop its processing. The main (GUI) thread can then simply wait for the function's thread to stop, by using its wait() method:

          (myFunction1Thread is a subclass of QThread)

          @void MyMainWindow::closeEvent ( QCloseEvent * event ) {
          myFunction1Thread->stopIt = true;
          myFunction1Thread->wait();
          }
          @

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Code_ReaQtor
            wrote on 12 Jan 2013, 11:29 last edited by
            #5

            Thanks vidar for the tip, I will try using QThread but with no guarantee since there are too much variables (and data) to exchange with the main thread plus function1() uses sql, xml, and network within it. I think this will help me though.

            Please visit my open-source projects at https://github.com/Code-ReaQtor.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vidar
              wrote on 12 Jan 2013, 11:36 last edited by
              #6

              I think it's worth a try. If your application gets more and more complex, you might want to make your main GUI thread to contain ONLY interactions with the GUI and to put any kind of time-consuming tasks (e.g. large SQL queries etc.) into other threads.
              Not working with a clean "thread architecture" will probably become hard to maintain.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 12 Jan 2013, 11:52 last edited by
                #7

                [quote author="Code_ReaQtor" date="1357987680"]Any examples how to use that property?

                I tried to add it in my QMainWindow constructor:

                @setAttribute(Qt::WA_QuitOnClose);
                qApp->setQuitOnLastWindowClosed(true); @[/quote]

                true is the default, and you don't want that. You should turn that property to false. It will prevent your app from quitting when user clicks the close button.

                (Z(:^

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Code_ReaQtor
                  wrote on 12 Jan 2013, 14:15 last edited by
                  #8

                  Thanks sierdzio, I get it now.

                  I also made another method, that is using modal QProgressDialog as a Qt::Popup to prevent the user from clicking the close button when function1() is still executing. I guess this way will be "non-boring" for the user and will inform him that something "important" is happening. The time might take 1 sec minimum up to few minutes though, depending on the "weight" of the data it processes.

                  I also tried QThread as vidar suggested but I needed to segment my codes with a "flag check", and I guess it is impractical since my code is too long...

                  @/My codes/
                  if(flag) { //terminate
                  }
                  /another code/
                  if(flag) { //terminate
                  }
                  /another code/
                  if(flag) { //terminate
                  }
                  /another code/
                  if(flag) { //terminate
                  }
                  /another code blah blah/@

                  "flag-checking" can also be changed into a function but still I need to segment my codes and insert function calls into it.

                  Thanks guys!

                  Please visit my open-source projects at https://github.com/Code-ReaQtor.

                  1 Reply Last reply
                  0

                  1/8

                  12 Jan 2013, 08:00

                  • Login

                  • Login or register to search.
                  1 out of 8
                  • First post
                    1/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved