<?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[Checking if running as command-line (QCoreApplication) or GUI]]></title><description><![CDATA[<p dir="auto">Is it correct to write:</p>
<pre><code>if (qApp)
{
     // Do gui stuff
}
else
{
    // Do command line stuff
}
</code></pre>
<p dir="auto">Thanks, David</p>
]]></description><link>https://forum.qt.io/topic/165244/checking-if-running-as-command-line-qcoreapplication-or-gui</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 01:45:11 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/165244.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 06 Nov 2026 11:01:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Checking if running as command-line (QCoreApplication) or GUI on Fri, 06 Nov 2026 19:05:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/perdrix">@<bdi>Perdrix</bdi></a> said in <a href="/post/840800">Checking if running as command-line (QCoreApplication) or GUI</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I build a GUI application which instantiates a QApplication in its main(), and a completely different piece of code is used for the command line application which creates a QCoreApplication.</p>
<p dir="auto">So I think that qApp will work in that case?</p>
</blockquote>
<p dir="auto">Hi,</p>
<p dir="auto">Something is not clear: are you starting both the CLI and the GUI from the same main function ?</p>
]]></description><link>https://forum.qt.io/post/840803</link><guid isPermaLink="true">https://forum.qt.io/post/840803</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Fri, 06 Nov 2026 19:05:19 GMT</pubDate></item><item><title><![CDATA[Reply to Checking if running as command-line (QCoreApplication) or GUI on Fri, 06 Nov 2026 19:02:16 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/perdrix">@<bdi>Perdrix</bdi></a><br />
It may depend on platform or what you mean by command line.  Have you just tested <em>linking</em> your command line program with a piece of code which goes <code>if (qApp)</code> and verified the final executable is still a command-line/console application?</p>
]]></description><link>https://forum.qt.io/post/840802</link><guid isPermaLink="true">https://forum.qt.io/post/840802</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Nov 2026 19:02:16 GMT</pubDate></item><item><title><![CDATA[Reply to Checking if running as command-line (QCoreApplication) or GUI on Fri, 06 Nov 2026 15:55:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> I build a GUI application which instantiates a QApplication in its main(), and a completely different piece of code is used for the command line application which creates a QCoreApplication.</p>
<p dir="auto">So I think that qApp will work in that case?</p>
]]></description><link>https://forum.qt.io/post/840800</link><guid isPermaLink="true">https://forum.qt.io/post/840800</guid><dc:creator><![CDATA[Perdrix]]></dc:creator><pubDate>Fri, 06 Nov 2026 15:55:31 GMT</pubDate></item><item><title><![CDATA[Reply to Checking if running as command-line (QCoreApplication) or GUI on Fri, 06 Nov 2026 12:03:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/perdrix">@<bdi>Perdrix</bdi></a><br />
I imagine that is a reasonable test if you want to do a Qt call, as in: "is there a <code>QApplication</code> object?"</p>
<blockquote>
<p dir="auto">A global pointer referring to the unique application object. It is equivalent to QCoreApplication::instance(), but cast as a QApplication pointer, so only valid when the unique application object is a QApplication.</p>
</blockquote>
<p dir="auto">However, are you intending to use single-source with two separate compilations/links, one for GUI and one for console?  Because if you are thinking you can do just <em>one</em> compilation/link and decide at <em>runtime</em> whether to run as GUI or command line you are going to stall.  Under Windows at least --- which ISTR you use --- an executable is <em>linked</em> as either "windows" (GUI) or "console" (command-line) and you are stuck with that at runtime.</p>
]]></description><link>https://forum.qt.io/post/840794</link><guid isPermaLink="true">https://forum.qt.io/post/840794</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 06 Nov 2026 12:03:38 GMT</pubDate></item></channel></rss>