<?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[Q_Property and reference data type]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I  try to start with Qt's property mechanism.</p>
<p dir="auto">The following is a first attemp</p>
<pre><code>#ifndef DBCONTEXT_H
#define DBCONTEXT_H

#include "dbset.h"
#include "dokumententity.h"
#include "qsqldatabase.h"
#include "quoteentity.h"

#include &lt;QObject&gt;

class DBContext : QObject {
  Q_OBJECT
public:
  DBContext();
  ~DBContext();
  Q_PROPERTY(DBSet&lt;DocumentEntity&gt; Documents READ Documents CONSTANT)

  DBSet&lt;DocumentEntity&gt; Documents() const;

private:
  DBSet&lt;DocumentEntity&gt; m_Documents;
};
#endif // DBCONTEXT_H

</code></pre>
<p dir="auto">and</p>
<pre><code>DBSet&lt;DocumentEntity&gt; DBContext::Documents() const { return m_Documents; }
</code></pre>
<p dir="auto">this seems to work, but...</p>
<p dir="auto">... should it be a better way to give reference type to avoid unnecessary copying?</p>
<pre><code>DBSet&lt;DocumentEntity&gt; &amp;DBContext::Documents() const { return &amp;m_Documents; }
</code></pre>
<p dir="auto">And this fails with</p>
<pre><code>dbcontext.cpp:29:62: Non-const lvalue reference to type 'DBSet&lt;DocumentEntity&gt;' cannot bind to a temporary of type 'const DBSet&lt;DocumentEntity&gt; *'
</code></pre>
<p dir="auto">I am confused since the lvalue is not taken as const as in the declaration.</p>
<p dir="auto">C++ const behaviour is still some miracle for me.</p>
<p dir="auto">Any hints, how to deal with it? And how should Q_PROPERTY look like to deal with reference types?</p>
<p dir="auto">greets</p>
<p dir="auto">Joachim</p>
]]></description><link>https://forum.qt.io/topic/144645/q_property-and-reference-data-type</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 21:45:05 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/144645.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 25 Apr 2023 15:54:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Q_Property and reference data type on Wed, 26 Apr 2023 06:36:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/masterq">@<bdi>MasterQ</bdi></a> said in <a href="/post/756166">Q_Property and reference data type</a>:</p>
<blockquote>
<p dir="auto">Why is "const" dropped?</p>
</blockquote>
<p dir="auto">Because you were trying to return a non-const reference to a const object.</p>
]]></description><link>https://forum.qt.io/post/756167</link><guid isPermaLink="true">https://forum.qt.io/post/756167</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 26 Apr 2023 06:36:58 GMT</pubDate></item><item><title><![CDATA[Reply to Q_Property and reference data type on Wed, 26 Apr 2023 06:32:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a></p>
<pre><code>dbcontext.cpp:29:62: Binding reference of type 'DBSet&lt;...&gt;' to value of type 'const DBSet&lt;...&gt;' drops 'const' qualifier
</code></pre>
<p dir="auto">I had tried this also before. But the main issue remains. Why is "const" dropped?</p>
<p dir="auto">This is not simply a warning, it is an error!</p>
<p dir="auto">--EDIT--</p>
<p dir="auto">OK, I got you poltergeist!</p>
<p dir="auto">I just deleted all and rewrote the whole thing new. Now it is working and no errors about dropping const appears anymore.</p>
<p dir="auto">Just wasted a whole day by struggling with this bullsh*t</p>
]]></description><link>https://forum.qt.io/post/756166</link><guid isPermaLink="true">https://forum.qt.io/post/756166</guid><dc:creator><![CDATA[MasterQ]]></dc:creator><pubDate>Wed, 26 Apr 2023 06:32:52 GMT</pubDate></item><item><title><![CDATA[Reply to Q_Property and reference data type on Wed, 26 Apr 2023 05:23:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/masterq">@<bdi>MasterQ</bdi></a> said in <a href="/post/756109">Q_Property and reference data type</a>:</p>
<blockquote>
<p dir="auto">DBSet&lt;BillEntity&gt; &amp;Bills() const { return &amp;m_Bills; };</p>
</blockquote>
<p dir="auto">This is wrong: you're trying to return a pointer, not reference!<br />
Should be:</p>
<pre><code>const DBSet&lt;BillEntity&gt; &amp;Bills() const { return m_Bills; };
</code></pre>
]]></description><link>https://forum.qt.io/post/756162</link><guid isPermaLink="true">https://forum.qt.io/post/756162</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Wed, 26 Apr 2023 05:23:39 GMT</pubDate></item><item><title><![CDATA[Reply to Q_Property and reference data type on Tue, 25 Apr 2023 21:36:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/masterq">@<bdi>MasterQ</bdi></a> said in <a href="/post/756109">Q_Property and reference data type</a>:</p>
<blockquote>
<p dir="auto">DBSet&lt;BillEntity&gt; &amp;Bills() const { return &amp;m_Bills; };</p>
</blockquote>
<pre><code>const DBSet&lt;BillEntity&gt; &amp;Bills() const { return &amp;m_Bills; };
</code></pre>
]]></description><link>https://forum.qt.io/post/756139</link><guid isPermaLink="true">https://forum.qt.io/post/756139</guid><dc:creator><![CDATA[JoeCFD]]></dc:creator><pubDate>Tue, 25 Apr 2023 21:36:38 GMT</pubDate></item><item><title><![CDATA[Reply to Q_Property and reference data type on Tue, 25 Apr 2023 18:41:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/joecfd">@<bdi>JoeCFD</bdi></a></p>
<p dir="auto">sry, maybe my post was misleading</p>
<pre><code>class DBContext : QObject {
  Q_OBJECT
public:
  DBContext();
  ~DBContext();

  Q_PROPERTY(DBSet&lt;DocumentEntity&gt; Documents READ Documents CONSTANT)
  Q_PROPERTY(DBSet&lt;BillEntity&gt; Bills READ Bills CONSTANT)

  DBSet&lt;DocumentEntity&gt; Documents() const { return m_Documents; }
  
  DBSet&lt;BillEntity&gt; &amp;Bills() const { return &amp;m_Bills; };

  private:
  const DBSet&lt;DocumentEntity&gt; m_Documents;
  const DBSet&lt;BillEntity&gt; m_Bills;

};
</code></pre>
<p dir="auto">The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.</p>
<p dir="auto">Question:<br />
Why is the lvalue not recognized as const in version2? How to do it right?</p>
<p dir="auto">Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?</p>
]]></description><link>https://forum.qt.io/post/756109</link><guid isPermaLink="true">https://forum.qt.io/post/756109</guid><dc:creator><![CDATA[MasterQ]]></dc:creator><pubDate>Tue, 25 Apr 2023 18:41:03 GMT</pubDate></item><item><title><![CDATA[Reply to Q_Property and reference data type on Tue, 25 Apr 2023 16:01:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/masterq">@<bdi>MasterQ</bdi></a> C++ does not allow  overloading a function with different return types.<br />
in your case, the two funcs are same, but return types are different.</p>
]]></description><link>https://forum.qt.io/post/756092</link><guid isPermaLink="true">https://forum.qt.io/post/756092</guid><dc:creator><![CDATA[JoeCFD]]></dc:creator><pubDate>Tue, 25 Apr 2023 16:01:17 GMT</pubDate></item></channel></rss>