<?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[Assign C++ object to Text element (implicit conversion to QString)]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I'm trying to implicitly convert an object to QString in order to<br />
assign it to a Text element in qml.</p>
<p dir="auto">I think an example makes things clear:<br />
@class Bar : public QObject<br />
{<br />
...<br />
Q_INVOKABLE QString value(int param = -1);<br />
};</p>
<p dir="auto">class Foo : public QObject<br />
{<br />
...<br />
Q_PROPERTY(Bar * bar READ bar WRITE setbar NOTIFY onBarChanged);<br />
};@</p>
<p dir="auto">A Foo object is registered as context property at application start:<br />
@viewer.rootContext()-&gt;setContextProperty("Foo", &amp;fooObject);@</p>
<p dir="auto">In qml the following works perfectly:<br />
@Text<br />
{<br />
...<br />
text: Foo.bar.value()<br />
}@</p>
<p dir="auto">But in most cases I do not need to give a parameter to the value call() and it would<br />
be much more straight forward if I could just write<br />
@Text<br />
{<br />
...<br />
text: Foo.bar<br />
}@</p>
<p dir="auto">At the moment there's a runtime error which says "Unable to assign Bar to QString".</p>
<p dir="auto">I tried to add a type cast operator to Bar:<br />
@    Q_INVOKABLE operator QString()<br />
{return value();}@</p>
<p dir="auto">But this did not resolve the problem.</p>
<p dir="auto">Is it possible to convert an object to a QString implicitly? And if yes: how?</p>
<p dir="auto">Thank you very much in advance,<br />
Martin</p>
]]></description><link>https://forum.qt.io/topic/31352/assign-c-object-to-text-element-implicit-conversion-to-qstring</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 14:21:57 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/31352.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Aug 2013 15:21:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Assign C++ object to Text element (implicit conversion to QString) on Fri, 13 Sep 2013 09:57:40 GMT]]></title><description><![CDATA[<p dir="auto">Hi JKSH,</p>
<p dir="auto">thank you very much.</p>
]]></description><link>https://forum.qt.io/post/195204</link><guid isPermaLink="true">https://forum.qt.io/post/195204</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 13 Sep 2013 09:57:40 GMT</pubDate></item><item><title><![CDATA[Reply to Assign C++ object to Text element (implicit conversion to QString) on Sat, 07 Sep 2013 02:31:30 GMT]]></title><description><![CDATA[<p dir="auto">Then it can't be done, I'm afraid.</p>
<p dir="auto">I actually just remembered: It doesn't make sense to <em>convert</em> a QObject to a string, either implicitly or explicitly. The reasoning is given "here":<a href="http://qt-project.org/doc/qt-5.1/qtcore/object.html#identity-vs-value" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5.1/qtcore/object.html#identity-vs-value</a> -- QObjects should be treated as identities, not values. Otherwise, Qt's object model breaks down.</p>
<p dir="auto">Instead, you should <em>extract</em> a string property from the QObject and copy that string, which is what <em>text: Foo.bar.value()</em> should do.</p>
]]></description><link>https://forum.qt.io/post/194509</link><guid isPermaLink="true">https://forum.qt.io/post/194509</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Sat, 07 Sep 2013 02:31:30 GMT</pubDate></item><item><title><![CDATA[Reply to Assign C++ object to Text element (implicit conversion to QString) on Fri, 30 Aug 2013 07:06:25 GMT]]></title><description><![CDATA[<p dir="auto">Hi JKSH,</p>
<p dir="auto">thank's for your response.<br />
I think overloading the assignment operator does not work unless I implement this operator in QString class directly (Bar should be assigned to QString not QString to Bar).</p>
<p dir="auto">Also overloading the "operator=" globally does not work. While this works well for other operators like &lt;&lt; operator= seems to be a special case because the compiler tells me that "operator=" had to be a static member:<br />
<a class="plugin-mentions-user plugin-mentions-a" href="/user/qstring">@<bdi>QString</bdi></a> &amp; operator=(QString &amp; str, const CTextProxy &amp; rhs)<br />
{<br />
str = rhs.value();<br />
return str;<br />
}@</p>
<p dir="auto">Best regards,<br />
Martin</p>
]]></description><link>https://forum.qt.io/post/193356</link><guid isPermaLink="true">https://forum.qt.io/post/193356</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 30 Aug 2013 07:06:25 GMT</pubDate></item><item><title><![CDATA[Reply to Assign C++ object to Text element (implicit conversion to QString) on Fri, 30 Aug 2013 01:18:02 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm not 100% sure, but try implementing <em>operator=()</em> to assign a Bar to a QString</p>
]]></description><link>https://forum.qt.io/post/193319</link><guid isPermaLink="true">https://forum.qt.io/post/193319</guid><dc:creator><![CDATA[JKSH]]></dc:creator><pubDate>Fri, 30 Aug 2013 01:18:02 GMT</pubDate></item></channel></rss>