QProcess::startDetached() doesn't work on self-written programs
-
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 -
@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::NotRunningNo 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 =/
-
@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 usestartDetached()
. 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".
-
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::NotRunningmhhh... 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.
-
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.
-
@Christian-Ehrlicher
It opens normally when I execute it from cmd. So that doesn't seem to be the problem?! -
@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'
-
@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. -
@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?
-
@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. -
@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.
-
@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. -