Skip to main content

QT - Signal-slot mechanism

This is first article describing some aspects of programming with Qt framework. I am going to focus on most important basic feature of Qt framework: signal-slot mechanism.

At the beginning take a look on below QT code:

Output of that code is this window:


This quick code is something like "Hello world" of Qt programming. Let's take a detailed look on the code.

In point I we are declaring QApplication object which will contain indefinite loop which will be invoked after program starts in order to wait for user interactions. We can also analyse command line input parameters in this line, but we are not doing it because we do not have any input parameters for our application.

In point II we are declaring QPushButton object which will be the only object of our application. As only parameter of our definition we are putting text which should be displayed on our button - "Hello World".

Point III is key point of this example. It presents basic usage of signal-slot mechanism. We are using QObject::connect function here to bind signal and slot.
This function has following syntax:
QObject::connect(senderObject, SIGNAL(senderSignal), receiverObject, SLOT(receiverSlot))

Where:
  • senderObject - object where signal is send from
  • SIGNAL(senderSignal) - signal which is send to receiverObject
  • receiverObject - object will should receiver sent signal
  • SLOT(receiverSlot) - slot function which should be invoked after receiving signal in receiverObject
In our case we are connecting signal clicked() from our button object with function quit() of our application. Signal clicked() is emitted when user clicks on button object. It means that when user clicks on button object signal clicked() will be captured by receiverObject. Then receiverObject runs function quit(). Than function closes our application.

Basic idea of QT signal-slot mechanism is invoking some function of one QT object by sending signal from another QT object.

In point IV we are invoking function show() of our QPushButton which displays our button on screen.

In point V we are starting our application. Function exec() contains infinite loop which will listen for user interactions.

As usual, code of this example you can find on our github account: Signal-Slot mechanism

Comments

  1. very good and quick explanation - more QT articles, please

    ReplyDelete

Post a Comment

Popular posts from this blog

GDB Automatic Deadlock Detector

Have you ever had a problem with detection deadlock between threads in your C/C++ application? Would you like to do that automatically? Try this GDB Automatic Deadlock Detector from my github: GDB Automatic Deadlock Detector Picture source: http://csunplugged.org/wp-content/uploads/2015/03/deadlock.jpg1286488735

STL - count and count_if algorithms

One of the basic and most useful STL algorithms is algorithm which can be used to count number of elements within selected container according to specified criteria. In order to do that we can use std::count or std::count_if algorithm. std::count (firstElementIterator, lastElementIterator, elementForSearch) - is function which will go through container using firstElementIterator and lastElementIterator and return number of container elements which value is equal elementForSearch std::count_if (firstElementIterator, lastElementIterator, UnaryPredicateFunction) - is function which examine range from firstElementIterator to lastElementIterator and return number of container elements which fulfill UnaryPredicateFunction criteria. UnaryPredicateFunction is function having following signature: bool functionName(const Type& a) . So, count_if returns number of elements where UnaryPredicateFunction returns true for. For better understanding let's take a

Advanced C++ - Stack unwinding

Stack unwinding is normally a concept of removing function entries from call stack (also known as Execution stack, Control stack, Function stack or Run-time stack). Call Stack is a stack data structure that stores active functions' addresses and helps in supporting function call/return mechanism. Every time when a function is called, an entry is made into Call stack which contains the return address of the calling function where the control needs to return after the execution of called function. This entry is called by various names like stack frame , activation frame or activation record. With respect to exception handling , stack Unwinding is a process of linearly searching function call stack to reach exception handler. When an exception occurs, if it is not handled in current function where it is thrown, the function Call Stack is unwound until the control reaches try block and then passes to catch block at the end of try block to handle exception. Also, in this proc