Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to use Signals and Slot in Qt

From 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 01:17.
39 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×