Top Banner
Introduction to Qt The IDE for C++
19
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Qt

Introduction to Qt The IDE for C++

Page 2: Qt

THE QT STORY Haavard Nord and Eirik Chambe-Eng (the original

developers of Qt and the CEO and President, respectively, of Trolltech) began development of "Qt" in 1991, three years before the company was incorporated as Quasar Technologies, then changed the name to Troll Tech and then to Trolltech.

Qt is developed by an open source project, the Qt Project, involving both individual developers as well as developers from Nokia, Digia, and other companies interested in the development of Qt.

Page 3: Qt

The Qt toolkit is a multi-platform C++ GUI toolkit (class library) that has been developed over a 6 year period.

The company Troll Tech AS was founded in 1994 to secure future development of Qt.

On May 20, 1995, Qt was made available under commercial and non-commercial GNU licenses. The non-commercial license grants any developer the right to use Qt to develop software for the free software community.

It was ten months before the first commercial license was purchased. The European Space Agency purchased the second.

Around 1997, Qt was chosen as the code basis for the KDE linux desktop environment.

Qt 3.0 was released in 2001 with Windows, Unix, Linux, Embedded Linux, and Mac OS X libraries.

Page 4: Qt

WHY CROSS-PLATFORM GUI TOOLKITS

Increases target market. May provides the same look-and-feel

across platform. Reduces training and documentation costs.

Page 5: Qt

STATEGIES FOR IMPLEMENTING CROSS-PLATFORM GUIS

API Layering – Mapping one API to many others Example – wxWindows – Win32 API on top of Motif or

Xt API under Unix. Advantages – easy to write, 100% compatible native

look and feel. Disadvantages –

slower problems mapping to vastly different API architectures Lowest common denominator – i.e. no pop-up help

anywhere Objects required a C++ wrapper to work with them in C++

Page 6: Qt

QT ASSISTANT All documentation is

available through the trolltech web site.

Qt Assistant is a Qt help browser that runs under Windows.

It has with search and indexing features that make it quicker and easier than the web.

Page 7: Qt

`QT DESIGNER Widgets and forms

created with Qt Designer integrated seamlessly with programmed code, using Qt's signals and slots mechanism, that lets you easily assign behavior to graphical elements. All properties set in Qt Designer can be changed dynamically within the code. Furthermore, features like widget promotion and custom plugins allow you to use your own components with Qt Designer.

Page 8: Qt

A SIMPLE EXAMPLE/* HELLOWORLD.CPP */

1 #include <qapplication.h> 2 #include <qlabel.h> 3 4 int main(int argc, char **argv) { 5 6 QApplication myapp(argc, argv); 7 8 Qlabel *mylabel = new Qlabel(“Hello World”, 0); 9 mylabel->resize(100, 200);1011 myapp.setMainWidget(mylabel);12 mylabel->show();13 return myapp.exec();14 }

Page 9: Qt

LINE-BY-LINE1 #include <qapplication.h>2 #include <qlabel.h>Always #include any Q types referenced in code.

6 QApplication myapp(argc, argv);Creates an object to manage application-wide resources. Passes argc and argv because Qt supports a few command line arguments of its own.

8Qlabel *mylabel = new Qlabel(“Hello World”, 0);Creates a QLabel widget on the heap. A widgets is any visual element in a user interface. Widgets can contain other widgets. For example a window may contain a QMenuBar, QToolBar, QStatusBar, and other widgets. The 0 parameters says that that the label is a stand-alone window, is not inside another window.

Page 10: Qt

LINE-BY-LINE9 mylabel->resize(100, 200);Invokes the resize() member function.

11 myapp.setMainWidget(mylabel);Make the label the main application widget. This means that closing the label windows closes the application.

12 mylabel->show();Invoke the show() member function to make the label visible. All widgets are created invisible so that their properties can be manipulated without flickering. For example, you would show a widget and then change its size and color. You would change the size and color first, and then show the widget.

13 return myapp.exec();Passes control of the application to Qt. At this point the application goes into “event-driven” mode. It will just sit there until the user does something to create an even. This is the same concept as Word. Word starts and waits for the user to do something.

Page 11: Qt

EVENT HANDLING QT's new approach: signals and slots

A widget sends out various signals Object methods can be declared as slots Compatible signals and slots can be connected or

plugged together like a telephone switchboard (parameter types must match)

Strict separation This strict separation between UI components and

program elements lends itself to component-based programming

Goal: separate UI from program logic

Page 12: Qt

SIGNALS AND SLOTS

clicked_method()

Page 13: Qt

QMAKE The qmake utility is typically invoked with the following

three commands]

qmake –projectqmakemake (or nmake under Windows)

Rules: Be sure to place code in its own directory. qmake scans all subdirectories for dependencies. Do not place

archive version under a “save” subdirectory. If you reorganize your files, like adding a new .h, delete all the .pro

and other working files, then start over.

Page 14: Qt

DEFINING SIGNALS AND SLOTS New C++ syntax for defining signals and

slots, added to public, private, etc.class myClass : public Qobject {Q_OBJECT //required macro, no semicolon…signals:

void somethingHappened();… public slots:

void slotDoSomething();…private slots:

void slotDoSomethingInternal();…};

Page 15: Qt

EVENTS Signals: emit events

declare as signals, otherwise normal member functions You don't implement them. Rather, you send them

with the (new) keyword emit E.g. emit(sliderChanged(5))

Slots: receive and handle events Normal member functions declared as slots

Connect: must connect signals to slots QObject::connect( mymenu, SIGNAL(activated(int)),

myobject, SLOT(slotDoMenuFunction(int)) ); moc: meta object compiler (preprocessor)

converts these new keywords to real C++

Page 16: Qt

WIDGETS Base class for all UI widgets Properties

width, height, backgroundColor, font, mouseTracking, backgroundPixmap, etc.

Slots repaint, show, hide, move, setGeometry,

setMainWidget, etc. Signals:

mouseMoveEvent, keyPressEvent, resizeEvent, paintEvent, enterEvent, leaveEvent, etc.

Page 17: Qt

QT, A GUI TOOLKIT Events processed with signals and slots

signal generates an event, e.g., button push slot processes the event, e.g., pop up a file dialog box

QPushButton * quitB = new QPushButton(“Quit”,...,...); connect (quitB, SIGNAL(clicked()), qApp,

SLOT(quit()); qApp is a global variable, of type QApplication

one QApplication per program defined first in main() main returns qApp.exec() SIGNAL and SLOT are macros, expanded by a meta-object

compiler (moc) moc generates .cpp files from user-defined Qt subclasses

Page 18: Qt

OTHER FEATURES OF QT The Qt Paint Engine QPainter is highly optimized and contains several caching

mechanisms to speed up drawing. Under X11, it caches GCs (graphics contexts), which often make it faster than native X11 programs.

QPainter contains all the functionality one would expect from a professional 2D graphics library. The coordinate system of a QPainter can be transformed using the standard 2D transformations (translate, scale, rotate and shear).

Qt supports Open GL for 3D graphics. Qt also contains a set of general purpose classes and a number of

collection-classes to ease the development of multi-platform applications.

Qt has platform independent support for the operating system dependent functions, such as time/date, files/directories and TCP/IP sockets.

Page 19: Qt

Thank you

Anupam Kumar SrivastavaSoftware Engineer Burl Software Pvt. Ltd E-mail: [email protected]