Tuesday 8 January 2013

Basic Qt Programming: Creator and First program


Before going to start programming in Qt here we explaining how to start with Qt Creator to make your project.
 Launch Qt Creator and go on file menu and choose new it will open following window:



You have to choose Projects >>Application>>Qt Console Project and click on “choose”
Then in next window give project name and select location and click on “Next”.
Select “Desktop” in target setup window click “Next”                  
Click “Finish” in summery window.
This will open your project.
In the left you can see project tree under which one .pro file and one .cpp file present.
.pro file is Qt Project file which contains all the information required by qmake to build your application, library, or plugin.
qmake is a tool that helps simplify the build process for development project across different platforms.
Your .pro file will have some content as:


#-------------------------------------------------
#
# Project created by QtCreator 2013-01-07T00:02:18
#
#-------------------------------------------------
 
QT       += core
QT       -= gui
TARGET = MyProject
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

Lines with # are comments
After comments first line indicate that project include Qt Core library and next line is for excluding Qt GUI module.
Since we are going to learn Qt GUI programming
So edit
 QT       -= gui
to
QT       += gui

For more info about .pro file syntax refer http://qt-project.org/doc/qt-4.8/qmake-project-files.html


Simple Qt program:

 A simple program to make a push button and its line by line explanation: 
 
#include <QApplication>
#include<QPushButton>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
    QPushButton * pushbutton = new QPushButton("Hello");
//creating pushbutton and setting its text as Hello
    pushbutton->show();
    return a.exec();
}

The QApplication class manages the GUI application's control flow and main settings. The QApplication constructor requires argc and argv because Qt supports a few command-line arguments of its own.

To use pushbutton we have to include its header QPushButton.  We can pass string to its constructor to set pushbutton’s text. 

show()  method is called  to make it visible.

return a.exec() enters the main event loop and waits until exit() is called, then returns the value that was set to exit() (which is 0 if exit() is called via quit()).

It is necessary to call this function to start event handling. The main event loop receives events from the window system and dispatches these to the application widgets.

No comments:

Post a Comment