Showing posts with label hello world using QT. Show all posts
Showing posts with label hello world using QT. Show all posts

Basic QT Tutorial

Basic Understanding of QT/ Tutorial for QT
Qt is cross platform UI widget toolkit, but also more that that. It is extensive application development framework and suitable also for implementing non-GUI related programs.

Qt supports symbian, maemo, windowsCE mobile platforms.

Qt API is implemented in C++.bindings for other languages like python, java, ruby also exist.

Qt Cross Platform Architecture.





The implementation of qt uses the private implementation programming idiom which separates the interfaces from the platform specific changes. the application just needs to be re-complied for the target platform in the corresponding development environment.

Qt Class Library



Qt object model

Qt expands C++ with Meta Object Model, it adds the following features to c++.

• Hierarchical and query-able object trees.
• Signals and slots mechanism for object communication.
• Events, Event-Filters and Timers.
• String Translation.
• Guarded pointers.
• RTTI[Type Identification and dynamic casting]
• Discoverable object properties.

QObject hierarchy tree

• QObject is the heart of Qt object model and base class of all Qt Objects.
It provides object trees and object ownership with parent-child relationship.
• A parent owns its children, the mechanism provides memory management for object trees, but no garbage collection for other allocations.
• Parent controls the childrens visibility,but a child object can be explicitly hidden.

Object Tree




Here The top most dialog that has a layout set and a push-button that is outside the layout.
within the horizontal layout there is a label,line-edit and a push-button.

Meta –object system
Meta –object system is based on 3 things.

• QObject class
• QObject macro
• Meta object compiler

• The QObject base class has capabilities to support meta object system(connect method that connects a signal to a slot.)
• Q_OBJECT which is declared inside the private section of the class,states that it requires meta object compiler.
• Meta object compiler is executed automatically before the actual compilation,it generates the c++ code which required for the compilation process.

QApplication
QApplication is the one class that handles event loop and the main settings of an application.
No matter howmany widgets may present in an application there must exist one QApplication instance.

The responsibilities of QApplication includes

• Perform event handling.
• Manage application settings like[screen size,font,application stylesheet and many other tasks].

Events in Qt

When something of interest for the application occurs,for example
A key is pressed,
A mouse is moved,
A Network packet has arrived.

Then an event is created. It is represented as an object of the QEvent class. such as QFocusEvent.Information about the event is enclosed with in this event object.
Events are received and handled by event handlers of QObject.The actual event handler depends on the type of event that has occurred. like if the event mouse movement occurs it is handled by
mouseMoveEvent event handler.
Events are mostly created by the underlying window system but they can also be created by the developer too.The application contains an eventloop that manages and sends event to the objects they are meant for.Eventloop is started by calling the QApplication “exec” method after initilisation,in the beginning of every Qt application.typically in the main function.
Generally,no longer interaction can take place before starting this main event loop.

Signals and Slots

signals and slots offer a way to communicate between QObjects.

A Signal can be emitted by a QObject.
A Slot is a member function which can be connected to a particular slot, or can be called in response to a particluar emitted signal call.
Many predefined signals and slots exist.but subclasses can define and add their own ones.

Signature of signal should match the receiving slot.Another common way to implement observer design pattern is to declare an interface class which is then implemented by all observers.

Signals and Slots are easier to use and they dont need to implement an interface.
Signals and Slots offer loose coupling with many to many relationship by nature.it means a class which emits a signal need not know to which slot it is going to connect to.

Signals
A signal is a way to inform a possible observer that something of interest has occurred in side the observer class.
Some example are.

1.A Http request has been sent to a server and asynchronous handling of request has been finished.
QHttp emits a “requestFinished(int id,bool error)”.
2.A value of slider has been changed.
Qslider emits a valueChanged(int value) signal.
3.A Pushbutton has been clicked.
QPushButton emits a clicked(bool checked) signal.

Signals are sent using the keyword “emit”.
Signals are declared by the developer and implemented by the "moc"[meta object compiler.]

1.Signals can never have return type.[void or any return type is not allowed for signals]
2.signals can take any number of arguments.
3.Inorder to connect a signal with a slot the signature of the signal should match with the receiving slot.
And the number of arguments should be equal to the arguments of the slot.

QT Tutorials

QT is a GUI programming using the Qt toolkit. It doesn't cover everything; the emphasis is on teaching the programming philosophy of GUI programming, and Qt's features are introduced as needed. Some commonly used features are never used in this tutorial.

This first program is a simple "Hello world" example. It contains only the bare minimum you need to get a Qt application up and running. The picture below is a screenshot of this program.

Below is the code :

#include 
 #include 

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

     QPushButton hello("Hello world!");
     hello.resize(100, 30);

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

Line by Line Walkthrough

#include

This line includes the QApplication class definition. There has to be exactly one QApplication object in every GUI application that uses Qt. QApplication manages various application-wide resources, such as the default font and cursor.
 
#include 
This line includes the QPushButton class definition. For each class that's part of the public Qt API, there exists a header file of the same name that contains its definition.
QPushButton is a GUI push button that the user can press and release. It manages its own look and feel, like every 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 well as the widget's content. A QPushButton can show either a text or a QIcon.
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 program about the user's actions via events.
The argc parameter is the number of command-line arguments and argv is the array of command-line arguments. This is a standard C++ feature.
QApplication app(argc, argv); The app object is this program's QApplication instance. Here it is created. We pass argc and argv to the QApplication constructor 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 decremented accordingly. See the QApplication::argv() documentation for details.
The QApplication object must be created before any GUI-related features of Qt are used.
QPushButton hello("Hello world!"); Here, after the QApplication, 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 second argument to the QPushButton constructor), the button will be a window of its own, with its own window frame and title bar.
hello.resize(100, 30); The button is set up to be 100 pixels wide and 30 pixels high (excluding the window frame, which is provided by the windowing system). 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 call QWidget::show() to make it visible.
return app.exec(); } This is where main() passes control to Qt. QCoreApplication::exec() will return when the application exits. (QCoreApplication is QApplication's base class. It implements QApplication's core, non-GUI functionality and can be used when developing non-GUI applications.)
In QCoreApplication::exec(), Qt receives and processes user and system events and passes these on to the appropriate widgets.
You should now try to compile and run this program.
The tutorial examples are located in Qt's examples/tutorial directory. They are automatically built when you build Qt.
If you have typed in the source code manually, you will need to follow these instructions: To compile a C++ application, you need to create a makefile. The easiest way to create a makefile for Qt is to use the qmake build tool supplied with Qt. If you've saved main.cpp in its own otherwise empty directory, all you need to do is:
qmake -project qmake The first command tells qmake to create a project file (a .pro file). The second command tells it to create a platform-specific makefile based on the project file. You should now be able to type make (or nmake if you're using Visual Studio) and then run your first Qt application!

Running the Application

When you run the application, you will see a small window filled with a single button, and on it you can read the famous words: "Hello world!"