Qt/Lesson 2

From Wikibooks, open books for an open world
< Qt
Jump to navigation Jump to search

Signals and Slots[edit | edit source]

You need to use Signals and Slots to give functionality to various components of Qt. For example, you might need the program to close the window, if the user clicks the Close Button.

#include <QApplication>
#include <QPushButton>

int main(int argc,char *argv[])
{
    QApplication A1(argc,argv);
    QPushButton *Close = new QPushButton("Close");
    QObject::connect(Close, SIGNAL(clicked()), &A1, SLOT(quit()));
    Close->show();
    return A1.exec();

}

When the user clicks the Close Button, the Signal clicked() is generated from the Close Button and is associated with quit slot of the A1(Our Application)using the QObject::connect call. The quit() function closes the window and returns control to the Operating System.