QProcess::startDetached() doesn't work on self-written programs
-
Hi,
I'm new to Qt and have a problem with my latest project.
I am using Windows 10, Qt 6.6.0 and MinGW_64.I have already written some programs and deployed them using the MinGW console and the command "windeployqt ." so that the dependencies are included and the programs can also be used on other (Windows) PCs.
These run without any problems and fulfill their purpose.
In my current project, I want to give the user the option of opening programs by pressing a QPush button. This works with all programs except the ones I wrote myself (7 others work, 2 self-written ones do not).Have I missed something?
Here is a minimal example, even if the problem is difficult to reproduce without the additional programs:
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QGridLayout> #include <QProcess> 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; private slots: void open_program(); }; #endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
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() { QProcess process; QString pfad = "I:\\FabTech\\Anlagen-Dokumentation\\85 TEOS 200 mm\\Teos-Trapping\\TEOS-Überwachung\\TEOS-Abscheidungs-Programm\\TEOS_Abscheidung.exe"; process.startDetached(pfad); }
I have also tried shorter paths (C:\test), which did not bring any change. I also use data from a subfolder of the program to be executed, which can be read and processed without any problems.
If there is no simple solution to the problem, I will probably leave out the integration of these programs and just work with the data, but maybe I have just overlooked something and there is a simple solution?!
Thanks in advance,
Qt_BobP.S.: I forgot to mention:
After pressing the QPush button, a window opens for a barely perceptible period of time and then closes again immediately. -
Don't use startDetached() but start() and connect to the appropirate error slots to maybe see what's going wrong.
-
@Qt_Bob
As @Christian-Ehrlicher says, begin withstart()
so that you can debug what is going on. Slot ontoerrorOccurred
as well asfinished
, and read anything which arrives on standard output or error (QProcess::readAll...()
). Windows/DOS often gives some informational output as to what might be wrong. When all is working you can go back tostartDetached()
for your users, but with that you cannot access errors so easily.If the only 2 which do not work are written by you and use Qt, the most likely guess is that they are not deployed properly enough, and have an issue when run from your other program.
-
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::UnknownErrorSo 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. -
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. -