Discover Qt 4 PDF Print E-mail
User Rating: / 0
PoorBest 
Tuesday, 20 May 2008

I. Overview of Qt
Qt is a library software offering mainly GUI components (widgets). Qt is not limited only to graphics but it offers components for network programming, management son execution and access to data.

Qt has been developed in C + +, therefore, it is primarily intended for development C + + but it should be noted that "bindings" exist in order to be able to use it from other languages.

Qt also extends C + + with signals and slots using moc, the compiler of meta-object (meta object compiler). The system of signals / slots is a specific characteristic of Qt which differs from other frameworks. A signal is issued when an event occurs (eg click of a mouse). Qt has many predefined signals, but we can always write our own signals. A slot is a function that is called in response to a signal. As signals, Qt offers many preset slots.

Rem: Qt, the library software for graphical interface written Trolltech Qt not QT or qt. It is pronounced in English "cute" which can mean cute in the language of Molière. Discovering this development tool cross-platform in that article!

II. Version 4 of the Qt library
Qt 4 is the latest generation of Qt kit application development cross-platform. Qt 4, the latest version of Qt was launched in June 2005 contains a new range of features and capabilities which accentuates even more developer productivity. The framework Qt gets so many new features.

The first version Qt 4 is therefore 4.0.0. Each new release of the minor version of Qt includes new features or improvements to existing functions. The latest versions are underway Qt 4.2 and Qt 4.3. It should be noted that some traits Qt 3 as well as the method recommended use of this version 3 have changed considerably in Qt 4; will therefore be careful when carrying a code Qt 3 to Qt 4.

A difference that affects the eye at first is the inclusion syntax. Formerly it was:
# include <qclassname.h>
Currently,
# include <QClassName>
suffice.
To include definitions for all classes dan contained a library, simply specify the name of this library. For example,
# include <QtCore>

II-A. The tools Qt 4
Qt comes with several commands and graphical tools to facilitate and accelerate programming. Some of these tools that already existed in previous versions:

Qt Designer: for the creation of forms (or sheets or frames)

Qt Assistant: aide Qt

Qt Linguist, lupdate, lrelease: Qt provides excellent support for translating applications into local languages.

qmake: to create the project file Qt. pro, this file is independent of the platform

Meta-Object Compiler (moc): generates information meta-object subclasses QObject (Qt is based around the object model Qt It is based entirely around the class QObject and tool moc )

User Interface Compiler (uic): generates C + + code corresponding to a file interface. Ui

Resource Compiler (rcc): inserts resources in the application during the compilation.

QtDBus XML compiler (qdbusxml2cpp): A tool for converting descriptions of D-Bus interface in source code C + +

D-Bus Viewer

Note: D-Bus is a mechanism CPI (Inter-Process Communication) and RPC (Remote Procedure Call) initially developed for Linux to replace the existing CPI solutions to create a unified protocol. It is useful, for example, when communication between a process system (printer and device drivers) and processes typical user. This mechanism is based on launching an application server providing central management applications connected to the bus and the proper delivery of messages from source to the destination.

The tool carrier code Qt 3 to Qt 4: qt3to4

The tool allows qt3to4 to migrate in a written code Qt 3 to Qt 4.

This tool can be used on an individual file. Cpp or a header. H and can also be applied to an entire project designated by the file. Pro.
C: \ Qt \ 4.3.0 \ bin> qt3to4 monfichier.cpp
C: \ Qt \ 4.3.0 \ bin> qt3to4 MyQtProject.pro
If you can use as a parameter a project qt3to4 translated all files listed in the file. Pro. The tool then modifies all files on the spot. So, we must make a copy of these files before running the port.

II-B. Portage a code Qt 3 to Qt 4
Programs compiled for Qt 3 must be recompiled to work with Qt 4. Qt4 includes new features and additional functionality abandon obsolete earlier versions hence its binary incompatibility with Qt 3.

Many things have indeed changed in version 4 compared to the previous version 3. For example, Qt Designer has changed considerably in Qt 4. There is then a tool uic3 order to move a file. Ui Qt 3 to a file. Ui valid for Qt 4.
Here are some major changes Qt Designer:

Designer works only with the file. Ui
On n'édite over the source code in Designer
The structure of the code generated by the uic has changed
The icons are no longer stored in the file. Ui but in a new type of resource file (*. qrc)
and so on.

II-C. The Qt 4.2
Qt 4.2 contains more features than ever before, it can then create more powerful applications with less work for the programmer. These new capabilities added to 4.2 are:

QGraphicsView allows you to write interactive applications that handle significantly to millions of 2D graphics objects with collision detection, multiple views, coordinate transformations and individual article and the ability to group articles integrated.
If you want to deploy your application Qt on the new Microsoft operating system (Windows Vista), this allows us to version 4.2 (version 4.3 is most suited for Vista). Famous applications like Google Earth already adapted to "look and feel" of Vista using Qt 4.2.
A new style of player you can develop quickly and easily interface of your application. This new style inspired by the CSS (HTML). A few lines of code enough to get a nice interface.
As with all new releases, a new look with more tools.
The improvements are more visible in Macintosh (Mac OS X)

II-D. The Qt 4.3
Qt 4.3 reveals many new features for developing an advanced user interface. The innovations in this release are primarily intended for development on Windows Vista and optimizations in the engine Qt OpenGL. Qt 4.3, the latest range presently is the version that contains the greatest change in the series Qt 4.

-- Qt 4.3 makes it possible to create an application to "look and feel" of Vista that Qt 4.2. The class QWindowsVistaStyle was then created. Qt Linguist 4.3 is now "Certified for Windows Vista."
-- It will introduce the script engine standard ECMA to be completely integrated in Qt, similar to other famous engines namely Mozilla Spider Monkey and WebKit.
-- The engine OpenGL Qt is improved
-- QMainWindow, the architecture of the main window Qt becomes more flexible. The main window now supports paradigms different style (tabs and bars titles vertical toolbar animated, etc.).
-- QSvgGenerator: possibility of saving operations rendering Qt in the form of the output SVG.
-- The QtNetwork module has been improved by introducing the use of commercial SSL.

II-E. Some differences compared to earlier versions

III. Installation
There is only one place to download Qt is on the website of the publisher and developer Trolltech both. You will be led to a page where fill in a form which is normally required to obtain the couple name validation serial number. To validate the installation of Qt, we must also specify the intended use (Commercial, Teaching, etc.). And the operating system on which you will install qt Note also that the Qt version 4 (Qt / Windows Open Source Edition) is available in open source (GPL), but we must MingW to compile. These sources are available on the website of Trolltech: http://www.trolltech.com/products/qt/downloads.

The installation of Qt under Visual C + + is very easy, just follow the instructions of the setup instructions.

MingW the compiler is only officially supported by the OpenSource version of Qt under windows.
The Qt / Windows Open Source Edition only supports MingW the compiler.

The download links that you receive by mail after filling out the form will be links addin to Visual C + + (Windows), a unique link for Linux / Unix and a link for Mac. So, for FDI other than Visual C + +, the ideal is therefore to download a Qt / Windows Open Source Edition. Besides, most of these FDI employ the compiler MingW include Code: Blocks and Dev C + +.
Simply launch the file recovered after downloading, it said MingW directory (C: \ Program Files \ CodeBlocks \ mingw32 for Code: Blocks) and then click next. The following steps are easy, click on Install at the end and the installation begins.

Compiling Qt4 Open Source under Visual C + +

I will lead you to an article by Aurélien Regat-Barrel who will explain details with how to compile Qt / Windows Open Source Edition under Visual C + + 2005 more accurately:

Compiling Qt 4 with VC + + 2005

IV. Initiation into application development with Qt


Well, now that Qt is properly installed, we can begin to return to the heart of the matter: the creation of an application qt Admire a little power of this library, what it can: not great! (From the example OverPainting)

I think Qt is a way to provide a graphic power to non-RAD IDE like Visual C + +, Dev C + +, Code: Blocks, etc.. even up to beat the powerful RAD C + +. The super users C + + Builder already has free tools as GLScene, Indy, Jedi ... namely myself Qt can see a way to deal with the other non-EDI "rapid aid application."

Various AddIn are made available to us by Trolltech Qt easy to use with an IDE or a specific platform.
IV-A. Integration of Qt for Visual C + +
This paragraph is aimed at users that publisher Microsoft.
A new toolbar will automatically appear in Visual Studio after installing the addin.

Microsoft distributes free Visual C + + 2005 in a limited but I think the features offered in this version are limited largely better than those of some C + + IDE free.
You can also install it manually: see the Tools menu> Customize> Add-ins and Macro Files and then check the box to Qt 4 Integration.
The plugin launch Qt Designer when you open a file. Ui which is actually an XML document and launch Qt Linguist every time you open a file. Ts. The files. Ui are XML files that describe a graphical user interface.
<ui Version="4.0">
  <class> TetrixWindow </ class>
  <widget class="QWidget" name="TetrixWindow">
   <property Name="geometry">
    <rect>
     <x> 0 </ x>
     <y> Represents 0 </ y>
     <width> 537 </ width>
     <height> 475 </ height>
    </ Rect>
   </ Property>
..................................
</ UI>
The resource file Qt's extension QRC with here is an example
<RCC>
     <qresource Prefix="/">
         <file> Tetrixwindow.js </ file>
         <file> Tetrixwindow.ui </ file>
         <file> Tetrixboard.js </ file>
         <file> Tetrixpiece.js </ file>
     </ Qresource>
</ RCC>
You can manually edit this file in an addition of other resources to a project Qt as an image for example, but is best left to Qt editing this file.

To learn how to manipulate Visual C + + 2005 and its new features, visit this tutorial a editors: start with VC + + Express 2005.

The difficulty of compiling an application Qt 4 under MsDev Visual C + + focuses on the compilation process and not on the sources of the library itself. It lacks, and the configuration files for different versions of Visual C + +.

IV-B. Application Hello World to Qt
This programme is the first application "Hello World" standard on the use of this vast library software. Here is the complete source corresponding:
# include <QApplication>
# include <QPushButton>

int main (int argc, char * argv [])
(
     QApplication app (argc, argv);

     QPushButton hello ( "Hi the World");
     hello.resize (100, 30);

     hello.show ();
     return app.exec ();
)
 

 


This code creates a window with a button which has the caption: Hi World!. Here is a screenshot:

The main () is the entry point of application. In "Qt programming", this function has increased with 2 parameters which then tell the program on the user's actions by events. app is an instance of the object QApplication who spent the main parameters as parameters of its manufacturer (argc and argv). hello is the push-button type QPushButton that redefines a size 100x30 after its inception. A "widget" is never visible at its inception, it is necessary to call a show () to view it.

IV-C. How to create a windows application Qt

We must, before starting to create an application with Qt, finish installing the plugin for FDI that you want.

Among the available commands on the toolbar Qt Qt choose New Project and in the dialog box that follows, we will choose Qt Windows Application. Two units files are then visible in the tree of files: main.cpp and mywidget.cpp (plus header mywidget.h) unless it has been renowned since the creation of the new project. A detailed (mywidget) derived from QMainWindow is created by default. You have probably experienced those endless nights only to create a form with some components above with a non-RAD IDE like Visual C + +. With Qt, to place components on a card or form, open the file. Ui with Qt Designer and it is sufficient to make a drag and drop components we need, more with Qt version 4, we l 'advantage of having components beautiful appearance.

Qt Designer is a tool available (layout) GUI powerful and creation of forms, which can create a user interface more quickly, but of good quality. With Qt Designer, Qt has a far better design than MFC Microsoft and Borland IU. Sub Qt 4, Qt Designer is included in the kit and used in EDI directly.

Designer provides different types of "form" depending on your use. You can choose one of these templates or open a form that was saved earlier.

Designer is a creator and publisher file UI. When a project is built Qt, UIC (User Interface Compiler) will create a header file (*. h), which gives us the construction of the "form". To access the form and content, we need to include the H file created by the UIC:
# include <ui_MyWidget.h>
The form created here is a descendant of QMainWindow but create a window descendant of QWidget is exactly the same way by choosing "Widget" to the creation of form.
class MyWidget: public QMainWindow
(
     Q_OBJECT
public:
     MyWidget (QWidget * parent = 0);
Private slots:
     Void on_aboutQtButton_clicked ();
private:
     Ui_MyWidget IU;
);
Q_OBJECT:

This is a macro specific Qt There are many types of macros Qt include among others: Q_PROPERTY (...) Q_CLASSINFO (Name, Value), Q_ENUMS (...) Q_FLAGS (...) Q_INTERFACES (...).

In addition to the manufacturer, the class contains methods private slots which are named according to the convention of naming self-login required by uic.

Here is the definition of manufacturer:
MyWidget: MyWidget (QWidget * parent)
     : QMainWindow (parent)
(
     ui.setupUi (this);
)
The method setupUi () is declared in the file created by uic: here Ui_MyWidget.h. The user interface is configured with this method. We put as an argument of this method to use this form itself as the container of the user interface
IV-D. Example 1: Hello World in another way
Demarrons by this application discovery qt This is the traditional Hello World on the creation of an application windows that I concocted.

Immediately explain the code corresponding to the window:
# include "ui_MyHelloWorld.h"

MyHelloWorld class: public QMainWindow
(
     Q_OBJECT

public:
     MyHelloWorld (QWidget * parent = 0);

Private slots:
Void afficherBoite ();
Void afficherBoite2 ();

private:
     Ui_MyHelloWorld IU;
);
The first line indicates the inclusion of the file compiled from file IU corresponding to the window created with Qt Designer.

Only the manufacturer to be encapsulated in the public section whose definition is as follows:
MyHelloWorld: MyHelloWorld (QWidget * parent)
     : QMainWindow (parent)
(
     ui.setupUi (this);
connect (ui.pushButton, SIGNAL (clicked ()), this, SLOT (afficherBoite ()));
connect (ui.pushButton_2, SIGNAL (clicked ()), this, SLOT (afficherBoite2 ()));
The new element is the function connect (). It creates a connection from the type of signal in the object sender (Sender) the method in the object receiver (Receiver). We must use macros SIGNAL () and SLOT () to specify the signal and the method. These two lines can thus affect respectively methods afficherBoite () and afficherBoite2 () at a click on the two push buttons program.

The two functions afficherBoite () and afficherBoite2 () are two ways to display message boxes in Qt as the famous MessageBox () MsgBox and other languages and frameworks. Here are their sources:
MyHelloWorld void: afficherBoite ()
(
     QMessageBox: about (this, tr ( "About me"), tr ( "My name is complete <b> Rija RANDRIANO </ b>. <br> This is my website: <b> http://randriano.developpez . com </ b> "));
)

MyHelloWorld void: afficherBoite2 ()
(
     QMessageBox: information (this, tr ( "About Me"), tr ( "I named <b> Rija RANDRIANO </ b>. <br> Here is my website: <b> http://randriano. developpez.com </ b> "));
)

Here is the link to the zip of this program: Application Hello World.



IV-E. Example 2: Program screenshot
It is a simple application to learn how to start programming with a more complex Qt 4.

Here is the code corresponding to the capture itself:
MaFenetre void: LancerLaCapture ()
(
     if (ui.CaptureSpinBox-> value ()! = 0)
         qApp-> beep ();
     originalPixmap = QPixmap:: grabWindow (QApplication: desktop () -> winId ());
     updateCaptureEcran ();

     ui.nouvBoutonCapture-> setDisabled (false);
     if (ui.CaseCacherFenetre-> isChecked ())
         show ();
)

MaFenetre void: updateCaptureEcran ()
(
     ui.CaptureEcran-> setPixmap (originalPixmap.scaled (ui.CaptureEcran-> size ()
                                                      Qt: KeepAspectRatio,
                                                      Qt: SmoothTransformation));
)
The UI elements. are the components (should see the zip for more) as a push button, spinbox, check box, and so on. The captured image will be displayed in a QLabel component, the component label Qt is different from other frameworks because it is often used to support an image Qt 4. Here is an example of inserting image in a QLabel:
     QLabel * label_1 = new QLabel (this);
     label_1-> setPixmap (QPixmap ( "/ images / logo_developpez.png"));
     label_1-> move (20, 20);
     label_1-> show ();
About:
private:
     QPixmap originalPixmap;
Qt provides us with four types of classes for imaging: QImage, QPixmap, QBitmap and QPicture. QImage is the most appropriate handling of an image and direct access to its pixels but QPixmap is optimized for display only image.
The attribute "originalPixmap" is used here to contain the captured image to be displayed through a QLabel. To save it in a file, you should:
originalPixmap.save (fileName, format.toAscii ());
I invite you to view the zip code to get the complete source.



Here is the link to the zip of this program: Shoot screen program.



IV-F. Example 3: The sample application provided with Qt Puzzle
PUZZLE is a part of the examples provided with Qt 4. This chapter is not to explain how the program instruction by instruction but only a global viewpoint.

This program demonstrates how to use the system drag and drop in a widget-type itemview.
Moreover, especially puzzle shows that we can make a drag'n drop a check of a view (itemview) to another widget.

Below are the files of this example:
draganddrop / puzzle / mainwindow.cpp
draganddrop / puzzle / mainwindow.h
draganddrop / puzzle / pieceslist.cpp
draganddrop / puzzle / pieceslist.h
draganddrop / puzzle / puzzlewidget.cpp
draganddrop / puzzle / puzzlewidget.h
draganddrop / puzzle / main.cpp
draganddrop / puzzle / puzzle.qrc
The application of this principle is simple: the elements of the puzzle are obtained from an image file that was loaded and that the program was cut and disorderly in the sight of left and put in place the pieces in sight Right.

The draft does not use file. Ui because the interface is fully established by code.

The source of the main entry point () is as follows:
# include <QApplication>

# include "mainwindow.h"

int main (int argc, char * argv [])
(
     Q_INIT_RESOURCE (puzzle);

     QApplication app (argc, argv);
     MainWindow window;
     window.openImage ( "/ images / example.jpg");
     window.show ();
     return app.exec ();
)
Q_INIT_RESOURCE allows to force the initialization of resources indicated by the base name of the file with the extension. Qrc. Thus, there is no more need to join the roster example.jpg with the program because the image will be included in the executable binary file or program (the so-called "resource" in fact).
The main () is typical of all applications using a single window except

window.openImage ( "/ images / example.jpg");
The class of the main window is defined by mainwindow.cpp and mainwindow.h. This allows us to learn a little more about this feature openImage ().
PiecesList class;
PuzzleWidget class;
QListWidgetItem class;

MainWindow class: public QMainWindow
(
     Q_OBJECT

public:
     MainWindow (QWidget * parent = 0);

public slots:
     void openImage (const QString & path = QString ());
     Void setupPuzzle ();

Private slots:
     Void setCompleted ();

private:
     Void setupMenus ();
     Void setupWidgets ();

     QPixmap puzzleImage;
     PiecesList * piecesList;
     PuzzleWidget * puzzleWidget;
);
The definition of the method openImage of the class of the main window is as follows:
void MainWindow: openImage (const QString & path)
(
     QString fileName = path;

     if (fileName.isNull ())
         fileName = QFileDialog: getOpenFileName (this,
             tr ( "Open Image"), "", tr ( "Image Files (*. png *. jpg *. bmp )"));

     if (! fileName.isEmpty ()) (
         QPixmap newImage;
         if (! newImage.load (fileName)) (
             QMessageBox: warning (this, tr ( "Open Image")
                                  tr ( "The image file could not be loaded.")
                                  QMessageBox: Cancel);
             return;
         )
         puzzleImage = newImage;
         setupPuzzle ();
     )
)
Quite simply, this piece of code can simply load the image by opening the dialog file selection.
This therefore brings us to another important function that cuts the image loaded in pieces of the puzzle: setupPuzzle ().


void MainWindow: setupPuzzle ()
(
     int size = qMin (puzzleImage.width (), puzzleImage.height ());
     puzzleImage = puzzleImage.copy ((puzzleImage.width () - size) / 2,
         (puzzleImage.height () - size) / 2, size, size). scaled (400,
             400, Qt: IgnoreAspectRatio, Qt: SmoothTransformation);

     qsrand (QCursor: pos (). x () ^ QCursor: pos (). y ());

     model-> addPieces (puzzleImage);
     puzzleWidget-> clear ();
)
Finally, to understand the system drag and drop, we must view the sources puzzlewidget.h and puzzlewidget.cpp. It then manipulates the mouse events occurring form of a class PuzzleWidget daughter QWidget type is that which allows the drag of sight from left to right eye.
     void dragEnterEvent (QDragEnterEvent * event);
     void dragLeaveEvent (QDragLeaveEvent * event);
     void dragMoveEvent (QDragMoveEvent * event);
     void dropEvent (QDropEvent * event);
     void mousePressEvent (QMouseEvent * event);
     void paintEvent (QPaintEvent * event);
Finally, I give you a link to the zip of this program but I said that the latter exists in the examples of Qt: Download the zip program

V. Conclusion
To address Qt, we must be able to understand the language of Shakespeare as most related documents are English. But I think it should not be an obstacle: the purpose of my article to discover Qt for beginners but passionate C + + and Qt rediscover another angle for super users.
What Qt is capable? I believe that many more than other tools C + + existing offer. Admire a little beauty of the graphical environment KDE Linux to understand its effectiveness in terms of look and feel. There are many famous software currently using Qt not only for appearance but also for its functions networks, access to databases namely software Adobe, Google, NASA, ... However, there are documentations in french written by others to plunge into the world qt

Qt 4 and C + +: Programming Interface GUI
  http://c.developpez.com/livres/

VI. Presentation of Trolltech
Trolltech is a computer firm based in Oslo, Norway. Qt library multi-platform is its main product, this tool is developed with C + +. Trolltech develops many other derivatives as Qt for embedded systems Qtopia, Qt version for Java: Qt Jambi, and so on.

Help on Qt
Personally, I advise you to browse first documentation Qt if you begin to Qt: it is contained in the file doc directory qt Then you can ask your questions in the forum Qt:

FORUM C / C + +> Qt

Acknowledgments
Let me express my sincere thanks for hiko-seijuro for its advice and gege2061 for its corrections to this article.

Download: Qt - C + + Fast for Windows, Mac OS X and Linux from a single source code.
Comments
Add New Search
Anonymous   |200.5.236.xxx |2008-10-31 14:25:37
bla
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 14 June 2008 )
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials