Getting Started on the Commandline: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Getting Started with C++ Software Development with Qt=
h1. Getting Started with C++ Software Development with Qt


Welcome to C++! This guide will show you how to get started with C++ application development with Qt. Before you continue you may want to start downloading the Qt <span class="caps">SDK</span> from [http://troll.no here] ''[troll.no]'' . Choose the <span class="caps">LGPL</span> version if unsure. The source code examples in this guide are entirely <span class="caps">LGPL</span> compatible. On Linux you can also just install “libqt4-dev” and “g++” using your favorite package manager.
Welcome to C+''! This guide will show you how to get started with C''+ application development with Qt. Before you continue you may want to start downloading the Qt SDK from &quot;here&amp;quot;:http://troll.no . Choose the LGPL version if unsure. The source code examples in this guide are entirely LGPL compatible. On Linux you can also just install &quot;libqt4-dev&amp;quot; and &quot;g+''&quot; using your favorite package manager.
<br />h2. &quot;Hello, world!&quot; console application
<br />Let's begin with a simple C''+ program. Open a text editor and enter the following source code. Keep care to type it exactly like shown in the following example. Like most programming languages C++ is case-sensitive.


==“Hello, world!” console application==
<code><br />#include &lt;QTextStream&amp;gt;


Let’s begin with a simple C++ program. Open a text editor and enter the following source code. Keep care to type it exactly like shown in the following example. Like most programming languages C++ is case-sensitive.
int main() {<br /> QTextStream(stdout) &lt;&lt; &quot;Hello, world!&quot; &lt;&lt; endl;<br /> return 0;<br />}<br /></code>


Create a directory “hello” and save the source code into a file hello.cpp residing in this directory.
Create a directory &quot;hello&amp;quot; and save the source code into a file hello.cpp residing in this directory.


The download of the Qt <span class="caps">SDK</span> or package install should be finished by now.
The download of the Qt SDK or package install should be finished by now.


Open a shell and inspect which version of Qt you have installed: enter “qmake -v”. If qmake can’t be found you have to add its installation path to your environment variable. Search for it in the <span class="caps">SDK</span> installation directory.
Open a shell and inspect which version of Qt you have installed: enter &quot;qmake -v&amp;quot;. If qmake can't be found you have to add its installation path to your environment variable. Search for it in the SDK installation directory.


Now enter the “hello” directory and type: “qmake -project”. This will create an qmake project file. Thereafter run just “qmake” without arguments. this will create a “Makefile” which contains the rules to build your application. Then run “make” (called nmake or gmake on some platforms), which will build your app according to the rules layed out in the “Makefile”. Finally you can run the app ./hello”.
Now enter the &quot;hello&amp;quot; directory and type: &quot;qmake -project&amp;quot;. This will create an qmake project file. Thereafter run just &quot;qmake&amp;quot; without arguments. this will create a &quot;Makefile&amp;quot; which contains the rules to build your application. Then run &quot;make&amp;quot; (called nmake or gmake on some platforms), which will build your app according to the rules layed out in the &quot;Makefile&amp;quot;. Finally you can run the app &quot;./hello&amp;quot;.


So, on a bash command prompt you would enter:<br /> $ cd my/dir/hello<br /> $ qmake -project<br /> $ qmake<br /> $ make<br /> $ ./hello
So, on a bash command prompt you would enter:<br />$ cd my/dir/hello<br />$ qmake -project<br />$ qmake<br />$ make<br />$ ./hello


==“Hello, world!desktop application==
== &quot;Hello, world!&quot; desktop application ==


Simply replace the source code in hello.cpp by:
Simply replace the source code in hello.cpp by:


Run “make” again. If you launch the application should see a small window saying “Hello, world!”.
<code><br />#include &lt;QtGui&amp;gt;


At this point I hope you were able to take the first step in C++ development using Qt. You may now want to start reading some of Qt’s documentation. Qt’s <span class="caps">API</span> reference is directly written by the software developer that create Qt. You find the latest <span class="caps">API</span> documentation under http://troll.no/doc . For instance go to [http://troll.no/doc/4.7/qlabel.html here] ''[troll.no]'' to learn more about the “QLabel” class. Another good starting point is to take a look at the examples, which ship with Qt. If you have chosen to install the Qt <span class="caps">SDK</span> and you launch the Qt creator you can open the examples directly from the start page.
int main(int argc, char **argv) {<br /> QApplication app(argc, argv);<br /> QLabel label(&quot;Hello, world!&quot;);<br /> label.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
 
Run &quot;make&amp;quot; again. If you launch the application should see a small window saying &quot;Hello, world!&quot;.
 
At this point I hope you were able to take the first step in C++ development using Qt. You may now want to start reading some of Qt's documentation. Qt's API reference is directly written by the software developer that create Qt. You find the latest API documentation under http://troll.no/doc . For instance go to &quot;here&amp;quot;:http://troll.no/doc/4.7/qlabel.html to learn more about the &quot;QLabel&amp;quot; class. Another good starting point is to take a look at the examples, which ship with Qt. If you have chosen to install the Qt SDK and you launch the Qt creator you can open the examples directly from the start page.

Revision as of 10:52, 24 February 2015

h1. Getting Started with C++ Software Development with Qt

Welcome to C+! This guide will show you how to get started with C+ application development with Qt. Before you continue you may want to start downloading the Qt SDK from "here&quot;:http://troll.no . Choose the LGPL version if unsure. The source code examples in this guide are entirely LGPL compatible. On Linux you can also just install "libqt4-dev&quot; and "g+" using your favorite package manager.
h2. "Hello, world!" console application
Let's begin with a simple C+ program. Open a text editor and enter the following source code. Keep care to type it exactly like shown in the following example. Like most programming languages C++ is case-sensitive.

<br />#include &lt;QTextStream&amp;gt;

int main() {<br /> QTextStream(stdout) &lt;&lt; &quot;Hello, world!&quot; &lt;&lt; endl;<br /> return 0;<br />}<br />

Create a directory "hello&quot; and save the source code into a file hello.cpp residing in this directory.

The download of the Qt SDK or package install should be finished by now.

Open a shell and inspect which version of Qt you have installed: enter "qmake -v&quot;. If qmake can't be found you have to add its installation path to your environment variable. Search for it in the SDK installation directory.

Now enter the "hello&quot; directory and type: "qmake -project&quot;. This will create an qmake project file. Thereafter run just "qmake&quot; without arguments. this will create a "Makefile&quot; which contains the rules to build your application. Then run "make&quot; (called nmake or gmake on some platforms), which will build your app according to the rules layed out in the "Makefile&quot;. Finally you can run the app "./hello&quot;.

So, on a bash command prompt you would enter:
$ cd my/dir/hello
$ qmake -project
$ qmake
$ make
$ ./hello

"Hello, world!" desktop application

Simply replace the source code in hello.cpp by:

<br />#include &lt;QtGui&amp;gt;

int main(int argc, char **argv) {<br /> QApplication app(argc, argv);<br /> QLabel label(&quot;Hello, world!&quot;);<br /> label.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br />

Run "make&quot; again. If you launch the application should see a small window saying "Hello, world!".

At this point I hope you were able to take the first step in C++ development using Qt. You may now want to start reading some of Qt's documentation. Qt's API reference is directly written by the software developer that create Qt. You find the latest API documentation under http://troll.no/doc . For instance go to "here&quot;:http://troll.no/doc/4.7/qlabel.html to learn more about the "QLabel&quot; class. Another good starting point is to take a look at the examples, which ship with Qt. If you have chosen to install the Qt SDK and you launch the Qt creator you can open the examples directly from the start page.