How use QProcess in C++ class
-
Is this a correct way to handle a QProcess as member of a class?
I think that I should do this in this case instead of creating one every time that I call the function that uses it.class MyClass : public QAbstractListModel { Q_OBJECT public: Project(QObject *parent = nullptr) : QAbstractListModel(parent), m_manager(new QNetworkAccessManager(this)), m_process(nullptr) { connect(m_manager, &QNetworkAccessManager::finished, this, &Project::downloadFinished); } ... private: QVector<Data> m_data; QNetworkAccessManager* m_manager; QProcess* m_process; };In one function called from QML I start a process:
void MyClass::doSomething(const QString& cmd, const QStringList& cmdArgs) { m_process = new QProcess(this); m_process->start(cmd, cmdArgs); }I did not test this code because until now I was creating a QProcess directly in the function.
When I call the function I do not care to what happen when the process ends.
I'd like to check if the process is already running in case the function is called quickly more times.
In that case the already running process should be stopped before running the new one.And I do not want the function to block QML and the rest of the application.
Therefore should I useQProcess::setProgram,QProcess::setArgumentsandQProcess::startDetached? -
Is this a correct way to handle a QProcess as member of a class?
I think that I should do this in this case instead of creating one every time that I call the function that uses it.class MyClass : public QAbstractListModel { Q_OBJECT public: Project(QObject *parent = nullptr) : QAbstractListModel(parent), m_manager(new QNetworkAccessManager(this)), m_process(nullptr) { connect(m_manager, &QNetworkAccessManager::finished, this, &Project::downloadFinished); } ... private: QVector<Data> m_data; QNetworkAccessManager* m_manager; QProcess* m_process; };In one function called from QML I start a process:
void MyClass::doSomething(const QString& cmd, const QStringList& cmdArgs) { m_process = new QProcess(this); m_process->start(cmd, cmdArgs); }I did not test this code because until now I was creating a QProcess directly in the function.
When I call the function I do not care to what happen when the process ends.
I'd like to check if the process is already running in case the function is called quickly more times.
In that case the already running process should be stopped before running the new one.And I do not want the function to block QML and the rest of the application.
Therefore should I useQProcess::setProgram,QProcess::setArgumentsandQProcess::startDetached?@realroot
You create and leak a newQProcessinstance each timedoSomething()is called.You sound like you want
startDetached(). I don't know whether theQProcessthen tells you whether it's still running.in case the function is called quickly more times
You can sort that out from code.
"Stopping" running programs means killing them. This is generally not a great idea.
You could perhaps still use
start(). I think you'll need to keep aQList<QProcess *>since you want to run multiple processes at a time (right?) and you still want the handles to the running ones. -
@realroot
You create and leak a newQProcessinstance each timedoSomething()is called.You sound like you want
startDetached(). I don't know whether theQProcessthen tells you whether it's still running.in case the function is called quickly more times
You can sort that out from code.
"Stopping" running programs means killing them. This is generally not a great idea.
You could perhaps still use
start(). I think you'll need to keep aQList<QProcess *>since you want to run multiple processes at a time (right?) and you still want the handles to the running ones.Using
startDetached()application flow is not interrupted.void MyClass::doSomething(const QString& cmd, const QStringList& cmdArgs) { QProcess process; process.setProgram(cmd); process.setArguments(cmdArgs); process.startDetached(); }Actually I think that that there is no need to handle them at least for now.
-
R realroot has marked this topic as solved on