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:
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
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
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
very good and quick explanation - more QT articles, please
ReplyDelete