Wednesday, April 29, 2015

Basic Qt

Prerequisite
  1. Qt Creator with MinGW or Visual C++

  2. Knowledge of C++

  3. Qt Library
If you have limited experience with C++, please take a look at C++ tutorial before proceeding this tutorial.
The only way to learn a new programming is by starting to write program and play around with it. In this tutorial, we start to write a basic Qt program by using basic functionality of C++ provided by Qt to create a small graphical user interface (GUI). As we have already experienced when we start to learn the programming language, we usually begin with "Hello World". Therefore, we also start with the "hello Qt".

Create HelloQt.cpp

1 #include <QApplication>

2 #include <QLabel>

3 #include <QPushButton>

4 int main(int argc, char *argv[])

5 {

6    QApplication app(argc, argv);

7    QLabel *helloLabel = new QLabel("<font color=blue><b><center>Hello  
      Qt!</center></b></font>");

8    QPushButton *helloButton = new QPushButton("Push me to say Hello");

9    QObject::connect(helloButton, SIGNAL(clicked()), helloLabel,  
      SLOT(show()));

10   helloButton->show();

11   return app.exec();

12   }

If you use Qt creator, you will need to create project before you can create your cpp file. After creating project, please add core gui and widgets as below to the project file that has .pro extension.

QT += core gui
QT += widgets

In case you do not use Qt creator, you can create cpp file to write a code and generate the project file later. Without Qt creator, you can use command prompt (Window) or terminal (Unix-like system) to run your Qt program. From there, go to the folder you retain your cpp file. Then you can generate a platform-independent project (HelloQt.pro) by typing the following command:

qmake -project

To generate a platform-specific makefile from the project file

qmake HelloQt.pro

Then type make to build the program

  • Run it by tyapping HelloQt on Windows
  • ./HelloQt on Unix
  • open hello.app on Mac OS X

Line 1, 2, 3 is a preprocessor command like another c++ program to tell the compiler to include QApplication, QLabel, and QPushButton.

In Qt, there is a header file with the same name (and capitalization) as the class that contains the class's definition for every class as illustrated in the code above.

Line 5 constructs a QApplication object to manage application-wide resources. The QApplication constructor needs argc and argv because Qt supports a few command-line arguments of its own.

Line 7 create QLabel and QPushButton widget displaying "Hello Qt" and " Push me to say Hello" respectively. Line 9 connects the event to the QPushButton.

Qt's widgets emit signals to indicate user action or a change of state. As shown in the code above, QPushButton will emit clicked() signal when the user clicks on the button. Signal is able to connect to a function called slot so that when the signal is emitted, the slot is automatically executed We will give detail about in the next tutorial. Line 10 makes the button visible.



No comments:

Post a Comment