Qt for beginners Hello World: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
(Redirect to Qt for Beginners (Merged))
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
=Qt for beginners — Hello World !=
#REDIRECT [[Qt for Beginners]]
 
[[Qt for beginners Introduction|<<< Introduction]] | [[Qt for beginners|Summary]] | [[Qt for beginners pretty button|A pretty button >>>]]
 
'''''Note:''' See the official [http://doc.qt.io/qt-5/gettingstartedqt.html Getting Started with Qt Widgets] ''[qt.io]'' page for an alternative tutorial.''
 
We are now ready to create our first window. And it will be as usual, a ''hello world''.
 
==Qt Creator features==
 
Before writing our first <span class="caps">GUI</span> app, let’s discover Qt Creator.
 
Qt Creator is yet another <span class="caps">IDE</span> for C++, but it is very well suited for coding Qt applications. It provides a doc browser and the “designer”, which makes creation of windows easier, all wrapped in a well-designed user interface. It’s also one of the fastest <span class="caps">IDE</span>’s available.
 
Here are some features of Qt Creator : [[:Category:Tools::QtCreator|Qt Creator overview]].
 
==Our first window==
 
Let’s start by creating our first project. It will be an empty project, so we have to proceed with:<br /> File &gt; New file or project &gt; Other Projects &gt; Empty Qt Project
 
[[Image:ELAMwEHo.jpg|Create new project]]
 
Follow the wizard, and after selecting the project folder and name, and select the version of Qt to use, you should land on this page
 
[[Image:IbEbCNNl.jpg|pro editor]]
 
This is the project file (extension .pro). Qt uses a command line tool that parses these project files in order to generate “makefiles”, files that are used by compilers to build an application. This tool is called '''qmake'''. But, we shouldn’t bother too much about qmake, since Qt Creator will do the job for us.
 
In a project file, there is some minimal code that should always be written :
 
* ''<span class="caps">TEMPLATE</span>'' describes the type to build. It can be an application, a library, or simply subdirectories.
* ''<span class="caps">TARGET</span>'' is the name of the app or the library.
* ''QT'' is used to indicate what libraries (Qt modules) are being used in this project. Since our first app is a small <span class="caps">GUI</span>, we will need QtCore and QtGui.
 
Let’s now add the entry point of our application. Using File &gt; New file or project &gt; C++ &gt; C++ Source file should do the job.
 
[[Image:h25u2z0P.jpg|Create main.cpp]]
 
Follow the wizard once again, naming the file “main”, and you are done.<br /> You will notice that in the project file, a new line has been added automatically by Qt Creator :
 
Headers and sources files can be added manually with
 
or
 
If you use Qt Creator’s wizards, this is done automatically.
 
The minimal source code of a Qt application is
 
'''QApplication''' is a very important class. It takes care of input arguments, but also a lot of other things, and most notably, the ''event loop''. The event loop is a loop that waits for used input in <span class="caps">GUI</span> applications.
 
When calling <code>app.exec();</code> the event loop is launched.
 
Let’s compile this application. By clicking on the green arrow on the bottom left, Qt Creator will compile and execute it. And what happened ? The application seems to be launched and not responding. That is actually normal. The event loop is running and waiting for events, like mouse clicks on a <span class="caps">GUI</span>, but we did not provide any event to be processed, so it will run indefinitely.
 
Let’s add something to be displayed.
 
Compile it, and … here it is ! Our first window !
 
[[Image:zxM3h2GE.jpg|Our first window]]
 
==How is a Qt program compiled==
 
Qt Creator does the job for us, but it might be interesting to know how Qt programs are compiled.
 
For small programs, it is easy to compile everything by hand, creating objects files, then linking them. But for bigger projects, the command line easily becomes hard to write. If you are familiar with Linux, you may know that all the programs are compiled using a makefile that describes all these command lines to execute. But for some projects, even writing a makefile can become tedious.
 
''qmake'' generates those makefiles for you. With a simple syntax, it produces the makefile that is used to compile a Qt program. But that is not its only goal. Qt uses meta-objects to extend C++ functionalities, and qmake is responsible to prepare a makefile that contains this meta-object extraction phase. You will see this in another chapter.
 
So, Qt apps are compiled in 3 steps
 
# A ''.pro'' file is written to describe the project to compile
# A makefile is generated using ''qmake''
# The program is build using ''make'' (or ''nmake'' or ''jom'' on windows)
 
[[Qt for beginners Introduction|&lt;&lt;&lt; Introduction]] | [[Qt for beginners|Summary]] | [[Qt for beginners pretty button|A pretty button &gt;&gt;&gt;]]
 
===Categories:===
 
* [[:Category:HowTo|HowTo]]
* [[:Category:Qt for beginners|Qt_for_beginners]]
* [[:Category:Tools|Tools]]
** [[:Category:Tools::QtCreator|QtCreator]]
* [[:Category:Tutorial|Tutorial]]

Latest revision as of 14:35, 5 May 2015

Redirect to: