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. QProcess finish signal not emitted.
Forum Updated to NodeBB v4.3 + New Features

QProcess finish signal not emitted.

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 4 Posters 2.8k Views 3 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.
  • T TiagoSD

    Hello, I have been struggling for a few days trying to make my Qt application to execute another application by using a QProcess and waiting for its completion
    by connecting to the finish signal.

    I have tried this way:

    connect(&proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
    [=](int exitCode, QProcess::ExitStatus exitStatus)
    {
    std::cout<<" Received"<<std::endl;
    this->handle_finished(exitCode);
    });

    and I have also tried :

    QObject::connect(&proc, SIGNAL(finished(int)), this, SLOT(handle_finished(int)),Qt::QueuedConnection);

    If use the second option, it never ever receive the signal, even if the process is closed or terminated by my main application.

    If I use the first method, I get a signal, but as a directConnect handled signal, and only if I close the QProccess from my own QtApplication.

    In neither case I receive the signal if the application closes by itself (or is closed by the operating system). I have tried literally a dozen different applications an all have this same issue (so I know the problem is not on the application I am starting).

    I am running an Ubuntu 20.04 desktop with Qt 5.12.8

    Can anyone point me some hints on how can I figure what is not working?

    I forgot to say, I do these connections in a thread that is not the main thread (is the same one that creates the QProcess).

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #3

    @TiagoSD said in QProcess finish signal not emitted.:

    I forgot to say, I do these connections in a thread that is not the main thread (is the same one that creates the QProcess).

    Could you clarify this sentence: the connects, signals, slots and the QProcess are all in the non-UI thread? Or, the QProcess is in the main thread, but the connections/signals/slots are not?

    T 1 Reply Last reply
    0
    • JonBJ JonB

      @TiagoSD said in QProcess finish signal not emitted.:

      I forgot to say, I do these connections in a thread that is not the main thread (is the same one that creates the QProcess).

      Could you clarify this sentence: the connects, signals, slots and the QProcess are all in the non-UI thread? Or, the QProcess is in the main thread, but the connections/signals/slots are not?

      T Offline
      T Offline
      TiagoSD
      wrote on last edited by TiagoSD
      #4

      @JonB The QProcces is created in a thread different from the GUI and connected immediately in the same thread.. I wanted to connect the signal with a queuedConnection exactly to ensure that this would not cause any problem.

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

        Hi and welcome to devnet,

        What exactly are you executing ?
        Can you show the full code related to QProcess ?

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

        T 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          What exactly are you executing ?
          Can you show the full code related to QProcess ?

          T Offline
          T Offline
          TiagoSD
          wrote on last edited by
          #6

          @SGaist said in QProcess finish signal not emitted.:

          Hi and welcome to devnet,

          What exactly are you executing ?
          Can you show the full code related to QProcess ?

          Basically this:
          procc = new QProcess();

          connect(procc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
              [=](int exitCode, QProcess::ExitStatus exitStatus)
          {
              std::cout<<"DEBUG"<<std::endl;
              this->handle_finished(exitCode);        
          });
          

          procc->start(appname.c_str());

          Now, I just discovered something.. weird. If I call waitForStarted().. it goes immediately into running state (otherwise it stays stuck in starting.. forever even after the process ends).

          but it does never reach not_running state, unless I waitForFinished()

          It seems the problem is not in the signals but in Qt keeping track of the state correctly. As far as I understood the documentation I should be able to start the process. NOT call waitForFinished() and expect the state of the QProccess to still be updated to the correct values. Am I wrong?

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

            What application are you trying to start that has issues ?

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

            JonBJ T 2 Replies Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Are you blocking the eventloop somehow?

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • SGaistS SGaist

                What application are you trying to start that has issues ?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #9

                @SGaist
                You know more than I, but I don't think the process he runs is at issue. I believe it itself runs fine. The issue, I think, lies in the use of threads, or where the event loop is allowed to run so the status is updated, signals are delivered, etc. The waitFor...() spins its own loop and works. No?

                1 Reply Last reply
                0
                • SGaistS SGaist

                  What application are you trying to start that has issues ?

                  T Offline
                  T Offline
                  TiagoSD
                  wrote on last edited by
                  #10

                  @SGaist I have tried even a helloworld program. Simple basic std::cout<<"Hello World" <<std::endl; return 0 at main. My objective is to tun another application that I made with Qt, but when I started to face the problem I decided to try with an application as simple as possible to help detect what is wrong.

                  @Christian-Ehrlicher nope. The events from my interface work correctly, all clicks, released etc events are handled perfectly even while the process that I started is running, so I figure the event Loop must be working.

                  I can reproduce the same issue by creating the minimal QtWidget program with QtCreator and with the following main.cpp

                  #include "mainwindow.h"

                  #include <QApplication>
                  #include <QProcess>
                  #include <thread>
                  #include <iostream>

                  int main(int argc, char *argv[])
                  {
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();

                  std::thread* ptr = new std::thread([]()
                  {
                      std::this_thread::sleep_for(std::chrono::seconds(5));
                      QProcess proc;
                      proc.start("/tmp/batata");
                      while(proc.state()!=QProcess::NotRunning)
                          std::cout<<" waiting:"<<proc.state()<<std::endl;
                      std::cout<<"DONE"<<std::endl;
                  }) ;
                  
                  auto execresult= a.exec();
                  ptr->join();
                  delete ptr;
                  return execresult;
                  

                  }

                  the process state stays stuck at 1.. forever.. the program at /tmp/batata just writes linesin a file for 10 seconds and then exits... but even after it finishes the state is stuck at 1.

                  I must be missing something here. Even by this simple example the QProcess is created and started quite after the QApplication exec starts (the sleep is there to ensure it).

                  JonBJ 1 Reply Last reply
                  0
                  • T TiagoSD

                    @SGaist I have tried even a helloworld program. Simple basic std::cout<<"Hello World" <<std::endl; return 0 at main. My objective is to tun another application that I made with Qt, but when I started to face the problem I decided to try with an application as simple as possible to help detect what is wrong.

                    @Christian-Ehrlicher nope. The events from my interface work correctly, all clicks, released etc events are handled perfectly even while the process that I started is running, so I figure the event Loop must be working.

                    I can reproduce the same issue by creating the minimal QtWidget program with QtCreator and with the following main.cpp

                    #include "mainwindow.h"

                    #include <QApplication>
                    #include <QProcess>
                    #include <thread>
                    #include <iostream>

                    int main(int argc, char *argv[])
                    {
                    QApplication a(argc, argv);
                    MainWindow w;
                    w.show();

                    std::thread* ptr = new std::thread([]()
                    {
                        std::this_thread::sleep_for(std::chrono::seconds(5));
                        QProcess proc;
                        proc.start("/tmp/batata");
                        while(proc.state()!=QProcess::NotRunning)
                            std::cout<<" waiting:"<<proc.state()<<std::endl;
                        std::cout<<"DONE"<<std::endl;
                    }) ;
                    
                    auto execresult= a.exec();
                    ptr->join();
                    delete ptr;
                    return execresult;
                    

                    }

                    the process state stays stuck at 1.. forever.. the program at /tmp/batata just writes linesin a file for 10 seconds and then exits... but even after it finishes the state is stuck at 1.

                    I must be missing something here. Even by this simple example the QProcess is created and started quite after the QApplication exec starts (the sleep is there to ensure it).

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #11

                    @TiagoSD said in QProcess finish signal not emitted.:

                    while(proc.state()!=QProcess::NotRunning)
                        std::cout<<" waiting:"<<proc.state()<<std::endl;
                    

                    But this blocks, how will state get updated? Or finish get emitted?

                    BTW: are you (trying to) use threads because of wanting to run a process, not block, and be notified of finish? Because with QProcess and Qt it's asynchronous, you get that without needing threads.

                    T 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @TiagoSD said in QProcess finish signal not emitted.:

                      while(proc.state()!=QProcess::NotRunning)
                          std::cout<<" waiting:"<<proc.state()<<std::endl;
                      

                      But this blocks, how will state get updated? Or finish get emitted?

                      BTW: are you (trying to) use threads because of wanting to run a process, not block, and be notified of finish? Because with QProcess and Qt it's asynchronous, you get that without needing threads.

                      T Offline
                      T Offline
                      TiagoSD
                      wrote on last edited by TiagoSD
                      #12

                      @JonB It is bocking a different thread.. not the Qt one.

                      I need to use threads for other purposes, the start of the application is not bound to the GUI , but to external events that are waited by a different thread (so when the external event happens I start the QProces.

                      I

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        As I guessed - there is no eventloop in the thread where QProcess lives.
                        No thread is needed at all for this task, and when then use QThread and run an eventloop.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        T 1 Reply Last reply
                        1
                        • Christian EhrlicherC Christian Ehrlicher

                          As I guessed - there is no eventloop in the thread where QProcess lives.
                          No thread is needed at all for this task, and when then use QThread and run an eventloop.

                          T Offline
                          T Offline
                          TiagoSD
                          wrote on last edited by
                          #14

                          @Christian-Ehrlicher Ok, the problem is that the thread that I pass the lambda (that creaes the QProcess) is created by another library (not mine but i need to use it). So I will have to use somethign other than QProcess to handle this.

                          Thanks for the help anyway.

                          1 Reply Last reply
                          0
                          • Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            Then emit a signal there and move to a QThread with an eventloop or use QEventLoop directly.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            0

                            • Login

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