<?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[Do I need to add qmldir for python objects exposed as singleton instances?]]></title><description><![CDATA[<p dir="auto">I am working on vscode using the official extensions  and I am registering a singleton instance as:</p>
<pre><code>QtQml.qmlRegisterSingletonInstance(
        controllers.AppController,
        "TodoApp",
        1,
        0,
        "AppController",
        context.app_controller,
    )
</code></pre>
<p dir="auto">Now My AppController doesnt use @QmlElement or @QmlSingleton bc as far as I understand I dont need those while registering a singleton instance this way. Now on my qml file I get a warning as TodoApp cant be imported:</p>
<pre><code>Warnings occurred while importing module "TodoApp": [import]qmllint
Failed to import TodoApp. Are your import paths set up properly? Did you build your project? [import]qmllint
</code></pre>
<p dir="auto">That means that I dont get any autocompletion for my AppController and ofc the linter complains about that. Checking the output of the language server I can see:</p>
<pre><code>[Dom] Warning: Failed to find main qmldir file for TodoApp  in /usr/lib/qt6/qml/, /mnt/storage/Projects/todo/src/todo/frontend/qml_modules, /usr/lib/python3.14/site-packages/PySide6/Qt/qml, /home/runner/work/qmlls-workflow/qmlls-workflow/qt-build/qtbase/qml.
</code></pre>
<p dir="auto">So I am wondering if I need to create a qmldir for this TodoApp module (that is just a module at runtime) and if that will help the qmlls to recognize it and offer autocompletion. The code works just fine is just an issue with the linter/language server.</p>
]]></description><link>https://forum.qt.io/topic/164365/do-i-need-to-add-qmldir-for-python-objects-exposed-as-singleton-instances</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 19:16:40 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/164365.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 03 Mar 2026 11:15:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Do I need to add qmldir for python objects exposed as singleton instances? on Fri, 06 Mar 2026 13:39:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> I am trying to go through your answer step by step going from the simplest case and adding complexity. I am trying to use the decorators as you mentioned but I still cant get the qmllint or the language server to recognize my python types. The code works but in my qml file I will get a warning that the module can not be imported.</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> said in <a href="/post/836774">Do I need to add qmldir for python objects exposed as singleton instances?</a>:</p>
<blockquote>
<p dir="auto">In addition to QML instantiation, these decorators automatically generate the qmldir + *.qmltypes files. These files are read by qmllint and the QML Language Server (qmlls) to understand your Python types.</p>
</blockquote>
<p dir="auto">I am not sure how adding a decorator to my class can generate these files so the language server can recognize the types. I feel like I am missing something here related to the qt tools provided for python but I cant see what. Going through the documentation I have tried to use pyside6-project build but nothing really happens even if I dont get any errors. I tried using pyside6-project qmllint and get the same warnings. My qml file and the .py file where the @QmlElement decorated class lives have been added to the pyproject.toml. Still not *.qmltypes are generated in this process. Information about these tools seems scarce so I am wondering if I am missing something.</p>
]]></description><link>https://forum.qt.io/post/836876</link><guid isPermaLink="true">https://forum.qt.io/post/836876</guid><dc:creator><![CDATA[Munhz]]></dc:creator><pubDate>Fri, 06 Mar 2026 13:39:40 GMT</pubDate></item><item><title><![CDATA[Reply to Do I need to add qmldir for python objects exposed as singleton instances? on Thu, 05 Mar 2026 00:09:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/munhz">@<bdi>Munhz</bdi></a> said in <a href="/post/836768">Do I need to add qmldir for python objects exposed as singleton instances?</a>:</p>
<blockquote>
<p dir="auto">My understanding was that when I use <strong>@QmlElement</strong>/<strong>QmlSingleton</strong> I was making the types available for QML to instantiate.</p>
</blockquote>
<p dir="auto">Yes, <code>@QmlElement</code> allows your type to be instantiated in QML. <code>@QmlSingleton</code> is quite flexible: The QML engine could automatically instantiate your singleton, or you can make the engine use your existing instance by implementing a static <code>create()</code> method (see below).</p>
<p dir="auto">In addition to QML instantiation, these decorators automatically generate the qmldir + *.qmltypes files. These files are read by qmllint and the QML Language Server (qmlls) to understand your Python types.</p>
<blockquote>
<p dir="auto">In my case, I am creating an instance of my class on the python side that I am registering after, so I can inject some dependencies into it before the QML engine gets it.</p>
<p dir="auto">Are you saying that if I just use both decorators in my class, I can still instantiate it before loading the engine and then my QML code can use the instance I created on the python side?</p>
</blockquote>
<p dir="auto">If you define a static <code>create()</code> method (see <a href="https://doc.qt.io/qtforpython-6/PySide6/QtQml/QmlSingleton.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qtforpython-6/PySide6/QtQml/QmlSingleton.html</a>), the QML engine calls that method instead of automatically instantiating your type.</p>
<p dir="auto">You can instantiate your object in Python, do your injections, then initialize the QML engine. The engine will call <code>create()</code> at this point, so let <code>create()</code> return your existing object (you can ignore the "engine" parameter of the method if you only have one engine).</p>
<p dir="auto">The Python docs are a bit sparse; see the C++ docs to get an idea on how to use <code>create()</code>: <a href="https://doc.qt.io/qt-6/qml-singleton.html#defining-singletons-in-c" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qml-singleton.html#defining-singletons-in-c</a></p>
<blockquote>
<p dir="auto">So I am not sure if I would need to do smth else extra or this approach does not work for registering an instance of my class.</p>
</blockquote>
<p dir="auto">The newer <code>@QmlElement</code> and its friends are replacing the older <code>qmlRegister*()</code> functions. If you want to continue using <code>qmlRegister*()</code>, you'll need to manually write a qmldir file and *.qmltypes file to keep qmllint/qmlls happy. Use some auto-generated ones as a template.</p>
]]></description><link>https://forum.qt.io/post/836774</link><guid isPermaLink="true">https://forum.qt.io/post/836774</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Thu, 05 Mar 2026 00:09:36 GMT</pubDate></item><item><title><![CDATA[Reply to Do I need to add qmldir for python objects exposed as singleton instances? on Wed, 04 Mar 2026 13:04:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jksh">@<bdi>JKSH</bdi></a> I am not sure that I follow you. My understanding was that when I use <strong>@QmlElement</strong>/<strong>QmlSingleton</strong> I was making the types available for QML to instantiate. In my case, I am creating an instance of my class on the python side that I am registering after, so I can inject some dependencies into it before the QML engine gets it.</p>
<p dir="auto">Are you saying that if I just use both decorators in my class, I can still instantiate it before loading the engine and then my QML code can use the instance I created on the python side? I actually did a quick test just changing that and it fails with:</p>
<pre><code>&lt;Unknown File&gt;: qmlRegisterSingletonType(): "AppController" is not available because the callback function returns a null pointer.
</code></pre>
<p dir="auto">So I am not sure if I would need to do smth else extra or this approach does not work for registering an instance of my class.</p>
]]></description><link>https://forum.qt.io/post/836768</link><guid isPermaLink="true">https://forum.qt.io/post/836768</guid><dc:creator><![CDATA[Munhz]]></dc:creator><pubDate>Wed, 04 Mar 2026 13:04:05 GMT</pubDate></item><item><title><![CDATA[Reply to Do I need to add qmldir for python objects exposed as singleton instances? on Wed, 04 Mar 2026 07:26:28 GMT]]></title><description><![CDATA[<p dir="auto">Hi, and welcome!</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/munhz">@<bdi>Munhz</bdi></a> said in <a href="/post/836744">Do I need to add qmldir for python objects exposed as singleton instances?</a>:</p>
<blockquote>
<p dir="auto">Now My AppController doesnt use @QmlElement or @QmlSingleton bc as far as I understand I dont need those while registering a singleton instance this way. Now on my qml file I get a warning as TodoApp cant be imported</p>
</blockquote>
<p dir="auto">It's the other way round: If you use <code>@QmlElement</code> + <code>@QmlSingleton</code>, then you don't need to call qmlRegisterSingletonInstance() and you shouldn't get those warnings.</p>
<p dir="auto">Just remember that your <a href="http://main.py" target="_blank" rel="noopener noreferrer nofollow ugc">main.py</a> file must <code>import</code> the file that contains <code>@QmlElement</code> and the related decorators/variables before you initialize your QML engine.</p>
]]></description><link>https://forum.qt.io/post/836760</link><guid isPermaLink="true">https://forum.qt.io/post/836760</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Wed, 04 Mar 2026 07:26:28 GMT</pubDate></item></channel></rss>