<?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[[solved] Accessing C++ constants from within QML]]></title><description><![CDATA[<p dir="auto">I'm having trouble accessing C++ constants from within QML. I'm trying to create a Q_OBJECT that holds Q_PROPERTYs:</p>
<p dir="auto">@<br />
class Colors: public QObject<br />
{<br />
private:<br />
Q_OBJECT<br />
Q_PROPERTY (QColor contentBg READ getContentBg CONSTANT)<br />
public:<br />
Colors();<br />
virtual ~Colors() { }<br />
QColor getContentBg() const { return QColor ("#fefa07"); }<br />
};<br />
@</p>
<p dir="auto">Then I register the type through qmlRegisterUncreatableType:</p>
<p dir="auto">@<br />
int main(int argc, char *argv[])<br />
{<br />
QGuiApplication app(argc, argv);<br />
QQmlApplicationEngine engine;<br />
qmlRegisterUncreatableType&lt;Colors&gt; ("FirstQtTest.Defs", 1, 0, "Colors", "Instances of Colors are disallowed!");<br />
engine.load (QUrl (QStringLiteral ("qrc:///main.qml")));<br />
return app.exec();<br />
}<br />
@</p>
<p dir="auto">My QML file then tries to access the colour constant like this:</p>
<p dir="auto">@<br />
import FirstQtTest.Defs 1.0<br />
// ...<br />
color: Colors.contentBg;<br />
@</p>
<p dir="auto">I keep getting the runtime error: "Unable to assign [undefined] to QColor". What am I doing wrong?</p>
<p dir="auto">Thanks in advance!</p>
]]></description><link>https://forum.qt.io/topic/42155/solved-accessing-c-constants-from-within-qml</link><generator>RSS for Node</generator><lastBuildDate>Mon, 04 May 2026 23:34:27 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/42155.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 03 Jun 2014 12:23:41 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [solved] Accessing C++ constants from within QML on Tue, 10 Jun 2014 13:09:55 GMT]]></title><description><![CDATA[<p dir="auto">I managed to get it to work by replacing qmlRegisterUncreatableType with setContextProperty:</p>
<p dir="auto">@<br />
Colors colors;<br />
engine.rootContext()-&gt;setContextProperty ("Colors", &amp;colors);<br />
@</p>
<p dir="auto">I still can't tell why qmlRegisterUncreatableType doesn't work, but the above works fine in my case.</p>
]]></description><link>https://forum.qt.io/post/231500</link><guid isPermaLink="true">https://forum.qt.io/post/231500</guid><dc:creator><![CDATA[digorydoo]]></dc:creator><pubDate>Tue, 10 Jun 2014 13:09:55 GMT</pubDate></item></channel></rss>