How to use Signals and Slot in Qt
Jump to navigation
Jump to search
Introduction[edit | edit source]
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[edit | edit source]
- 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();
}