Qt Demo Script: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(clean-up)
 
(2 intermediate revisions by 2 users not shown)
Line 5: Line 5:
= Main function =
= Main function =


<code><br />#include &lt;QtCore&amp;gt;
<code>
#include <QtCore>


int main(int argc, char **argv)<br />{<br /> // sample code goes here<br />}<br /></code>
int main(int argc, char **argv)
{
// sample code goes here
}
</code>


= Fun with Strings =
= Fun with Strings =


<code><br />QString s1(&quot;Hello&amp;quot;);<br />QString s2(&quot;World&amp;quot;);<br />QString s = s1 + &quot; &quot; + s2;<br />qDebug() &lt;&lt; s; // &quot;Hello World&amp;quot;<br /></code>
<code>
QString s1("Hello");
QString s2("World");
QString s = s1 + " " + s2;
qDebug() << s; // "Hello World"
</code>


This is easy.
This is easy.
Line 17: Line 27:
= List of Strings =
= List of Strings =


<code><br />QList&amp;lt;QString&amp;gt; list;<br />list &lt;&lt; s1 &lt;&lt; s2;<br />qDebug() &lt;&lt; list;<br />// or<br />list = s.split(&quot; &quot;); // &quot;Hello World&amp;quot; =&gt; [&quot;Hello&amp;quot;, &quot;World&amp;quot;]<br /></code>
<code>
QList<QString> list;
list << s1 << s2;
qDebug() << list;
// or
list = s.split(" "); // "Hello World" => ["Hello", "World"]
</code>


= QStringList =
= QStringList =


<code><br />QString s(&quot;Hello World&amp;quot;);<br />QStringList list = s.split(&quot; &quot;);<br />QString s2 = list.join(&quot; &quot;) + &quot;!&quot;;<br />QDebug() &lt;&lt; s &lt;&lt; &quot; =&gt; &quot; &lt;&lt; list; &lt;&lt; &quot; =&gt; &quot; &lt;&lt; s2; // &quot;Hello World =&gt; [&quot;Hello&amp;quot;, &quot;World&amp;quot;] =&gt; &quot;Hello World!&quot;<br /></code>
<code>
QString s("Hello World");
QStringList list = s.split(" ");
QString s2 = list.join(" ") + "!";
QDebug() << s << " => " << list; << " => " << s2; // "Hello World => ["Hello", "World"] => "Hello World!"
</code>

Latest revision as of 21:58, 23 March 2016

This is a short introduction to Qt using a simple demo script. We play around with strings and look into the issue how to present them.

Let's get started.

Main function

#include <QtCore>

int main(int argc, char **argv)
{
 // sample code goes here
}

Fun with Strings

QString s1("Hello");
QString s2("World");
QString s = s1 + " " + s2;
qDebug() << s; // "Hello World"

This is easy.

List of Strings

QList<QString> list;
list << s1 << s2;
qDebug() << list;
// or
list = s.split(" "); // "Hello World" => ["Hello", "World"]

QStringList

QString s("Hello World");
QStringList list = s.split(" ");
QString s2 = list.join(" ") + "!";
QDebug() << s << " => " << list; << " => " << s2; // "Hello World => ["Hello", "World"] => "Hello World!"