QT – hello world – signals and slots

QT framework is a great application stack that allows for cross development (between different OS’s) without having to re-do the code for e.g.Linux/Windows different setups. It is similar to Java and C Sharp (C#) in that way, but the difference with them is that they compile into a native object which can then run within a virtual machine where as the QT framework is more designed to be compiled within each setup, which should make it quicker as well because it will be in the native code and not running in a virtual machine.

The nice thing about QT is that it has its own SIGNAL and SLOTS, similar to C Sharp (C#) events process where you can link something happening to when something else has just happened (e.g. moved a value on a slider bar and a integer value alters as well).

I shall go into more detail with SIGNAL’s and SLOT’s in the next tutorial for QT development, but in essence they are defined within the class of the object itself and then you link them with a “connect” syntax. e.g.

    object::connect(object_to_send_signal, SIGNAL(signal_of_the_object_that_will_be_emit),
                        object_to_pick_up_signal, SLOT(function_to_call_once_signal_has_been_emitted));

so you will link/connect a objects that sends a signal to a slot that receives the signal.

Here is a basic GUI for a QT application that will have a button that says “QUIT” and also a signal that is emitted once that button class clicked() has happened, that links to the application (QApplication object) that has a “quit” function that stops the program from executing.

To start with we first include the basic header files and then create a QApplication (the gui as such) and then a QPushButton object with the default text being “Quit”.

    QApplication app(argc, argv);
    QPushButton *button = new QPushButton("Quit");

once this has happened we then create the connection between the clicking of the button to the application “quit”ing function.

    QObject::connect(button, SIGNAL(clicked()),
                        &app, SLOT(quit()));

we have only created the button but not placed it on the screen, so we use the show method within the button class and then run the application execution function to actually run the GUI program

    button->show();
    return app.exec();

if you save this file within a directory called quit and call this file quit.cpp

#include <QApplication>
#include <QPushButton>
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QPushButton *button = new QPushButton("Quit");
    QObject::connect(button, SIGNAL(clicked()),
                        &app, SLOT(quit()));
    button->show();
    return app.exec();
}

then change to that directory within your console/command line and then we need to build up the QT compiling project files.

qmake -project

this will make the quit.pro file within that same directory (it uses the same name as the directory name), and within that file is

######################################################################
# Automatically generated by qmake (2.01a) Wed Feb 24 11:49:39 2010
######################################################################
 
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
 
# Input
SOURCES += quit.cpp

which basically means that the application uses the source files of quit.cpp, you then need to create the Makefile (which is the actual compiling c++ file that g++ uses to create the application)

qmake

so the directory will be something similar to

Makefile  
quit.cpp
quit.pro

within Linux you can just now run the make command which will make the application “quit” but without windows you would need to have the nmake command instead, so run either make or nmake and within the directory now will be a quit application that you can run and the output would be a simple GUI with a button on it that says “Quit” and once you click that button the application will quit.

Leave a Reply

Your email address will not be published.