Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a little case study:</p> <pre><code>QString h = "Hello"; // works QString w = "World"; // works too, of course QString a = h + "World"; // works QString b = "Hello" + w; // also works QString c = "Hello" + "World"; // does not work </code></pre> <p><em>String literals</em> in C++ (text in quotes) are not objects and don't have methods...just like numeric values aren't objects. To make a string start acting "object-like" it has to get wrapped up into an object. <code>QString</code> is one of those wrapping objects, as is the <code>std::string</code> in C++.</p> <p>Yet the behavior you see in <strong>a</strong> and <strong>b</strong> show we're somehow able to <em>add a string literal to an object</em>. That comes from the fact that Qt has defined global operator overloads for both the case where the left operand is a QString with the right a <code>const char*</code>:</p> <p><a href="http://doc.qt.nokia.com/latest/qstring.html#operator-2b-24" rel="nofollow">http://doc.qt.nokia.com/latest/qstring.html#operator-2b-24</a></p> <p>...as well as the other case where the left is a <code>const char*</code> and the right is a QString:</p> <p><a href="http://doc.qt.nokia.com/latest/qstring.html#operator-2b-27" rel="nofollow">http://doc.qt.nokia.com/latest/qstring.html#operator-2b-27</a></p> <p>If those did not exist then you would have had to write:</p> <pre><code>QString a = h + QString("World"); QString b = QString("Hello") + w; </code></pre> <p>You could still do that if you want. In that case what you'll cause to run will be the addition overload for both operands as QString:</p> <p><a href="http://doc.qt.nokia.com/latest/qstring.html#operator-2b-24" rel="nofollow">http://doc.qt.nokia.com/latest/qstring.html#operator-2b-24</a></p> <p>But if even <em>that</em> didn't exist, you'd have to call a member function. For instance, append():</p> <p><a href="http://doc.qt.nokia.com/latest/qstring.html#append" rel="nofollow">http://doc.qt.nokia.com/latest/qstring.html#append</a></p> <p>In fact, you might notice that there's no overload for appending an integer to a string. (There's one for a <code>char</code>, however.) So if your <code>TipsCount</code> is an integer, you'll have to find some way of turning it into a QString. The static <code>number()</code> methods are one way.</p> <p><a href="http://doc.qt.nokia.com/latest/qstring.html#number" rel="nofollow">http://doc.qt.nokia.com/latest/qstring.html#number</a></p> <p>So you might find you need:</p> <pre><code>ui-&gt;button-&gt;setText(QString(" Tips ") + "\n" + QString::number(TipsCount)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload