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. Make GUI main() thread responsive when Qthread is running
Forum Updated to NodeBB v4.3 + New Features

Make GUI main() thread responsive when Qthread is running

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 875 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.
  • M Offline
    M Offline
    meganathan
    wrote on last edited by
    #1

    Hi,

    i am trying to update my GUI when QThread is still running.
    Inside run():

    QProcess *process = new QProcess();
    process->setWorkingDirectory(QString(getenv("TEMP"))+"\Felix");
    process->start(""" + ansysPath1 + "" -b -i get_node_results.mac -o output.out");
    process->waitForFinished(-1);

    in mainwindow:

    FelixThread *ansysThread = new FelixThread(tempDirPath,ansysPath,felixMutex);
    ansysThread->start();
    qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
    ansysThread->wait();

    the ansysthread will take more time, i even tried to get finished() signal of the thread, but on using finished() emits before the qthread finished executing the script inside run(). Any suggestion how to approach this??

    SamurayHS 1 Reply Last reply
    0
    • M meganathan

      Hi,

      i am trying to update my GUI when QThread is still running.
      Inside run():

      QProcess *process = new QProcess();
      process->setWorkingDirectory(QString(getenv("TEMP"))+"\Felix");
      process->start(""" + ansysPath1 + "" -b -i get_node_results.mac -o output.out");
      process->waitForFinished(-1);

      in mainwindow:

      FelixThread *ansysThread = new FelixThread(tempDirPath,ansysPath,felixMutex);
      ansysThread->start();
      qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
      ansysThread->wait();

      the ansysthread will take more time, i even tried to get finished() signal of the thread, but on using finished() emits before the qthread finished executing the script inside run(). Any suggestion how to approach this??

      SamurayHS Offline
      SamurayHS Offline
      SamurayH
      wrote on last edited by SamurayH
      #2

      Hi @meganathan,

      QProcess starts processes asynchronously, so no need to use QThread.
      Instead try something like this (in mainwindow):

      QProcess *process = new QProcess(this);
      process->setWorkingDirectory(QString(getenv("TEMP"))+"\Felix");
      
      // you can connect the finished signal of process to a lambda like this, or by using a slot.
       
      connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
          [=](int exitCode, QProcess::ExitStatus exitStatus)
              {
                   /* here you can handle the finished state of your process */
              });
      	
      process->start(""" + ansysPath1 + "" -b -i get_node_results.mac -o output.out");
      

      As I said, use this code inside mainwindow (GUI thread), and it's not going to block anything. QProcess is designed to work asynchronously and synchronously (waitForFinished()). In your case you must use its signals, instead of its synchronous API
      .

      "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

      M 1 Reply Last reply
      2
      • SamurayHS SamurayH

        Hi @meganathan,

        QProcess starts processes asynchronously, so no need to use QThread.
        Instead try something like this (in mainwindow):

        QProcess *process = new QProcess(this);
        process->setWorkingDirectory(QString(getenv("TEMP"))+"\Felix");
        
        // you can connect the finished signal of process to a lambda like this, or by using a slot.
         
        connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            [=](int exitCode, QProcess::ExitStatus exitStatus)
                {
                     /* here you can handle the finished state of your process */
                });
        	
        process->start(""" + ansysPath1 + "" -b -i get_node_results.mac -o output.out");
        

        As I said, use this code inside mainwindow (GUI thread), and it's not going to block anything. QProcess is designed to work asynchronously and synchronously (waitForFinished()). In your case you must use its signals, instead of its synchronous API
        .

        M Offline
        M Offline
        meganathan
        wrote on last edited by
        #3

        Hi @SamurayH ,

        i am using Qt 4.8 ,does it support
        connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
        [=](int exitCode, QProcess::ExitStatus exitStatus)
        {
        /* here you can handle the finished state of your process */
        });

        i made it like this
        connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(onQProcessFinish(int,QProcess::ExitStatus))); //in constructor

        finished sigmal is not emitting..do i need to use QEventloop to block the Qprocess->start() to complete the Qprocess.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You should also check for errors with QProcess.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            You should also check for errors with QProcess.

            M Offline
            M Offline
            meganathan
            wrote on last edited by
            #5

            @SGaist
            Hi,
            connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(onQProcessFinish(int,QProcess::ExitStatus)))
            connect(process,SIGNAL(error()),this,SLOT(onQProcessFailure()));

            Both are in constructor of mainwindow. both the signal are not emitting

            SamurayHS 1 Reply Last reply
            0
            • M meganathan

              @SGaist
              Hi,
              connect(process,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(onQProcessFinish(int,QProcess::ExitStatus)))
              connect(process,SIGNAL(error()),this,SLOT(onQProcessFailure()));

              Both are in constructor of mainwindow. both the signal are not emitting

              SamurayHS Offline
              SamurayHS Offline
              SamurayH
              wrote on last edited by SamurayH
              #6

              @meganathan,

              Make sure you're connecting the signals before calling start.

              "قال رسول الله صلى الله عليه وسلم : " أحب الناس إلى الله أنفعهم للناس

              M 1 Reply Last reply
              0
              • SamurayHS SamurayH

                @meganathan,

                Make sure you're connecting the signals before calling start.

                M Offline
                M Offline
                meganathan
                wrote on last edited by
                #7

                @SamurayH On using finished() signal to emit, will the main thread wait until finished() completes the execution.
                Because i need to work on the task based on files generated by scripts.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  No, the idea is that the code you want call once your process has finished is in the slot you connect to the finished signal.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3

                  • Login

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