<?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[Out of range condition on Q_PROPERTY]]></title><description><![CDATA[<p dir="auto">I am new to QT and was wondering how are you supposed to handle errors in the QT property system, more specifically on set the functions?</p>
<p dir="auto">class SomeClass : public QObject<br />
{<br />
Q_OBJECT<br />
Q_PROPERTY(qint16 temperature READ temperature WRITE setTemperature)</p>
<p dir="auto">private:<br />
qint16 mTemperature;<br />
...<br />
...<br />
...</p>
<p dir="auto">void SomeClass::temperature()<br />
{<br />
return mTemperature;<br />
}</p>
<p dir="auto">void SomeClass::setTemperature(qint16 _temperature)<br />
{<br />
if (_temperature &lt; 10 || _temperature &gt; 100)<br />
{<br />
// argument is out of range, what do you do here?<br />
return;<br />
}<br />
mTemperature = _temperature;<br />
}</p>
<p dir="auto">Are you supposed to throw an exception, invoke a signal or what?<br />
Any help would be greatly appreciated.</p>
]]></description><link>https://forum.qt.io/topic/60826/out-of-range-condition-on-q_property</link><generator>RSS for Node</generator><lastBuildDate>Wed, 11 Nov 2026 22:17:46 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/60826.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 19 Nov 2015 23:56:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Out of range condition on Q_PROPERTY on Fri, 20 Nov 2015 14:45:19 GMT]]></title><description><![CDATA[<p dir="auto">Mcosta,</p>
<p dir="auto">Thanks for the input, I'll probably use coercion as you suggest seems like the logical thing to do.</p>
]]></description><link>https://forum.qt.io/post/298980</link><guid isPermaLink="true">https://forum.qt.io/post/298980</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 20 Nov 2015 14:45:19 GMT</pubDate></item><item><title><![CDATA[Reply to Out of range condition on Q_PROPERTY on Fri, 20 Nov 2015 00:15:09 GMT]]></title><description><![CDATA[<p dir="auto">HI and welcome to devnet,</p>
<blockquote>
<p dir="auto">Are you supposed to throw an exception, invoke a signal or what?</p>
</blockquote>
<p dir="auto">For sure Qt does not use exceptions.</p>
<p dir="auto">What usually <strong>I do</strong> (is not an official Qt guideline) is to force the value to be in the range; you have 2 options depending of what do you need:</p>
<ol>
<li>Don't change the value if the proposed value is out-of range;</li>
</ol>
<pre><code>void MyObject::setValue (int newValue) 
{
    if (newValue &lt; MIN_VALUE || newValue &gt; MAX_VALUE)
        return;

    if (_v != newValue) {
        _v = newValue;

        Q_EMIT valueChanged(_v);
    }
}
</code></pre>
<ol start="2">
<li>Force the value to the limit values.</li>
</ol>
<pre><code>void MyObject::setValue (int newValue) 
{
    if (newValue &lt; MIN_VALUE)
        newValue = MIN_VALUE;

    if (newValue &gt; MAX_VALUE)
        newValue = MAX_VALUE;

    if (_v != newValue) {
        _v = newValue;

        Q_EMIT valueChanged(_v);
    }
}
</code></pre>
<p dir="auto">Anyway if you have a range (or only one of lower/upper limit) I suggest to define READ ONLY properties for the limits</p>
]]></description><link>https://forum.qt.io/post/298868</link><guid isPermaLink="true">https://forum.qt.io/post/298868</guid><dc:creator><![CDATA[mcosta]]></dc:creator><pubDate>Fri, 20 Nov 2015 00:15:09 GMT</pubDate></item></channel></rss>