Slot Example In Qt

Traditional syntax: SIGNAL and SLOT QtCore.SIGNAL and QtCore.SLOT macros allow Python to interface with Qt signal and slot delivery mechanisms. This is the old way of using signals and slots. The example below uses the well known clicked signal from a QPushButton.The connect method has a non python-friendly syntax. I modified the example 'Configuration Dialog' (from Qt 4.8/ creator2.8) for learning by doing. On a QPushbutton clicked event on one of the pages of a QStackedWidget a short message should be printed out. While being better in many regards, the new connection syntax in Qt5 has one big weakness: Connecting overloaded signals and slots. In order to let the compiler resolve the overloads we need to use staticcasts to member function pointers, or (starting in Qt 5.7) qOverload and friends. 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.

The Camera Example demonstrates how you can use Qt Multimedia to implement some basic Camera functionality to take still images and record video clips with audio.

Slot Example In Qt 8

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

A Camera class is created that will act as our Camera. It has a user interface, control functions, setting values and a means of defining the location where the image or video clip is to be saved. It will also store the image and video settings.

The Camera class contains an instance of QCamera, the API class interface to the hardware. It also has an instance of QCameraImageCapture to take still images and an instance of QMediaRecorder to record video. It also contains the user interface object.

The Camera constructor does some basic initialization. The camera object is set to '0', the user interface is initialized and UI signal are connected to slots that react to the triggering event. However, most of the work is done when the setCamera() function is called, passing in a QByteArray.

setCamera() sets up various connections between the user interface and the functionality of the Camera class using signals and slots. It also instantiates and initializes the QCamera, QCameraImageCapture and QMediaRecorder objects mentioned above. The still and video recording visual tabs are enabled and finally the start() function of the QCamera object is called.

Now that the camera is ready for user commands it waits for a suitable event. Such an event will be the key press of either the Qt::Key_CameraFocus or Qt::Key_Camera buttons on the application window. Camera focus will simply display the viewfinder and lock the camera settings. Key_Camera will either call takeImage() if the captureMode() is QCamera::CaptureStillImage, or if the capture mode is for video then one of two actions will occur. If the recording state shows that we are currently recording then the stop() function is called resulting in a call to QCamera::stop(), whereas if we are not recording then a video recording is started with a call to QMediaRecorder::record().

Files:

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.

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.

Slot

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

Slot Example In Qt Interval

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.

Slot Example In Qt Ice Chest

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.

Slot Example In Qtc

Menu Bar Item with Action Editor

Slot Example In Qts

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