Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword or section
Like this
103Activity
P. 1
Qt Tutorial

Qt Tutorial

Ratings:

4.5

(2)
|Views: 11,411 |Likes:
Published by HMaSN

More info:

Published by: HMaSN on Oct 27, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

12/13/2012

pdf

text

original

 
Qt Tutorial
This tutorial gives an introduction to GUI programming using the Qt toolkit. It doesn't cover everything; theemphasis is on teaching the programming philosophy of GUI programming, and Qt's features are introduced asneeded. Some commonly used features are never used in this tutorial.Chapter one starts with a minimal "Hello world" program and the following chapters introduce new concepts.By Chapter 14, the "Hello world" program from Chapter 1 will have turned into a 650-line game.If you are completely new to Qt, please readHow to Learn Qtif you haven't already done so.The tutorial's source code is located in Qt's
examples/tutorials/tutorial
directory.Tutorial chapters:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
Facing the WallThis little game doesn't look much like a modern GUI application. It uses a some GUI techniques, but after you've worked through it, we recommend checking out theApplicationexample, which presents a small GUIapplication, with menus, tool bars, a status bar, and so on.
Qt Tutorial 1 - Hello World!
Files:
This first program is a simple "Hello world" example. It contains only the bare minimum you need to get a Qtapplication up and running. The picture below is a screenshot of this program.Here's the complete source code for the application:
 
#include <QApplication>#include <QPushButton>
 
int main(int argc, char *argv[]){QApplication app(argc, argv);QPushButton hello("Hello world!");hello.show();return app.exec();}
Line by Line Walkthrough
#include <QApplication>
This line includes theQApplicationclass definition. There has to be exactly oneQApplication object in every GUI application that uses Qt.QApplicationmanages various application-wide resources, such as the defaultfont and cursor.
#include <QPushButton>
This line includes theQPushButtonclass definition. For each class that's part of the public Qt API, there existsa header file of the same name that contains its definition.QPushButtonis a GUI push button that the user can press and release. It manages its own look and feel, likeevery other QWidget.A widget is a user interface object that can process user input and draw graphics. The  programmer can change both the overall look and feel and many minor properties of it (such as color), as wellas the widget's content. AQPushButtoncan show either a text or aQIcon.
int main(int argc, char *argv[]){
The
main()
function is the entry point to the program. Almost always when using Qt,
main()
only needs to perform some kind of initialization before passing the control to the Qt library, which then tells the programabout the user's actions via events.The
argc
parameter is the number of command-line arguments and
argv
is the array of command-linearguments. This is a standard C++ feature.
QApplication app(argc, argv);
The
app
object is this program'sQApplicationinstance. Here it is created. We pass
argc
and
argv
to theQApplicationconstructor so that it can process certain standard command-line arguments (such as
-display
under X11). All command-line arguments recognized by Qt are removed from
argv
, and
argc
is decrementedaccordingly. See theQApplication::arguments() documentation for details.TheQApplication object must be created before any GUI-related features of Qt are used.
QPushButton hello("Hello world!");
Here, after theQApplication, comes the first GUI-related code: A push button is created.The button is set up to display the text "Hello world!". Because we don't specify a parent window (as secondargument to the QPushButton constructor), the button will be a window of its own, with its own window frame and title bar.
 
The button's size is determined by its default size hint. We could call QWidget::move() to assign a specific screen position to the widget, but instead we let the windowing system choose a position.
 
hello.show();
A widget is never visible when you create it. You must callQWidget::show() to make it visible.
return app.exec();}
This is where
main()
passes control to Qt.QCoreApplication::exec() will return when the application exits.(QCoreApplicationisQApplication's base class. It implementsQApplication's core, non-GUI functionality and can be used when developing non-GUI applications.)InQCoreApplication::exec(), Qt receives and processes user and system events and passes these on to theappropriate widgets.You should now compile and run this program.
Environment Variables
If you are developing Qt applications at the command line, you need to ensure that the Qt libraries andexecutables are accessible to your environment by prepending the path to Qt's
bin
directory to your 
 PATH 
variable; this is described in theInstallationinstructions for your platform.On Windows, this is automatically done for you if you get your command line prompt from the
Start>All  Programs>Qt 
menu. If you get your command line prompt with
Start>Run>Command 
, you have to set the
 PATH 
variable there yourself.
Building an application
The tutorial examples are located in the Qt
examples/tutorials/tutorial 
directory. If you installed a binary Qt package, pre-built examples were installed as well. If you built Qt yourself, the examples were built at the sametime. In either case, there is a lot to be learned about using Qt by modifying and building the examples yourself.Assuming you have copied an example's
.cpp
and
.h
files to an otherwise empty directory, and you have madeyour changes there, the next step is to create a Qt makefile in that directory. To create a Qt makefile, useqmake,the build tool supplied with Qt. Run the following two commands in the directory containing your modifiedsources to create the makefile.
qmake -projectqmake
The first command tells qmake to create a project (
.pro
) file. The second command tells qmake to use the
.pro
file to create a platform-specific makefile. Now you can just run
make
(
nmake
if you are using Visual Studio) to compile the program, and then you canrun your first Qt application!
Running this example
When you run the example, you will see a small window showing a single button. On the button you can readthe famous words: "Hello world!"
Exercises
Try to resize the window. Click the button. If you are running X11, try running the program with the
-geometry
option (e.g.,
-geometry 100x200+10+20
).
Qt Tutorial 2 - Calling it Quits

Activity (103)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Divya Talwar liked this
Bakhtiyar Sierad liked this
rajppd liked this
rajppd liked this
rajppd liked this
rajppd liked this
rajppd liked this
rajppd liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->