Signal Slots Qt Example

Qt 5 signals and slots mechanism. How signals and slots in Qt differ from the callback architecture in other widget toolkits. A Qt basics tutorial. How to add signals and slots in Qt Creator.

To use it, create a QTimer, connect its timeout signal to the appropriate slots, and call start. From then on, it will emit the timeout signal at constant intervals. In this example, we'll use Qt Console application. Modifying widget signals to pass contextual information to slots. Signals are a neat feature of Qt that allow you to pass messages between different components in your applications. Signals are connected to slots which are functions (or methods) which will be run every time the signal fires. Many signals also transmit data, providing. Qt documentation: Multi window signal slot connection. A simple multiwindow example using signals and slots. There is a MainWindow class that controls the Main Window view.

Part 9 of the Qt Creator C++ Tutorial

What are Qt 5 Signals and Slots?

Very basically, signals and slots in Qt allow communication between objects.

In Qt, a signal is emitted when an event occurs. A slot is a function that is called when a signal is emitted. For example, a push button emits a clicked signal when clicked by a user. A slot that is attached to that signal is called when the clicked signal is emitted.

Multiple signals can be connected to any slot. Signals can be connected to any number of slots.

Signal Slot Qt Example

Most of the details of signals and slots are hidden in their implementation in Qt. At this stage of the tutorial series we do not look in depth at signals and slots.

Using Signals and Slots in Qt Creator

There are several ways to use signals and slots in Qt Creator projects. This includes manually adding them in code. Here we briefly look at the easier ways to use signals and slots to respond to events. Events are generated by users interacting with widgets in an application. These events cause signals to be emitted. Corresponding slots, or functions then run.

Qt 5 Signals and Slots Demonstration

The following image shows the application built in this section using Qt Creator. It demonstrates some methods of using signals and slots.

Each section below shows a method of adding signals and slots to a Qt Creator program. Watch the video embedded near the top of this page for details.

Add a Slot to a Button for the Clicked Signal

Place a push button on the main window. Right click the push button and select Go to slot… to add code for the clicked signal.

Connect a Slider to a Progress Bar Visually

Place a Horizontal Slider and a Progress Bar on the main window.

Press F4 on the keyboard. This toggles to Edit Signals/Slots mode.

Drag to connect the slider to the progress bar.

Press F3 to change back to Edit Widgets mode.

Connect a Slider to a Progress Bar with Code

Place a second Horizontal Slider and a Progress Bar on the main window.

Signal Slots Qt Example

Right-click the Horizontal Slider. In the menu that pops up, click Go to slot…

In the dialog box that pops up, select sliderMoved(int). Click the OK button.

Add code for the sliderMoved signal.

Menu Bar Item with Action Editor

Add a File menu with Open, Close and Quit menu items.

Qt Creator must be in Design mode. Make sure that the Action Editor and Signal and Slots Editor are visible. Do this from the top menu as follows. Select Window → Views and then click the check box next to each of the desired editors.

Add slots for the triggered() signal for the Open and Close menu items. Do this in the Action Editor as follows. Right click a menu item. Click Go to slot… on the menu that pops up. Click triggered() in the dialog box that pops up and then click the OK button.

Add code in the slot function.

Menu Bar Item with Signals and Slots Editor

In Design mode, select the Signals and Slots tab. Click the big green + sign to add an item. Change the following for the new item.

  • Sender : actionQuit
  • Signal : triggered()
  • Receiver : MainWindow
  • Slot : close()

Code Listing

Below is the code listing for mainwindow.cpp for the example project. Follow the video embedded near the top of this page to add the code.

mainwindow.cpp

This article is the most comprehensive description of signals and slots in QML compared to all previous articles on this site.

In this article, I will try to explain the following when working with Qt/QML + Qt/C++:

Signal Slots Qt Example
  • ways to declare signals and slots, also called methods in the C ++ class, which will be registered in the QML layer
  • ways to connect to signals of classes declared in C ++ as context
  • work with Q_PROPERTY, which also requires signals and slots
  • ways to connect signals and slots in QML
  • etc.

Signals and slots from the C++ class

Let's create our first class that will work with signals and slots in QML. This is one of the very first examples that I have already shown, but I will repeat this example so that the article is as complete as possible.

In this example, I want to create an application that has one button and by pressing this button increases the counter that is inside the C++ class. This C++ class will be registered as a context property in the QML engine of our application.

Signal Slots Qt Examples

App appearance will be next

AppCore.h

Declaring signals and slots in C ++ code will not differ much from the classical Qt/C++.

AppCore.cpp

As well as the implementation of the methods themselves.

main.cpp

main.qml

And now the most interesting. How to use an object loaded in a QML context and how to connect to its signals.

As you remember, we loaded the object into the context QML under the name appCore , we will use this object to access it. But to connect to the signal, we will need to use the QML type Connections .

Thus, you can access the object that was loaded into the context of the QML engine, call its slot, and process the signal from this object.

It is also not necessary to declare receiveFromQml() as a slot in this case. This method can also be declared as Q_INVOKABLE method.

Using Q_PROPERTY

The next option is to use the Q_PROPERTY macro. A classic property in Qt might look like this for our task

This property has the following components:

  • type of property, as well as its name: int counter , which are bound to the variable int m_counter inside the class, this is the logic of code generation in Qt
  • name of the method to read, matches the name of the property: counter
  • method name for setting the value: setCounter
  • signal that reports property changes: counterChanged

You can also pass additional parameters to this macro, but this is beyond the scope of this article. And also the property can be read only, that is, without a setter.

Now look at the full code using Q_PROPERTY

AppCore.h

AppCore.cpp

main.qml

Here you will see that connecting the property and accessing it has become easier thanks to the declarative style of QML code. Of course, you cannot always use properties, sometimes you just need to use signals, slots, and Q_INVOKABLE methods. But for variables like counter, properties are likely to be much more convenient.

Connecting signals inside QML files

Now consider the option of connecting signals and slots (functions) inside QML files. There will no longer be any C ++ code.

Among other things, you can use and disable signals from slots

Connect a signal to a signal

Also in QML there is still the ability to connect a signal to a signal, as in Qt/C++. Look at the following artificial example.

In this case, the counter will continue to increase when the button is pressed. But the button press signal is not connected directly to the counter increase function, but is forwarded through the signal.

Using Variables in Signals

QML also has the ability to use variables in signals.

Conclusion

For the most part, this entire article fits into several points:

  • In C ++, to interact with the QML layer, you can use signals, slots, Q_INVOKABLE methods, as well as create properties using the Q_PROPERTY macro
  • In order to respond to signals from objects, you can use the QML type Connections
  • Q_PROPERTY obeys the declarative style of QML and, when a property is changed, it can automatically set new values, if the property has been added to any object in QML. In this case, the signal slot connections are set automatically.
  • In QML, you can connect and disconnect signal / slot connections using the following syntax:
    • object1.signal.connect (object2.slot)
    • object1.signal.disconnect (object2.slot)
  • Signals in QML can also be connected to other signals, as is done in Qt / C ++
  • Signals in QML may also have arguments