×
Namespaces

Variants
Actions

How to use Signals and Slot in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Symbian
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();
}

Screen Shot

Signal1.jpg

Signal2.jpg

This page was last modified on 11 October 2012, at 04:17.
65 page views in the last 30 days.
×