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::startDetached() doesn't work on self-written programs
Forum Updated to NodeBB v4.3 + New Features

QProcess::startDetached() doesn't work on self-written programs

Scheduled Pinned Locked Moved Solved General and Desktop
qprocessstartdetached
17 Posts 3 Posters 2.0k Views
  • 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.
  • Q Offline
    Q Offline
    Qt_Bob
    wrote on last edited by
    #5

    @Christian-Ehrlicher @JonB

    Can't I just use

        qDebug() << process.readAllStandardError();
        qDebug() << process.readAllStandardOutput();
        qDebug() << process.error();
    

    inside my open_program() Slot instead of connecting to the slots (I don't know how exactly)?!

    If I do that I get the qDebug-Output:

    QIODevice::read (QProcess): device not open
    ""
    QIODevice::read (QProcess): device not open
    ""
    QProcess::UnknownError

    So as far as I know Unknown Error == no error and the program doesn't even start right?
    I also think my problem has something to do with the deployment of the other programs.
    I think I will build them again (I built them some months ago with an older Qt version) and deploy them again.

    Christian EhrlicherC 1 Reply Last reply
    0
    • Q Qt_Bob

      @Christian-Ehrlicher @JonB

      Can't I just use

          qDebug() << process.readAllStandardError();
          qDebug() << process.readAllStandardOutput();
          qDebug() << process.error();
      

      inside my open_program() Slot instead of connecting to the slots (I don't know how exactly)?!

      If I do that I get the qDebug-Output:

      QIODevice::read (QProcess): device not open
      ""
      QIODevice::read (QProcess): device not open
      ""
      QProcess::UnknownError

      So as far as I know Unknown Error == no error and the program doesn't even start right?
      I also think my problem has something to do with the deployment of the other programs.
      I think I will build them again (I built them some months ago with an older Qt version) and deploy them again.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      You have to use start() and not the static version startDetached().
      On how to connect signals and slots (which you should learn when you really want to use Qt): https://doc.qt.io/qt-6/signalsandslots.html

      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
      3
      • Q Offline
        Q Offline
        Qt_Bob
        wrote on last edited by Qt_Bob
        #7

        @Christian-Ehrlicher
        Okay, that was pretty stupid of me.
        I want to get the stateChanged() signal or other signals before reading out.
        I've been using connect, signals and slots for a while now, but I'm still relatively new and it's taking me a while to understand the best way to set it up. I can't think of a good construct at the moment.

        I have now looked at it again in the dirtiest possible way (QTimer, which outputs the following every 5 ms):

        qDebug() << process.readAllStandardError();
                    qDebug() << process.readAllStandardOutput();
                    qDebug() << process.error();
                    qDebug() << process.state();
                    qDebug() << MainWindow::counter;
        

        The counter is just for counting the cycles.
        With that I still get no output, besides
        QProcess::Running
        and after ~1.5 s
        QProcess::NotRunning

        No errors or output.

        I will get more into the documentation and write it the way you mentioned.
        It may take a while, but I'll get back to you here.
        Thanks.

        P.S. I changed from startDetached() to start() for this, but the connection I mentioned wasn't working. I'm trying to get the code you mentioned done toda until 5 o' clock. If not, it may take until 4.12. until my next update. Sorry for that =/

        JonBJ 1 Reply Last reply
        0
        • Q Qt_Bob

          @Christian-Ehrlicher
          Okay, that was pretty stupid of me.
          I want to get the stateChanged() signal or other signals before reading out.
          I've been using connect, signals and slots for a while now, but I'm still relatively new and it's taking me a while to understand the best way to set it up. I can't think of a good construct at the moment.

          I have now looked at it again in the dirtiest possible way (QTimer, which outputs the following every 5 ms):

          qDebug() << process.readAllStandardError();
                      qDebug() << process.readAllStandardOutput();
                      qDebug() << process.error();
                      qDebug() << process.state();
                      qDebug() << MainWindow::counter;
          

          The counter is just for counting the cycles.
          With that I still get no output, besides
          QProcess::Running
          and after ~1.5 s
          QProcess::NotRunning

          No errors or output.

          I will get more into the documentation and write it the way you mentioned.
          It may take a while, but I'll get back to you here.
          Thanks.

          P.S. I changed from startDetached() to start() for this, but the connection I mentioned wasn't working. I'm trying to get the code you mentioned done toda until 5 o' clock. If not, it may take until 4.12. until my next update. Sorry for that =/

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

          @Qt_Bob
          If output shows running and then not running a bit later maybe it has run to conclusion successfully with no output? How do we know what program you run and what it's supposed to do or how you know it is not working right?

          Let's be 100% clear: you must be using start() here, do not even try to use startDetached(). If you cannot get that to work something is wrong.

          Your code above should either be in readyRead.../finished/errorOccurred/stateChanged slots, or for the purpose of your test you can just go:

          process.start(...);
          qDebug() << process.waitForFinished();
          

          followed by your code. Find out why your 2 programs "do not work".

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            Qt_Bob
            wrote on last edited by Qt_Bob
            #9

            @JonB @Christian-Ehrlicher

            Ok, I hope this is ok.
            At least it's compiling:

            mainwindow.h

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            #include <QPushButton>
            #include <QGridLayout>
            #include <QProcess>
            #include <QTextBrowser>
            #include <QByteArray>
            #include <QDebug>
            #include <QTimer>
            
            QT_BEGIN_NAMESPACE
            namespace Ui { class MainWindow; }
            QT_END_NAMESPACE
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
            
            private:
                Ui::MainWindow *ui;
                QProcess *process;
            
            private slots:
                void open_program();
                void ausgabe();
            };
            #endif // MAINWINDOW_H
            

            mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)
                , ui(new Ui::MainWindow)
            {
                ui->setupUi(this);
                QPushButton *but_open = new QPushButton("Open");
                QGridLayout *layout = new QGridLayout();
                layout->addWidget(but_open, 0, 0);
                ui->centralwidget->setLayout(layout);
                connect(but_open,SIGNAL(clicked()),this,SLOT(open_program()));
            }
            
            MainWindow::~MainWindow()
            {
                delete ui;
            }
            
            void MainWindow::open_program()
            {
                MainWindow::process = new QProcess();
                QString pfad = "I:\\FabTech\\Anlagen-Dokumentation\\85 TEOS 200 mm\\Teos-Trapping\\TEOS-Überwachung\\TEOS-Abscheidungs-Programm\\TEOS_Abscheidung.exe";
                process->start(pfad);
                connect(MainWindow::process,&QProcess::stateChanged,this,MainWindow::ausgabe);
                connect(MainWindow::process,&QProcess::errorOccurred,this,MainWindow::ausgabe);
            }
            
            void MainWindow::ausgabe()
            {
                qDebug() << MainWindow::process->readAllStandardError();
                qDebug() << MainWindow::process->readAllStandardOutput();
                qDebug() << MainWindow::process->error();
                qDebug() << MainWindow::process->state();
            }
            
            

            No changes to main.cpp

            Only output I got:
            ""
            ""
            QProcess::UnknownError
            QProcess::NotRunning

            mhhh... So in the next step I build and deploy the other programs again.
            They are a bit complex.
            I have a double-chained list in which data is read from a database and output in a text window.
            The user can then enter all the data for a physical process in some QSpinBoxes, QLineEdits and so on and these are added to the database. Or the database can be searched for specific entries and so on. In addition, the open database can be archived under a specific name and archived databases can be opened, etc.
            In any case, the program should remain open and wait for user input.

            I can open multiple instances of the programs I want to open with my new program from Windows and never had errors or crashes (even while the new program is running). Just open it from the new program doesn't work.

            Christian EhrlicherC 1 Reply Last reply
            0
            • Q Qt_Bob

              @JonB @Christian-Ehrlicher

              Ok, I hope this is ok.
              At least it's compiling:

              mainwindow.h

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              #include <QPushButton>
              #include <QGridLayout>
              #include <QProcess>
              #include <QTextBrowser>
              #include <QByteArray>
              #include <QDebug>
              #include <QTimer>
              
              QT_BEGIN_NAMESPACE
              namespace Ui { class MainWindow; }
              QT_END_NAMESPACE
              
              class MainWindow : public QMainWindow
              {
                  Q_OBJECT
              
              public:
                  MainWindow(QWidget *parent = nullptr);
                  ~MainWindow();
              
              private:
                  Ui::MainWindow *ui;
                  QProcess *process;
              
              private slots:
                  void open_program();
                  void ausgabe();
              };
              #endif // MAINWINDOW_H
              

              mainwindow.cpp

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent)
                  , ui(new Ui::MainWindow)
              {
                  ui->setupUi(this);
                  QPushButton *but_open = new QPushButton("Open");
                  QGridLayout *layout = new QGridLayout();
                  layout->addWidget(but_open, 0, 0);
                  ui->centralwidget->setLayout(layout);
                  connect(but_open,SIGNAL(clicked()),this,SLOT(open_program()));
              }
              
              MainWindow::~MainWindow()
              {
                  delete ui;
              }
              
              void MainWindow::open_program()
              {
                  MainWindow::process = new QProcess();
                  QString pfad = "I:\\FabTech\\Anlagen-Dokumentation\\85 TEOS 200 mm\\Teos-Trapping\\TEOS-Überwachung\\TEOS-Abscheidungs-Programm\\TEOS_Abscheidung.exe";
                  process->start(pfad);
                  connect(MainWindow::process,&QProcess::stateChanged,this,MainWindow::ausgabe);
                  connect(MainWindow::process,&QProcess::errorOccurred,this,MainWindow::ausgabe);
              }
              
              void MainWindow::ausgabe()
              {
                  qDebug() << MainWindow::process->readAllStandardError();
                  qDebug() << MainWindow::process->readAllStandardOutput();
                  qDebug() << MainWindow::process->error();
                  qDebug() << MainWindow::process->state();
              }
              
              

              No changes to main.cpp

              Only output I got:
              ""
              ""
              QProcess::UnknownError
              QProcess::NotRunning

              mhhh... So in the next step I build and deploy the other programs again.
              They are a bit complex.
              I have a double-chained list in which data is read from a database and output in a text window.
              The user can then enter all the data for a physical process in some QSpinBoxes, QLineEdits and so on and these are added to the database. Or the database can be searched for specific entries and so on. In addition, the open database can be archived under a specific name and archived databases can be opened, etc.
              In any case, the program should remain open and wait for user input.

              I can open multiple instances of the programs I want to open with my new program from Windows and never had errors or crashes (even while the new program is running). Just open it from the new program doesn't work.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Then please try to start the not working program from a plain command line. Maybe some dlls are missing so it can not be started.

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

              Q 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                Then please try to start the not working program from a plain command line. Maybe some dlls are missing so it can not be started.

                Q Offline
                Q Offline
                Qt_Bob
                wrote on last edited by
                #11

                @Christian-Ehrlicher
                It opens normally when I execute it from cmd. So that doesn't seem to be the problem?!

                Christian EhrlicherC 1 Reply Last reply
                0
                • Q Qt_Bob

                  @Christian-Ehrlicher
                  It opens normally when I execute it from cmd. So that doesn't seem to be the problem?!

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @Qt_Bob Make sure the PATH env var is the same. You can get it from QProcess::systemEnvironment() and on the command line with 'set PATH'

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

                  Q 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @Qt_Bob Make sure the PATH env var is the same. You can get it from QProcess::systemEnvironment() and on the command line with 'set PATH'

                    Q Offline
                    Q Offline
                    Qt_Bob
                    wrote on last edited by Qt_Bob
                    #13

                    @Christian-Ehrlicher
                    That could be it, I was editing the PATH env var to set up QwtPlot last week.
                    If I get the list from QProcess::systemEnvironment() it shows all the content in PATH. What should I look out for? The QT folder and MinGW and so on are set up correctly in any case. I spent a few hours on this to get QwtPlot run and make sure everything there is correct (I wanted to use python first for my new project,...). Should the PATH env var also contain something about the two programs?

                    Edit:
                    Ok I figured it out. I let it print both QProcess::systemEnvironment() and they are the same.

                    JonBJ 1 Reply Last reply
                    0
                    • Q Qt_Bob

                      @Christian-Ehrlicher
                      That could be it, I was editing the PATH env var to set up QwtPlot last week.
                      If I get the list from QProcess::systemEnvironment() it shows all the content in PATH. What should I look out for? The QT folder and MinGW and so on are set up correctly in any case. I spent a few hours on this to get QwtPlot run and make sure everything there is correct (I wanted to use python first for my new project,...). Should the PATH env var also contain something about the two programs?

                      Edit:
                      Ok I figured it out. I let it print both QProcess::systemEnvironment() and they are the same.

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

                      @Qt_Bob
                      Make sure your sub-programs do not care what the current directory is when run.

                      Put a qDebug() << "Hi there"; as first statement in sub-program, do you read that back successfully?

                      Make sub-program non-Qt, just plain Hello World, does that work?

                      Q 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Qt_Bob
                        Make sure your sub-programs do not care what the current directory is when run.

                        Put a qDebug() << "Hi there"; as first statement in sub-program, do you read that back successfully?

                        Make sub-program non-Qt, just plain Hello World, does that work?

                        Q Offline
                        Q Offline
                        Qt_Bob
                        wrote on last edited by
                        #15

                        @JonB
                        I have made a note of this next step in my diary and will carry it out as soon as possible and report back here. However, I will probably not be able to reply until 4 December. I'm sorry, I opened this topic at an inopportune time. One of the successfully tested programmes was pure C# without Qt and it worked. The other programmes were also developed in-house (Visual Basic, Pascal, ...). Apparently only the Qt programmes cause problems, but I will test this again with a "Hello World" programme with and without Qt.
                        Many thanks for the help.

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • Q Qt_Bob

                          @JonB
                          I have made a note of this next step in my diary and will carry it out as soon as possible and report back here. However, I will probably not be able to reply until 4 December. I'm sorry, I opened this topic at an inopportune time. One of the successfully tested programmes was pure C# without Qt and it worked. The other programmes were also developed in-house (Visual Basic, Pascal, ...). Apparently only the Qt programmes cause problems, but I will test this again with a "Hello World" programme with and without Qt.
                          Many thanks for the help.

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          @Qt_Bob said in QProcess::startDetached() doesn't work on self-written programs:

                          Apparently only the Qt programmes cause problems

                          This sounds like a deployment issue.

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

                          Q 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            @Qt_Bob said in QProcess::startDetached() doesn't work on self-written programs:

                            Apparently only the Qt programmes cause problems

                            This sounds like a deployment issue.

                            Q Offline
                            Q Offline
                            Qt_Bob
                            wrote on last edited by
                            #17

                            @Christian-Ehrlicher, @JonB
                            I deployed the two programs again and had the same problem as before. Then I went through all the deployed .dll's and there were some missing. I copied them inside the folder manually and now it works.
                            So the problem was windeployqt or some missing windeployqt-commands while deploying.
                            Thank you again, I'll close this topic now.

                            1 Reply Last reply
                            0
                            • Q Qt_Bob has marked this topic as solved on

                            • Login

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