<?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 expose a normal function to QML?]]></title><description><![CDATA[<p dir="auto">I’m working on a database project written in C that exposes many plain C functions for running queries. We’re now building a QML front end and I want the simplest, lowest-effort way to integrate the two.</p>
<p dir="auto">I’ve seen forum posts recommending middleware classes that use the <code>Q_OBJECT</code> macro and <code>Q_INVOKABLE</code> methods, registered with the QML engine. Before I commit to writing hundreds of thin wrapper classes, is there a way to call plain, non-class member C functions directly from QML? (These functions aren’t attached to any class.)</p>
]]></description><link>https://forum.qt.io/topic/163126/how-to-expose-a-normal-function-to-qml</link><generator>RSS for Node</generator><lastBuildDate>Fri, 30 Oct 2026 17:44:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/163126.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 01 Sep 2025 16:34:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to expose a normal function to QML? on Fri, 05 Sep 2025 05:37:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> said in <a href="/post/831106">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto">Do you need to expose every single C function directly to your GUI? Perhaps you could design your wrapper class as an abstraction layer that exposes fewer but higher-level methods to QML, while these higher-level functions each call multiple lower-level C functions.</p>
</blockquote>
<p dir="auto">You are absolutely right that not all functions need to be exposed to the GUI layer. However, there are still a significant number of functions that do need to be invoked directly from the GUI. For that reason, I believe your proposed solution of creating a controller or wrapper unit with automated C++ generation is indeed the best approach. That said, I’ll continue researching to see if there are any alternative technologies that might be a better fit for my situation.</p>
]]></description><link>https://forum.qt.io/post/831216</link><guid isPermaLink="true">https://forum.qt.io/post/831216</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 05 Sep 2025 05:37:00 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Tue, 02 Sep 2025 14:04:10 GMT]]></title><description><![CDATA[<p dir="auto">@Saviz said in <a href="/post/831077">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto">Before I commit to writing hundreds of thin wrapper classes, is there a way to call plain, non-class member C functions directly from QML? (These functions aren’t attached to any class.)</p>
</blockquote>
<ol>
<li>As others have pointed out, at least one class with <code>Q_OBJECT</code> or <code>Q_GADGET</code> is required.</li>
<li>You don't need hundreds of classes. You just need one class to hold your many methods/functions (or perhaps a few classes, if you want to organize/group your functions)</li>
<li>Do you need to expose every single C function directly to your GUI? Perhaps you could design your wrapper class as an abstraction layer that exposes fewer but higher-level methods to QML, while these higher-level functions each call multiple lower-level C functions.</li>
<li>If you do want to expose hundreds of C functions directly to QML, then it could make sense to write a wrapper script to read your C header and generate the corresponding wrapper C++ method.</li>
</ol>
]]></description><link>https://forum.qt.io/post/831106</link><guid isPermaLink="true">https://forum.qt.io/post/831106</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Tue, 02 Sep 2025 14:04:10 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Tue, 02 Sep 2025 05:41:59 GMT]]></title><description><![CDATA[<p dir="auto">@Saviz said in <a href="/post/831090">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kh-219design">@<bdi>KH-219Design</bdi></a> Thank you for taking my concern seriously. I’m surprised this topic isn’t covered in more depth.</p>
<p dir="auto">It feels unreasonable that exposing a simple function to QML requires such a rigid, inconvenient structure. Why does everything need to be a class and include some kind of <code>Q_OBJECT</code> / <code>Q_GADET</code> macro ? If that’s truly the only option, I may have to consider alternatives to QML. It would be simply too much work to create layers just for exposing these functions to the GUI.</p>
</blockquote>
<p dir="auto">QML = <a href="https://en.wikipedia.org/wiki/QML" target="_blank" rel="noopener noreferrer nofollow ugc">Qt Meta-object Language</a>.<br />
The meta-object system enables transforming strings in the QML source text into properties (including functions) in a C++ object at runtime. Q_OBJECT and Q_GADGET macros expand into support code, and tell moc to generate corresponding meta-objects.</p>
<p dir="auto">Perhaps you can get what you seek from the <a href="https://doc.qt.io/qt-6/qjsengine.html" target="_blank" rel="noopener noreferrer nofollow ugc">javascript engine</a>. I've never felt the need to investigate that route.</p>
]]></description><link>https://forum.qt.io/post/831102</link><guid isPermaLink="true">https://forum.qt.io/post/831102</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Tue, 02 Sep 2025 05:41:59 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 19:52:42 GMT]]></title><description><![CDATA[<p dir="auto">@Saviz said in <a href="/post/831090">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto">It feels unreasonable that exposing a simple function to QML requires such a rigid, inconvenient structure. Why does everything need to be a class and include some kind of <code>Q_OBJECT</code> / <code>Q_GADET</code> macro ? If that’s truly the only option, I may have to consider alternatives to QML. It would be simply too much work to create layers just for exposing these functions to the GUI.</p>
</blockquote>
<p dir="auto">That's not an uncommon sentiment among people who are evaluating Qt (and in all candor, is likely one of the reasons Qt doesn't have a broader following). Qt is definitely not a "toe in the water" framework; it's more a "hold your nose and jump in" deal. If your C functions truly measure in the hundreds, you are indeed looking at a big interface layer, though it could probably be handled in a single (large) controller object.</p>
<p dir="auto">But yeah...Qt might not be the right tool for your application.</p>
]]></description><link>https://forum.qt.io/post/831093</link><guid isPermaLink="true">https://forum.qt.io/post/831093</guid><dc:creator><![CDATA[mzimmers]]></dc:creator><pubDate>Mon, 01 Sep 2025 19:52:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 19:43:54 GMT]]></title><description><![CDATA[<blockquote>
<p dir="auto">database project written in C that exposes <strong>many</strong> plain C functions for running queries.</p>
</blockquote>
<p dir="auto">(bold-font emphasis is my own)</p>
<p dir="auto">Yes. I empathize.</p>
<p dir="auto">A lot hinges on the magnitude of your "<em>many</em>" quantifier in the original problem description.</p>
<p dir="auto">I suspect that beyond some threshold it would be reasonable to write an automated wrapper-generator system rather than wrap each C header manually. For loose inspiration on wrapper-generator systems, I'm thinking something like <a href="https://docs.python.org/3/library/ctypes.html" target="_blank" rel="noopener noreferrer nofollow ugc">ctypes</a> (C-to-python) or <a href="https://github.com/woboq/qmetaobject-rs" target="_blank" rel="noopener noreferrer nofollow ugc">QMetaObject-rs</a> (Rust-to-QML).</p>
<p dir="auto">Come to think of it, maybe you could use <code>ctypes</code> to go from C to Python, then use PySide (Qt Python) for the GUI?** That's still a non-zero amount of work, but I give it pretty favorable odds of being less work than wrapping all your C for Q_GADGET. I guess the trade-off is roughly that you would end up with <code>py</code> files as your "glue" between C and QML, instead of <code>C++</code> files as your glue between C and QML. Pick your poison? 🧪🧙😈</p>
<p dir="auto">**It isn't incredibly well known, but a GUI in PySide is perfectly able to support QML.</p>
]]></description><link>https://forum.qt.io/post/831092</link><guid isPermaLink="true">https://forum.qt.io/post/831092</guid><dc:creator><![CDATA[KH-219Design]]></dc:creator><pubDate>Mon, 01 Sep 2025 19:43:54 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 19:14:33 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kh-219design">@<bdi>KH-219Design</bdi></a> Thank you for taking my concern seriously. I’m surprised this topic isn’t covered in more depth.</p>
<p dir="auto">It feels unreasonable that exposing a simple function to QML requires such a rigid, inconvenient structure. Why does everything need to be a class and include some kind of <code>Q_OBJECT</code> / <code>Q_GADET</code> macro ? If that’s truly the only option, I may have to consider alternatives to QML. It would be simply too much work to create layers just for exposing these functions to the GUI.</p>
]]></description><link>https://forum.qt.io/post/831090</link><guid isPermaLink="true">https://forum.qt.io/post/831090</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Mon, 01 Sep 2025 19:14:33 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 18:34:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kh-219design">@<bdi>KH-219Design</bdi></a> said in <a href="/post/831083">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> said in <a href="/post/831082">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto">Needing a hundreds of imperative functions sounds like a design in need of better compartmentalization of logic.</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> , please note that @Saviz is dealing with a "plain C" codebase. There are essentially no other options but imperative functions available under that scenario.</p>
</blockquote>
<p dir="auto">As <a class="plugin-mentions-user plugin-mentions-a" href="/user/mzimmers">@<bdi>mzimmers</bdi></a> noted, there is no support for Q_INVOKABLE outside of a QObject or Q_GADGET C++ class. I think it's reasonable to presume that OP and the general audience will understand as much at this point in the conversation.</p>
<p dir="auto">Once we are at that point, it's wise to consider other mechanisms to ease the burden.</p>
]]></description><link>https://forum.qt.io/post/831085</link><guid isPermaLink="true">https://forum.qt.io/post/831085</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Mon, 01 Sep 2025 18:34:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 18:12:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> said in <a href="/post/831082">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto">Needing a hundreds of imperative functions sounds like a design in need of better compartmentalization of logic.</p>
</blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> , please note that @Saviz is dealing with a "plain C" codebase. There are essentially no other options but imperative functions available under that scenario.</p>
<p dir="auto">I'm subscribed to this question/thread because I am also interested if others have found "the shortest path" (more specifically: the most concise path) to wrapping a free function for QML.</p>
<p dir="auto">Use of a C++ object will most likely be required (but again: I, too, await any insights from others, since I have not explored this topic in depth).</p>
<p dir="auto">However, one bit of good news may be the possibility to use a C++ object that does <em>not</em> inherit <code>QObject</code>. That could be possible via the "q_gadget" concept:  <a href="https://doc.qt.io/qt-6/qobject.html#Q_GADGET" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qobject.html#Q_GADGET</a></p>
<p dir="auto">per the prior link: <em>"<code>Q_GADGETs</code> can have Q_ENUM, Q_PROPERTY and <code>Q_INVOKABLE</code>, but they cannot have signals or slots."</em></p>
]]></description><link>https://forum.qt.io/post/831083</link><guid isPermaLink="true">https://forum.qt.io/post/831083</guid><dc:creator><![CDATA[KH-219Design]]></dc:creator><pubDate>Mon, 01 Sep 2025 18:12:18 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 18:00:41 GMT]]></title><description><![CDATA[<p dir="auto">@Saviz said in <a href="/post/831077">How to expose a normal function to QML?</a>:</p>
<blockquote>
<p dir="auto">I’m working on a database project written in C that exposes many plain C functions for running queries. We’re now building a QML front end and I want the simplest, lowest-effort way to integrate the two.</p>
<p dir="auto">I’ve seen forum posts recommending middleware classes that use the <code>Q_OBJECT</code> macro and <code>Q_INVOKABLE</code> methods, registered with the QML engine. Before I commit to writing hundreds of thin wrapper classes, is there a way to call plain, non-class member C functions directly from QML? (These functions aren’t attached to any class.)</p>
</blockquote>
<p dir="auto">Does the function return a value? A there cleanup function or other funtions that can map to transitions in a UI?</p>
<p dir="auto">While Q_INVOKABLE almost certainly involves the least code to implement, use usually doesn't fit in with the declarative style of QML. Needing a hundreds of imperative functions sounds like a design in need of better compartmentalization of logic.</p>
]]></description><link>https://forum.qt.io/post/831082</link><guid isPermaLink="true">https://forum.qt.io/post/831082</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Mon, 01 Sep 2025 18:00:41 GMT</pubDate></item><item><title><![CDATA[Reply to How to expose a normal function to QML? on Mon, 01 Sep 2025 16:42:30 GMT]]></title><description><![CDATA[<p dir="auto">Qt is intended to be used by C++, not C. The macros you mentioned are intended for classes derived from QObject. Can you convert your project to C++, or at least create a hybrid C/C++ project so you can instantiate Qt classes?</p>
]]></description><link>https://forum.qt.io/post/831078</link><guid isPermaLink="true">https://forum.qt.io/post/831078</guid><dc:creator><![CDATA[mzimmers]]></dc:creator><pubDate>Mon, 01 Sep 2025 16:42:30 GMT</pubDate></item></channel></rss>