Return to site

Qt Signals And Slots Tutorial

broken image


Use QThread, reimplement run and emit signals as needed. Connect the signals to the GUI thread's slots using queued signal/slot connections. One call: Operations are to be performed on all items of a container. Processing should be performed using all available cores. A common example is to produce thumbnails from a list of images.

Build complex application behaviours using signals and slots, and override widget event handling with custom events.

As already described, every interaction the user has with a Qt application causes an Event. There are multiple types of event, each representing a difference type of interaction — e.g. mouse or keyboard events.

The Qt Object Model a very powerful mechanism for seamless object communication called signals and slots queryable and designable object properties powerful events. Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. In GUI programming, when we change one widget, we often want another widget to be notified. This signal does nothing, by itself; it must be connected to a slot, which is an object that acts as a recipient for a signal and, given one, acts on it. Connecting Built-In PySide/PyQt Signals. Qt widgets have a number of signals built in. For example, when a QPushButton is clicked, it emits its clicked signal.

Events that occur are passed to the event-specific handler on the widget where the interaction occurred. For example, clicking on a widget will cause a QMouseEvent to be sent to the .mousePressEvent event handler on the widget. This handler can interrogate the event to find out information, such as what triggered the event and where specifically it occurred.

You can intercept events by subclassing and overriding the handler function on the class, as you would for any other function. You can choose to filter, modify, or ignore events, passing them through to the normal handler for the event by calling the parent class function with super().

However, imagine you want to catch an event on 20 different buttons. Subclassing like this now becomes an incredibly tedious way of catching, interpreting and handling these events.

python

Thankfully Qt offers a neater approach to receiving notification of things happening in your application: Signals.

Signals

Instead of intercepting raw events, signals allow you to 'listen' for notifications of specific occurrences within your application. While these can be similar to events — a click on a button — they can also be more nuanced — updated text in a box. Data can also be sent alongside a signal - so as well as being notified of the updated text you can also receive it.

The receivers of signals are called Slots in Qt terminology. A number of standard slots are provided on Qt classes to allow you to wire together different parts of your application. However, you can also use any Python function as a slot, and therefore receive the message yourself.

Load up a fresh copy of `MyApp_window.py` and save it under a new name for this section. The code is copied below if you don't have it yet.

Basic signals

First, let's look at the signals available for our QMainWindow. You can find this information in the Qt documentation. Scroll down to the Signals section to see the signals implemented for this class.

Qt 5 Documentation — QMainWindow Signals

As you can see, alongside the two QMainWindow signals, there are 4 signals inherited from QWidget and 2 signals inherited from Object. If you click through to the QWidget signal documentation you can see a .windowTitleChanged signal implemented here. Next we'll demonstrate that signal within our application.

Qt 5 Documentation — Widget Signals

The code below gives a few examples of using the windowTitleChanged signal.

Slots
python

Try commenting out the different signals and seeing the effect on what the slot prints.

We start by creating a function that will behave as a ‘slot' for our signals.

Then we use .connect on the .windowTitleChanged signal. We pass the function that we want to be called with the signal data. In this case the signal sends a string, containing the new window title.

If we run that, we see that we receive the notification that the window title has changed.

Events

Next, let's take a quick look at events. Thanks to signals, for most purposes you can happily avoid using events in Qt, but it's important to understand how they work for when they are necessary.

As an example, we're going to intercept the .contextMenuEvent on QMainWindow. This event is fired whenever a context menu is about to be shown, and is passed a single value event of type QContextMenuEvent.

To intercept the event, we simply override the object method with our new method of the same name. So in this case we can create a method on our MainWindow subclass with the name contextMenuEvent and it will receive all events of this type.

If you add the above method to your MainWindow class and run your program you will discover that right-clicking in your window now displays the message in the print statement.

Sometimes you may wish to intercept an event, yet still trigger the default (parent) event handler. You can do this by calling the event handler on the parent class using super as normal for Python class methods.

python

This allows you to propagate events up the object hierarchy, handling only those parts of an event handler that you wish.

However, in Qt there is another type of event hierarchy, constructed around the UI relationships. Widgets that are added to a layout, within another widget, may opt to pass their events to their UI parent. In complex widgets with multiple sub-elements this can allow for delegation of event handling to the containing widget for certain events.

However, if you have dealt with an event and do not want it to propagate in this way you can flag this by calling .accept() on the event.

Alternatively, if you do want it to propagate calling .ignore() will achieve this.

python

In this section we've covered signals, slots and events. We've demonstratedsome simple signals, including how to pass less and more data using lambdas.We've created custom signals, and shown how to intercept events, pass onevent handling and use .accept() and .ignore() to hide/show eventsto the UI-parent widget. In the next section we will go on to takea look at two common features of the GUI — toolbars and menus.

  • cross platform
  • open source
  • support multi language
    • PyQt
  • it's cute(?)
  • help you design GUI
  • elements drag and drop
  • instant preview
  • Qt 5.7 recommand (for Program Design II)
  • you'll need to specific the compiler you want yo use

Right click the pushbutton, and select 'Go to slot..'

and choose 'clicked()'

Write some code in the method(e.g. 'cout'), and run the program to see what happend !

Tutorial
  • Not standard C++ function
  • Use Macros
  • To use signals and slots, your object must be a derived class of 'QObject' and use the 'Q_OBJECT' macro.
  • connect to slots
  • when 'emit' a signal, all connected slots will be called.
  • is a function prototype
  • connect to signals
  • can be called by signals, or use like a normal function.
  • return value only available when use as a normal function.
  • 在public slots宣告method
  • 需要定義
  • 可當作一般method使用
  • 可以有return值,但只有當作一般method使用時才有意義
  • 在signals宣告
  • 是function prototype
  • 不需要定義

Qt Signals And Slots Tutorial

  • 使用方式:emit signal
  • Signals and slots can have parameters too.
Qt Signals And Slots Tutorial
  • Notice that the compiler will not check whether the signal/slot exist, if you use not exist ones, will cause runtime error.

'connect' support many different syntaxs, if you're interested, you can visit official Qt documentation.

what is the result if you use 'sleep()' in your program?

  • Use signals and slots
  • Will not block your program
  • See Qt doc
  • Add a QTimer to your program
  • Connect the signal 'timeout()' to your slot
  • Start the timer with interval(in ms)
Qt designer signals and slots tutorial

I use some Qt Objects

(e.g. QString), but 'cout'

Slots

Build complex application behaviours using signals and slots, and override widget event handling with custom events.

As already described, every interaction the user has with a Qt application causes an Event. There are multiple types of event, each representing a difference type of interaction — e.g. mouse or keyboard events.

The Qt Object Model a very powerful mechanism for seamless object communication called signals and slots queryable and designable object properties powerful events. Signals and slots are used for communication between objects. The signals and slots mechanism is a central feature of Qt and probably the part that differs most from the features provided by other frameworks. In GUI programming, when we change one widget, we often want another widget to be notified. This signal does nothing, by itself; it must be connected to a slot, which is an object that acts as a recipient for a signal and, given one, acts on it. Connecting Built-In PySide/PyQt Signals. Qt widgets have a number of signals built in. For example, when a QPushButton is clicked, it emits its clicked signal.

Events that occur are passed to the event-specific handler on the widget where the interaction occurred. For example, clicking on a widget will cause a QMouseEvent to be sent to the .mousePressEvent event handler on the widget. This handler can interrogate the event to find out information, such as what triggered the event and where specifically it occurred.

You can intercept events by subclassing and overriding the handler function on the class, as you would for any other function. You can choose to filter, modify, or ignore events, passing them through to the normal handler for the event by calling the parent class function with super().

However, imagine you want to catch an event on 20 different buttons. Subclassing like this now becomes an incredibly tedious way of catching, interpreting and handling these events.

python

Thankfully Qt offers a neater approach to receiving notification of things happening in your application: Signals.

Signals

Instead of intercepting raw events, signals allow you to 'listen' for notifications of specific occurrences within your application. While these can be similar to events — a click on a button — they can also be more nuanced — updated text in a box. Data can also be sent alongside a signal - so as well as being notified of the updated text you can also receive it.

The receivers of signals are called Slots in Qt terminology. A number of standard slots are provided on Qt classes to allow you to wire together different parts of your application. However, you can also use any Python function as a slot, and therefore receive the message yourself.

Load up a fresh copy of `MyApp_window.py` and save it under a new name for this section. The code is copied below if you don't have it yet.

Basic signals

First, let's look at the signals available for our QMainWindow. You can find this information in the Qt documentation. Scroll down to the Signals section to see the signals implemented for this class.

Qt 5 Documentation — QMainWindow Signals

As you can see, alongside the two QMainWindow signals, there are 4 signals inherited from QWidget and 2 signals inherited from Object. If you click through to the QWidget signal documentation you can see a .windowTitleChanged signal implemented here. Next we'll demonstrate that signal within our application.

Qt 5 Documentation — Widget Signals

The code below gives a few examples of using the windowTitleChanged signal.

python

Try commenting out the different signals and seeing the effect on what the slot prints.

We start by creating a function that will behave as a ‘slot' for our signals.

Then we use .connect on the .windowTitleChanged signal. We pass the function that we want to be called with the signal data. In this case the signal sends a string, containing the new window title.

If we run that, we see that we receive the notification that the window title has changed.

Events

Next, let's take a quick look at events. Thanks to signals, for most purposes you can happily avoid using events in Qt, but it's important to understand how they work for when they are necessary.

As an example, we're going to intercept the .contextMenuEvent on QMainWindow. This event is fired whenever a context menu is about to be shown, and is passed a single value event of type QContextMenuEvent.

To intercept the event, we simply override the object method with our new method of the same name. So in this case we can create a method on our MainWindow subclass with the name contextMenuEvent and it will receive all events of this type.

If you add the above method to your MainWindow class and run your program you will discover that right-clicking in your window now displays the message in the print statement.

Sometimes you may wish to intercept an event, yet still trigger the default (parent) event handler. You can do this by calling the event handler on the parent class using super as normal for Python class methods.

python

This allows you to propagate events up the object hierarchy, handling only those parts of an event handler that you wish.

However, in Qt there is another type of event hierarchy, constructed around the UI relationships. Widgets that are added to a layout, within another widget, may opt to pass their events to their UI parent. In complex widgets with multiple sub-elements this can allow for delegation of event handling to the containing widget for certain events.

However, if you have dealt with an event and do not want it to propagate in this way you can flag this by calling .accept() on the event.

Alternatively, if you do want it to propagate calling .ignore() will achieve this.

python

In this section we've covered signals, slots and events. We've demonstratedsome simple signals, including how to pass less and more data using lambdas.We've created custom signals, and shown how to intercept events, pass onevent handling and use .accept() and .ignore() to hide/show eventsto the UI-parent widget. In the next section we will go on to takea look at two common features of the GUI — toolbars and menus.

  • cross platform
  • open source
  • support multi language
    • PyQt
  • it's cute(?)
  • help you design GUI
  • elements drag and drop
  • instant preview
  • Qt 5.7 recommand (for Program Design II)
  • you'll need to specific the compiler you want yo use

Right click the pushbutton, and select 'Go to slot..'

and choose 'clicked()'

Write some code in the method(e.g. 'cout'), and run the program to see what happend !

  • Not standard C++ function
  • Use Macros
  • To use signals and slots, your object must be a derived class of 'QObject' and use the 'Q_OBJECT' macro.
  • connect to slots
  • when 'emit' a signal, all connected slots will be called.
  • is a function prototype
  • connect to signals
  • can be called by signals, or use like a normal function.
  • return value only available when use as a normal function.
  • 在public slots宣告method
  • 需要定義
  • 可當作一般method使用
  • 可以有return值,但只有當作一般method使用時才有意義
  • 在signals宣告
  • 是function prototype
  • 不需要定義

Qt Signals And Slots Tutorial

  • 使用方式:emit signal
  • Signals and slots can have parameters too.
  • Notice that the compiler will not check whether the signal/slot exist, if you use not exist ones, will cause runtime error.

'connect' support many different syntaxs, if you're interested, you can visit official Qt documentation.

what is the result if you use 'sleep()' in your program?

  • Use signals and slots
  • Will not block your program
  • See Qt doc
  • Add a QTimer to your program
  • Connect the signal 'timeout()' to your slot
  • Start the timer with interval(in ms)

I use some Qt Objects

(e.g. QString), but 'cout'

cannot print it out !

  • can print out Qt Classes use operator<<
  • #include
  • usage: qDebug() << something
    • will print newline automatically

Qt Tutorial

By Liang Yu-Cheng

Qt Signals And Slots Tutorial Free





broken image