Learn Qt

Teaching Qt® to the world!

Flower

A First Look at Signals and Slots

Signals and slots is one of the key components that makes Qt unique. It allows you to connect events to slots so that you can react to button clicks, checkboxes being checked, text being edited – but also to values being changed, timers timing out, and so much more.

To make your Qt application do something, all you have to do is to connect signals to slots. This can be done in three ways. Last time we had a look at two of them, lets repeat:

Connecting by name. This means naming the slot on_widget_signal. This connection is made when you call setupUi and lets you connect simple triggering events to a slot. The slot’s name isn’t always the best from a readability and reusability standpoint, but it is quick and easy. Also, you can make this type of connections by right clicking in the Designer view of QtCreator.

Connecting using the connect method. By specifying a signal source object, the signal name, a destination object and a slot name you can connect any signal to any slot.

Then, there is the last way:

Connecting using Designer. This is when you actually “draw” your connections in Designer, and this is what we will look at today.

The last blog entries have been using the QtCreator wizard’s QMainWindow based template. This time around, we will use th QWidget based template. To create such a project, simply run through the wizard for creating a new Qt 4 Gui project and make sure to use a QWidget instead of a QMainWindow as the base class.

widget-tempate

Creating a GUI project based on a QWidget.

Then located with widget.ui file and double click it to open the Designer view. Here, drag and drop a QPushButton onto the widget and change its text to “Close”. You can either right click and choose to change its text, or you simply double click it and edit the text in place.

A widget with a QPushButton.

A widget with a QPushButton.

This gives you a basic widget to work from. Now comes the fun part. Until now, you’ve been working in the widget editing mode of Designer. Now it is time to edit some signals and slots, so locate the mode changing buttons in the tool bar and switch to the signals/slots editing mode.

The edit signals/slots working mode.

The edit signals/slots working mode.

In this working mode, Designer no longer lets you move widgets about. Instead, you can drag (click and hold) from one widget to another to make a signal/slot connection. Start by dragging from the QPushButton to the QWidget itself (the actual form).

Dragging from the button to the widget.

Dragging from the button to the widget.

When you release the button, a dialog showing the available signals will appear. Pick the clicked() signal and connect it to the close() slot. You will have to check the show signals and slots inherited from QWidget check box in order to see the slot.

Connecting a signal to a slot (notice the checked box).

Connecting a signal to a slot (notice the checked box).

That was it – you’ve made a connection. Save the widget.ui file and build and run the application and you can test your button.

Be Sociable, Share!

2 Responses to “A First Look at Signals and Slots”

  1. April 20th, 2009 at 16:30

    Aprende Qt mediante tutoriales | KDE Blog says:

    […] A First Look at Signals and Slots (Primer vistazo a seƱales y slots) – Shows how to connect signals and slots, making applications react to […]

  2. April 21st, 2009 at 04:07

    linkfeedr » Blog Archive » Johan Thelin: New Chapter - RSS Indexer (beta) says:

    […] was no valid link found in the RSS Feed. Because of this the whole article is displayed. Added a new chapter to the Learn Qt site and created a TOC type of page for the tutorial entries. Report This Post: […]

Leave a Reply

You must be logged in to post a comment.