<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How use QProcess in C++ class]]></title><description><![CDATA[<p dir="auto">Is this a correct way to handle a QProcess as member of a class?<br />
I think that I should do this in this case instead of creating one every time that I call the function that uses it.</p>
<pre><code>class MyClass : public QAbstractListModel {
    Q_OBJECT

public:
    Project(QObject *parent = nullptr) : QAbstractListModel(parent),
        m_manager(new QNetworkAccessManager(this)), m_process(nullptr) {
        connect(m_manager, &amp;QNetworkAccessManager::finished,
                this, &amp;Project::downloadFinished);
    }

...

private:
    QVector&lt;Data&gt; m_data;
    QNetworkAccessManager* m_manager;
    QProcess* m_process;
};
</code></pre>
<p dir="auto">In one function called from QML I start a process:</p>
<pre><code>void MyClass::doSomething(const QString&amp; cmd, const QStringList&amp; cmdArgs) {
    m_process = new QProcess(this);
    m_process-&gt;start(cmd, cmdArgs);
}
</code></pre>
<p dir="auto">I did not test this code because until now I was creating a QProcess directly in the function.</p>
<p dir="auto">When I call the function I do not care to what happen when the process ends.<br />
I'd like to check if the process is already running in case the function is called quickly more times.<br />
In that case the already running process should be stopped before running the new one.</p>
<p dir="auto">And I do not want the function to block QML and the rest of the application.<br />
Therefore should I use <code>QProcess::setProgram</code>, <code>QProcess::setArguments</code> and <code>QProcess::startDetached</code>?</p>
]]></description><link>https://forum.qt.io/topic/159985/how-use-qprocess-in-c-class</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 22:39:49 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/159985.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 01 Dec 2024 15:17:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How use QProcess in C++ class on Sun, 01 Dec 2024 16:55:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/realroot">@<bdi>realroot</bdi></a><br />
You create and leak a new <code>QProcess</code> instance each time <code>doSomething()</code> is called.</p>
<p dir="auto">You sound like you want <code>startDetached()</code>.  I don't know whether the <code>QProcess</code> then tells you whether it's still running.</p>
<blockquote>
<p dir="auto">in case the function is called quickly more times</p>
</blockquote>
<p dir="auto">You can sort that out from code.</p>
<p dir="auto">"Stopping" running programs means killing them.  This is generally not a great idea.</p>
<p dir="auto">You could perhaps still use <code>start()</code>.  I think you'll need to keep a <code>QList&lt;QProcess *&gt;</code> since you want to run multiple processes at a time (right?) and you still want the handles to the running ones.</p>
]]></description><link>https://forum.qt.io/post/816017</link><guid isPermaLink="true">https://forum.qt.io/post/816017</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 01 Dec 2024 16:55:09 GMT</pubDate></item><item><title><![CDATA[Reply to How use QProcess in C++ class on Sun, 01 Dec 2024 19:43:31 GMT]]></title><description><![CDATA[<p dir="auto">Using <code>startDetached()</code> application flow is not interrupted.</p>
<pre><code>void MyClass::doSomething(const QString&amp; cmd, const QStringList&amp; cmdArgs) {
    QProcess process;
    process.setProgram(cmd);
    process.setArguments(cmdArgs);
    process.startDetached();
}
</code></pre>
<p dir="auto">Actually I think that that there is no need to handle them at least for now.</p>
]]></description><link>https://forum.qt.io/post/816031</link><guid isPermaLink="true">https://forum.qt.io/post/816031</guid><dc:creator><![CDATA[realroot]]></dc:creator><pubDate>Sun, 01 Dec 2024 19:43:31 GMT</pubDate></item><item><title><![CDATA[Reply to How use QProcess in C++ class on Sun, 01 Dec 2024 16:55:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/realroot">@<bdi>realroot</bdi></a><br />
You create and leak a new <code>QProcess</code> instance each time <code>doSomething()</code> is called.</p>
<p dir="auto">You sound like you want <code>startDetached()</code>.  I don't know whether the <code>QProcess</code> then tells you whether it's still running.</p>
<blockquote>
<p dir="auto">in case the function is called quickly more times</p>
</blockquote>
<p dir="auto">You can sort that out from code.</p>
<p dir="auto">"Stopping" running programs means killing them.  This is generally not a great idea.</p>
<p dir="auto">You could perhaps still use <code>start()</code>.  I think you'll need to keep a <code>QList&lt;QProcess *&gt;</code> since you want to run multiple processes at a time (right?) and you still want the handles to the running ones.</p>
]]></description><link>https://forum.qt.io/post/816017</link><guid isPermaLink="true">https://forum.qt.io/post/816017</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 01 Dec 2024 16:55:09 GMT</pubDate></item></channel></rss>