<?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 to pass cmd line arguments to a running instance of mainwindow.]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I am trying to find a way how to send command line arguments to an already running instance. Here is a minimal example - which works on initial launch.<br />
But how to send those arguments to MainWindow if this is already active?</p>
<p dir="auto">QtSingleApplication seems to be gone. What is the way to do this?<br />
The 2 examples in this forum did not work for me.</p>
<pre><code>#include &lt;QApplication&gt;
#include &lt;mainwindow.h&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    MainWindow w;
    w.show();
    if(argc &gt; 1) {
        QStringList arguments = a.arguments();
        w.processArguments(arguments);
    }
    a.exec();
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/145894/how-to-pass-cmd-line-arguments-to-a-running-instance-of-mainwindow</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 02:44:13 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/145894.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 20 Jun 2023 07:20:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 20:50:34 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Are you looking for <a href="https://doc.qt.io/qt-6/qfileopenevent.html" target="_blank" rel="noopener noreferrer nofollow ugc">QFileOpenEvent</a> ?</p>
]]></description><link>https://forum.qt.io/post/762125</link><guid isPermaLink="true">https://forum.qt.io/post/762125</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 20 Jun 2023 20:50:34 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Fri, 23 Jun 2023 15:04:55 GMT]]></title><description><![CDATA[<p dir="auto">Hi all, thx to everybody for helping.</p>
<p dir="auto">I mark <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> answer as the right one, because he made me investigate again and deeper into QFileOpenEvent.</p>
<p dir="auto">From this nice tutorial - at the very end - I have got the details, which are<br />
needed to get QFileOpenEvent work on MacOS: <a href="https://youtu.be/o7Z0riL2kgE" target="_blank" rel="noopener noreferrer nofollow ugc">https://youtu.be/o7Z0riL2kgE</a></p>
<p dir="auto"><strong>After overwriting QApplication you can do this in main.cpp</strong></p>
<pre><code>#include &lt;mainwindow.h&gt;
#include "myapplication.h"

int main(int argc, char *argv[])
{
    MyApplication a(argc, argv);
    auto arguments = a.arguments();

    MainWindow w;
    w.show();

#ifdef Q_OS_MACOS
    QObject::connect(&amp;a, &amp;MyApplication::openFile,
                     &amp;w, &amp;MainWindow::messageSend);
#else
    w.messageSend(arguments.at(1));
#endif

    return a.exec();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/762417</link><guid isPermaLink="true">https://forum.qt.io/post/762417</guid><dc:creator><![CDATA[ademmler]]></dc:creator><pubDate>Fri, 23 Jun 2023 15:04:55 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Wed, 21 Jun 2023 09:33:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ademmler">@<bdi>ademmler</bdi></a> I think <code>QtSingleApplication</code> is more suitable for that.<br />
I had written code to make "double click" activate the existing instance's window, which was requested by the client.<br />
Something like</p>
<pre><code>    if(!listen())
        return 0;
    MainWindow w;
    w.show();
    return app.exec();
</code></pre>
<p dir="auto">In the "listen()" part I try to connect a <code>QLocalServer</code> with certain name. If connected, I'll send some information to it (in you case you can send the arguments) and return false. If such server is not found,  I'll create one, make it listening and return true.<br />
<code>QtSingleApplication</code> seems to have similar design.</p>
]]></description><link>https://forum.qt.io/post/762187</link><guid isPermaLink="true">https://forum.qt.io/post/762187</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Wed, 21 Jun 2023 09:33:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Wed, 21 Jun 2023 09:11:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/simonschroeder">@<bdi>SimonSchroeder</bdi></a></p>
<p dir="auto">in my solution I can create multiple instances of the main window. The reason is by software concept. All processing parameters are bound to the window . It is like "Workspaces" with a fixed set of settings.</p>
]]></description><link>https://forum.qt.io/post/762180</link><guid isPermaLink="true">https://forum.qt.io/post/762180</guid><dc:creator><![CDATA[ademmler]]></dc:creator><pubDate>Wed, 21 Jun 2023 09:11:58 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Wed, 21 Jun 2023 09:13:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a>  thx for your response</p>
<p dir="auto">yes I did - still struggling to get this work<br />
Let me send a minimal example later.</p>
<p dir="auto">But this is mac only - right?<br />
Does it need separate implementation for windows than?</p>
<p dir="auto">An I am wondering if i really need to implement</p>
<ul>
<li>QtSingleApplication</li>
<li>FileOpenEvent</li>
<li>qApp-Arguments</li>
</ul>
<p dir="auto">To get a simple "Double Click" or "Drag to Icon" experience. Looks like this.</p>
]]></description><link>https://forum.qt.io/post/762179</link><guid isPermaLink="true">https://forum.qt.io/post/762179</guid><dc:creator><![CDATA[ademmler]]></dc:creator><pubDate>Wed, 21 Jun 2023 09:13:44 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Wed, 21 Jun 2023 07:23:18 GMT]]></title><description><![CDATA[<p dir="auto">This reminds me of the old days when applications still used MDI (multiple documents interface). Qt seems to still support this. It is probably not what you want, but this would have the same kind of problem to open files inside an existing window. So, searching for MDI explicitly might give you some pointers on how to handle this.</p>
]]></description><link>https://forum.qt.io/post/762164</link><guid isPermaLink="true">https://forum.qt.io/post/762164</guid><dc:creator><![CDATA[SimonSchroeder]]></dc:creator><pubDate>Wed, 21 Jun 2023 07:23:18 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 20:50:34 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Are you looking for <a href="https://doc.qt.io/qt-6/qfileopenevent.html" target="_blank" rel="noopener noreferrer nofollow ugc">QFileOpenEvent</a> ?</p>
]]></description><link>https://forum.qt.io/post/762125</link><guid isPermaLink="true">https://forum.qt.io/post/762125</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 20 Jun 2023 20:50:34 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 12:49:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> hi, you made a good question.</p>
<p dir="auto">Let me try to describe in my own words:<br />
main does initiate a "mainwindow" as an instance.</p>
<p dir="auto">When I double click a file or do some of the other actions above I want to open this file in exact this already open window.<br />
Hence somehow I need to tell the running instance about it. Right?</p>
<p dir="auto">As what already got this approach might differ on MacOS and Windows.</p>
<p dir="auto">I did what <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a> recommended and added this to my MainWindow routine.</p>
<pre><code>#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    QStringList arguments = qApp-&gt;arguments();
    if(arguments.size() &gt; 1) {
        qDebug() &lt;&lt; arguments.at(1);
        processArguments(arguments);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::processArguments(QStringList arguments)
{
    ui-&gt;plainTextEdit-&gt;clear();
    ui-&gt;plainTextEdit-&gt;appendPlainText(arguments.join("\n"));
}
</code></pre>
<p dir="auto">This work only on initial launch of the application,<br />
but not if the application is already running.</p>
]]></description><link>https://forum.qt.io/post/762072</link><guid isPermaLink="true">https://forum.qt.io/post/762072</guid><dc:creator><![CDATA[ademmler]]></dc:creator><pubDate>Tue, 20 Jun 2023 12:49:36 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 10:32:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ademmler">@<bdi>ademmler</bdi></a> said in <a href="/post/762058">How to pass cmd line arguments to a running instance of mainwindow.</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>  Dear JS. the use cases are:</p>
<p dir="auto">Double click a file on the desktop and get it open<br />
Drag a file on the application Icon and get it opened<br />
Use contact menu to send a file to this application to get it opened</p>
</blockquote>
<p dir="auto">But why you need a running instance for that?<br />
I know, there are some examples but from my experience, if you have your program running and you open a file connected to your program, in most cases a new window / instance of the program will start. (because it's a lot easier, I guess)</p>
]]></description><link>https://forum.qt.io/post/762065</link><guid isPermaLink="true">https://forum.qt.io/post/762065</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Tue, 20 Jun 2023 10:32:14 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 09:55:29 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bonnie">@<bdi>Bonnie</bdi></a>  thx I check on this too!</p>
]]></description><link>https://forum.qt.io/post/762059</link><guid isPermaLink="true">https://forum.qt.io/post/762059</guid><dc:creator><![CDATA[ademmler]]></dc:creator><pubDate>Tue, 20 Jun 2023 09:55:29 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 09:54:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>  Dear JS. the use cases are:</p>
<ul>
<li>Double click a file on the desktop and get it open</li>
<li>Drag a file on the application Icon and get it opened</li>
<li>Use contact menu to send a file to this application to get it opened</li>
</ul>
<p dir="auto">All those on macOS as OnWindows.</p>
<p dir="auto">Thx for the hint to    "qApp" - I gave it a try - and yes it delivers what I wanted.<br />
now I need to see how to achieve the above requirements.</p>
]]></description><link>https://forum.qt.io/post/762058</link><guid isPermaLink="true">https://forum.qt.io/post/762058</guid><dc:creator><![CDATA[ademmler]]></dc:creator><pubDate>Tue, 20 Jun 2023 09:54:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 08:44:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ademmler">@<bdi>ademmler</bdi></a> <code>QtSingleApplication</code>'s code is still available in <a href="https://code.qt.io/cgit/qt-solutions/qt-solutions.git" target="_blank" rel="noopener noreferrer nofollow ugc">https://code.qt.io/cgit/qt-solutions/qt-solutions.git</a> or <a href="https://github.com/qtproject/qt-solutions" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/qtproject/qt-solutions</a></p>
]]></description><link>https://forum.qt.io/post/762048</link><guid isPermaLink="true">https://forum.qt.io/post/762048</guid><dc:creator><![CDATA[Bonnie]]></dc:creator><pubDate>Tue, 20 Jun 2023 08:44:31 GMT</pubDate></item><item><title><![CDATA[Reply to How to pass cmd line arguments to a running instance of mainwindow. on Tue, 20 Jun 2023 08:12:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/ademmler">@<bdi>ademmler</bdi></a> said in <a href="/post/762032">How to pass cmd line arguments to a running instance of mainwindow.</a>:</p>
<blockquote>
<p dir="auto">But how to send those arguments to MainWindow if this is already active?</p>
</blockquote>
<p dir="auto">What is the use case?<br />
And if you really need to do so then you can do that at any time because you can get them from QApplication instance (<a href="https://doc.qt.io/qt-6/qapplication.html#qApp" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qapplication.html#qApp</a>) just like you do in the code snippet.</p>
]]></description><link>https://forum.qt.io/post/762042</link><guid isPermaLink="true">https://forum.qt.io/post/762042</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Tue, 20 Jun 2023 08:12:20 GMT</pubDate></item></channel></rss>