How to use Signals and Slot in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData


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]

  1. include <QApplication>
  2. include <QPushButton>
  3. 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(); }

Screen Shot[edit | edit source]

File:Signal1.jpg

File:Signal2.jpg