How to use Signals and Slot in Qt
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Article
Keywords: QPushButton,SIGNAL,SLOT
Created: james1980
(26 Dec 2008)
Last edited: hamishwillee
(11 Oct 2012)
Introduction
Here is the simple code which will demonstrate the use of the signals and slot in Qt. In this code first button does not connected to any function and it does nothing when you click on it. Second button emits a signal whenever you click on it and it is connected with the function quit.
Code
#include <QApplication>
#include <QPushButton>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget* win = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(win);
QPushButton* but1 = new QPushButton("Hello James");
but1->resize(100,20);
layout->addWidget(but1);
QPushButton* but2 = new QPushButton("Bye Bye");
but2->resize(100,20);
layout->addWidget(but2);
QObject::connect(but2, SIGNAL(clicked()), &app, SLOT(quit()));
win->show();
return app.exec();
}




This article shows the use of signal and slots in QT for S60.In this article there is a use of Qpushbutton.using this button how to apply signal and slots is Shown in this article.
In this article first button HELLO JAMES is Don't have any Signal so it will do nothing whwn you pres that button.
But when you press the button "Bye Bye" at that time you will exit from that emulator.because it contains the signal EXIT.
--nayan_trivedi
Article shows how the tool of Qt can be hook up to the application. In Qtimer critique, it also shows the connection of a timer and application using signal and slot. Linking this article with “Qtimer article” is the best improvement.
[--fasttrack 19:09, 15 September 2009 (UTC)]
Although the concept of signals and slots is explained in other article Understanding Signals and Slot in Qt by same author, the working code example will helps beginners to understand the concept of signal ans slots mechanism. The example shows how clicked() signal of QPushButton is connected to quit() slot of QApplication, whenever use click on button a clicked event is fired and which call quit slot of QApplication.
--savaj 04:55, 28 September 2009 (UTC)