Qt Signals And Slots

Objectives

  • Learn the advantages of signals/slots
  • Understand the concept of signal/slots
  • Learn how to connect signals to slots
  • Be able to use and define your own signals/slots

Meta-Object System

  • Extends C++ with dynamic features.
  • Features such as
    • Mechanism to access any function in the class (used by signals and slots)
    • Class information
    • Translate strings for internationalization
    • Dynamic properties

Signals and Slots

  • Observer pattern
  • Type-safe callbacks
  • Many-to-many relationship
  • Implemented in QObject
  • Advantages:
    1. loose coupling: The key advantage of the signals and slots is that the caller does not have to know anything about the receiver and vice versa.
    2. Only connect the signals you need, while in a listener you need to implement also the methods you won't use.
    3. Slots are automatically disconnected when the receiver is deleted.
    4. You can directly connect signals to slots, without having to implement a listener method calling another method
    5. when implementing your own signals/slots, there is no need to do the listener management yourself as this is done by the qt object system

Signal

  • A signal is a way to inform a possible observer that something happened.
  • For example
    • A QPushButton is clicked.
    • An asynchronous service handler is finished.
    • Value of QSlider changed.
  • Member functions
  • Signal is sent, or emitted, using the keyword emit.

Slot

  • A slot is a function that is to be executed when a signal has been emitted.
  • Continuing the signal examples…
    • (When QPushButton is pressed), close QDialog
    • (When service is ready), ask for the value and store it
    • (When QSlider value is changed), show a new value in QLCDNumber

Connecting Signals and Slots

  • To setup the signal-slot connection, we must first determine the connection.
  • Syntax: connect(sender, SIGNAL(signal), receiver, SLOT(slot)));

Example 1: When the button is clicked the application closes.

#include <QtGui>
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
 
    //QLabel label = new QLabel("Not pushed");
    QPushButton *button = new QPushButton("click me");
    QObject::connect(button, SIGNAL(clicked()), &app, SLOT(quit()));
    button->show();
 
    return app.exec();
}

Features

  1. One signal can be connected to many slots
  2. Many signals can be connected to the same slot
  3. A signal can be connected to another signal
  4. Connections can be removed

Examples

Example 1: Private slot that checks for username and password.

mywindow.h

#ifndef MYWINDOW_H
#define MYWINDOW_H
 
#include <QtGui>
 
class MyWindow : public QDialog
{
      Q_OBJECT
public:
    MyWindow();
 
private slots:
    void checkCredentials();
 
private:
    QLabel *labelUsername;
    QPushButton *button;
    QLabel *labelPassword;
    QLineEdit *editUsername;
    QLineEdit *editPassword;
    QLabel *label;
    QVBoxLayout *mainLayout;
};
 
#endif // MYWINDOW_H

mywindow.cpp

#include "mywindow.h"
 
MyWindow::MyWindow()
{
    setWindowTitle("Signals and Slots Example");
 
    //add some widgets
    labelUsername = new QLabel("Username");
    button = new QPushButton("Submit");
    labelPassword = new QLabel("Password");
    editUsername = new QLineEdit();
    editPassword = new QLineEdit();
    editPassword->setEchoMode(QLineEdit::Password);
    label = new QLabel();
 
    connect(button, SIGNAL(clicked()), this, SLOT(checkCredentials()));
 
    mainLayout = new QVBoxLayout; //vertically
    mainLayout->addWidget(labelUsername);
    mainLayout->addWidget(editUsername);
    mainLayout->addWidget(labelPassword);
    mainLayout->addWidget(editPassword);
    mainLayout->addWidget(button);
    mainLayout->addWidget(label);
    setLayout(mainLayout);
 
}
 
void MyWindow::checkCredentials()
{
    if(editUsername->text() == "chrys")
    {
        if(editPassword->text() == "password")
        {
            label->setText("Correct");
        }
    }
    else
        label->setText("Wrong");
}

main.cpp

#include <QtGui>
#include "mywindow.h"
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 
    MyWindow myWindow;
    myWindow.show();
 
    return a.exec();
}
qt
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.