Qthread Slot Connection

Posted onby admin
  1. Qthread Lock
  2. Qthread Slots

EnArBgDeElEsFaFiFrHiHuItJaKnKoMsNlPlPtRuSqThTrUkZh

This page was used to describe the new signal and slot syntax during its development. The feature is now released with Qt 5.

From main thread: QThread(0x9e8100) Event received in thread QThread(0x9e8100) Event received in thread QThread(0x9e8100) Queued Connection For queued connection, when the signal is emitted, a event will be post to the event queue. Qt automatically breaks a signal/slot connection if either the sender or the receiver are destroyed (or if context object is destroyed, when using the new connection syntax and connecting to free functions). This is a major feature of the signals and slots mechanism. The Basics of QThread QThread manages one thread of execution The thread itself is defined by run Note that run is a protected method Calling start will create and start the thread QThread inherits from QObject Support properties, events, signals and slots Several threading primitive classes.

  • Differences between String-Based and Functor-Based Connections (Official documentation)
  • Introduction (Woboq blog)
  • Implementation Details (Woboq blog)

PyQt5 - Tutorial 009. Using QThread with MoveToThread. Based on one of the questions on the forum, I wrote an example on using QThread in PyQt5, as well as using the moveToThread method to move the class object of the inherited QObject to another thr. The second thing you need to know about using QThread is, as with all QObject subclasses, a QThread object has affinity to the thread in which it is instantiated. If you subclass QThread and add your own slots, you may be surprised to learn that these slots will actually run on the main thread.

Note: This is in addition to the old string-based syntax which remains valid.

  • 1Connecting in Qt 5
  • 2Disconnecting in Qt 5
  • 4Error reporting
  • 5Open questions

Connecting in Qt 5

There are several ways to connect a signal in Qt 5.

Old syntax

Qt 5 continues to support the old string-based syntax for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget)

New: connecting to QObject member

Here's Qt 5's new way to connect two QObjects and pass non-string objects:

Qthread Lock

Pros

  • Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
  • Argument can be by typedefs or with different namespace specifier, and it works.
  • Possibility to automatically cast the types if there is implicit conversion (e.g. from QString to QVariant)
  • It is possible to connect to any member function of QObject, not only slots.

Cons

  • More complicated syntax? (you need to specify the type of your object)
  • Very complicated syntax in cases of overloads? (see below)
  • Default arguments in slot is not supported anymore.

New: connecting to simple function

The new syntax can even connect to functions, not just QObjects:

Pros

  • Can be used with std::bind:
  • Can be used with C++11 lambda expressions:

Cons

  • There is no automatic disconnection when the 'receiver' is destroyed because it's a functor with no QObject. However, since 5.2 there is an overload which adds a 'context object'. When that object is destroyed, the connection is broken (the context is also used for the thread affinity: the lambda will be called in the thread of the event loop of the object used as context).

Disconnecting in Qt 5

As you might expect, there are some changes in how connections can be terminated in Qt 5, too.

Old way

You can disconnect in the old way (using SIGNAL, SLOT) but only if

  • You connected using the old way, or
  • If you want to disconnect all the slots from a given signal using wild card character

Symetric to the function pointer one

Only works if you connected with the symmetric call, with function pointers (Or you can also use 0 for wild card)In particular, does not work with static function, functors or lambda functions.

New way using QMetaObject::Connection

Works in all cases, including lambda functions or functors.

Asynchronous made easier

With C++11 it is possible to keep the code inline

Here's a QDialog without re-entering the eventloop, and keeping the code where it belongs:

Another example using QHttpServer : http://pastebin.com/pfbTMqUm

Error reporting

Tested with GCC.

Fortunately, IDEs like Qt Creator simplifies the function naming

Missing Q_OBJECT in class definition

Type mismatch

Open questions

Default arguments in slot

If you have code like this:

The old method allows you to connect that slot to a signal that does not have arguments.But I cannot know with template code if a function has default arguments or not.So this feature is disabled.

There was an implementation that falls back to the old method if there are more arguments in the slot than in the signal.This however is quite inconsistent, since the old method does not perform type-checking or type conversion. It was removed from the patch that has been merged.

Overload

As you might see in the example above, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting, e.g. a connection that previously was made as follows:

connect(mySpinBox, SIGNAL(valueChanged(int)), mySlider, SLOT(setValue(int));

cannot be simply converted to:

...because QSpinBox has two signals named valueChanged() with different arguments. Instead, the new code needs to be:

Unfortunately, using an explicit cast here allows several types of errors to slip past the compiler. Adding a temporary variable assignment preserves these compile-time checks:

Some macro could help (with C++11 or typeof extensions). A template based solution was introduced in Qt 5.7: qOverload

Qthread Slots

The best thing is probably to recommend not to overload signals or slots …

… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.

Disconnect

Should QMetaObject::Connection have a disconnect() function?

The other problem is that there is no automatic disconnection for some object in the closure if we use the syntax that takes a closure.One could add a list of objects in the disconnection, or a new function like QMetaObject::Connection::require


Callbacks

Function such as QHostInfo::lookupHost or QTimer::singleShot or QFileDialog::open take a QObject receiver and char* slot.This does not work for the new method.If one wants to do callback C++ way, one should use std::functionBut we cannot use STL types in our ABI, so a QFunction should be done to copy std::function.In any case, this is irrelevant for QObject connections.

Retrieved from 'https://wiki.qt.io/index.php?title=New_Signal_Slot_Syntax&oldid=34943'

Effective Threading Using Qt

Over the years using Qt I’ve seen a lot of difficulty using threads with Qt.Threads can be difficult and Qt provides a lot of ways to make threads easy towork with. Still basic / direct / low level threading (I’ll just call thisbasic) is often seen as difficult with Qt. It really isn’t though.

There are three main ways I’ve seen people handle basic threading in their Qtapplications. The first is using system threads, either pthread or Windowsthreads. I don’t like this approach because you’re basically writing a portablethread library. You can use an already built portable thread library but whyuse a non-Qt solution when Qt already provides portable threading.

The two other approaches are defined by Qt’s QThreaddocumentation. 1) use a QObject worker. 2)subclass QThread and reimplement the run function.

I like using a QObject worker and I think this is the best (and easiest)approach. That said, I don’t like how Qt’s documentation explains this. It usestwo QObject classes to handle this. A variation of this approach is what I’mgoing to demonstrate.

The other documented approach is subclassing QThread. This isn’t a horribleidea but I don’t think it’s the cleanest approach. It also ties thefunctionality to a thread making code reuse low in this case. You could getaround this limitation by putting the functionality in a separate class but nowyou have two classes and the thread subclass really isn’t necessary when usingthe QObject worker approach.

This is a very simple example that demonstrates two types of workers. One takesarguments and runs until it’s task is finished. The second on runs until it’stold to stop. The second worker could take arguments if you need it to. Forthis example the workers both just increment a count every second and send theresult to a QMainWindow for it to be displayed.

Here is all the code that goes into the example which will have the main partsexplained after:

main.cpp

mainwindow.h

mainwindow.cpp

mainwindow.ui

countworker.h

countworker.cpp

infinitecountworker.h

infinitecountworker.cpp

portablesleep.h

threader.pro

CountWorker

This is a very simple object that increases a count every second. The start andend are set as part of the constructor. Setting up the object using theconstructor makes sense but it’s also because to start the worker we can’t passany arguments. This is a limitation of this method that the worker in Qt’s docsdoes not have. That said, I think it’s fine to use the constructor for argumentpassing. This method doesn’t reuse workers (not a limitation) so it’s fine.Once the worker finishes it’s work is all done and it stops.

InfiniteCountWorker

This worker is very similar to the count worker. It’s to demonstrate infinitetasks unlike CountWorker which demonstrates finite tasks.

Connection

The key to his worker is the stopWork slot. Calling this sets sets a flag tolet the worker’s doWork function to exit.

*Workers

Both workers have two signals in common, updateCount and finished.UpdateCount simply sends the current count off. The MainWindow connects anduses this to, well, update the count. finished signals that the worker is done.This is used to stop the thread and takes care of cleanup. The finished signalcan have multiple overloads. For example to send off a result in addition tosignaling that the worker is finished. You must have a no argument signal forcompletion with this method. You could use a different signal name instead ofoverloading “finished” if you want.

One thing to think about is thread synchronization. Well, with this workermethod you don’t need to worry about it. Initialization of parameters happensbefore the worker is moved to the thread and before the thread is even started.All passing (such as updateCount) happens using signals and slots. When passingdata between threads using signals and slots Qt handles thread synchronizationfor you. The stopWork function is called via a signal so the function runs onthe thread the work is running on between iterations of the while loop. Sothere is no need to wrap m_running in a mutex or other synchronizationtechniques.

It might be confusing that I said the stopWork function happens betweeniterations of the while loop in InfiniteCountWorker. This is because of theqApp->processEvents call. Without this, the signal to initiate the stopWorkslot won’t be delivered until after the while loop finishes. Which isimpossible in infiniteCountWorker. Remember the thread is a single thread andeverything running on it is single threaded. The while loop will block anythingelse in the object from running unless you use qApp->processEvents to allowsignals and slots to process. In the case of the CountWorkerqApp->processEvents isn’t really necessary since there aren’t any signalsthat are delivered to it. processEvents is only necessary for signals (fromoutside of or within the thread the worker is running) to initiate slots in theworker. It is not necessary for the worker to send off a signal (likeupdateCount) to another thread that it’s connected to.

If you haven’t realized by now that with the worker method (my variation orQt’s) the worker has an event loop. Subclassing QThread only does in somecases (see the documentation for when). You get signals into and out of workerwithout any additional code (except for a single processEvents call). You getthis essentially for free by using a worker!

MainWindow

The workers aren’t really useful unless they can be started on a thread. That’swhat the MainWindow in this example is for. The startCount andstartInfiniteCount functions are the main thing to look at.

startCount Breakdown

The thread and worker are created on the heap. The worker is initialized heretoo.

Put the worker on the thread so the worker will be run on the thread we createdinstead of the UI thread.

This is how the worker is started when the thread is started.

Connect all the finished signals. When the worker finishes the thread will bestopped with quit(), the worker will be deleted via deleteLater. Qt handlesdeletion and takes care of it when safe. This is another advantage of usingQObject workers. Also, when the worker finishes the countFinished functionis called so anything in the MainWindow that needs to happen when the workeris finished is run. Again, you could have a second finished signal (overload)that passes result data back to the MainWindow. Finally, when the threadfinishes (because of the worker’s finished signal calling the threads quitslot) it will be deleted by deleteLater. The deleteLater slots mean wedon’t need to track the the worker or thread pointers and worry about manuallydeleting them.

deleteLater is very useful. We have finished connected to multiple slots.Deleting a QObject when there are events pending or within a signal handler(slot) can lead to a crash. deleteLater ensures that this won’t happen bywaiting until all events are delivered. Also, you can’t delete a QObject froma thread so this also ensures the UI thread that created the worker and threadis where they are deleted. In this example this isn’t a concern but it’s niceto know that it won’t become one.

We don’t need to worry about something like dangling connections because when aQObject is deleted it automatically disconnects all signals and slots (not inall cases but that will be covered later).

Here the count will be updated on the MainWindow as it’s incremented in theworker. Again, there is no manual thread synchronization necessary because Qthandles this as part of it’s signal and slot system.

The last line to worry about actually starts the thread which in turns startsthe worker.

startInfiniteCount Breakdown

This function is very similar to startCount. The only difference is one line.

The MainWindows button that will stop the InfiniteCountWorker is connectedto the InfiniteCountWorker’s stop function.

PortableSleep

portablesleep.h is a header only cross platform sleep class. This is how wecreate the one second delay when counting. With Qt5 you can (should) useQThreadsmsleep or sleep public static functions instead. Qt4 on theother hand defines these functions as protected static functions. So there isno clean way to use them aside from creating a QThread subclass and exposingthem. Note that this example is not limited to Qt4. This example will runand work with both Qt4 and Qt5. A little bit of thought is all that’s needed toachieve this.

This information isn’t specific to threading but could be very useful to reallyget the most of the worker concept.

This example only uses int. This is a type that can be used with Qt’s signalsand slots as well as many other types such as QString. There are many complextypes like your own classes or even QMap<QString, QString>> cannot be usedimmediately with signals and slots.

I say immediately because any class with a public constructor, copy constructorand destructor can be registered with Qt using qRegisterMetaType. This allowsthe object type to be used with signals and slots. For example you can use thefollowing in the MainWindow’s constructor to allow a complex type to be used.

The reason we need constructor, copy constructor and destructor is because whennecessary Qt will create a copy of an object and pass the copy to a slot. Inthe case of passing objects between threads using signals and slots a copy willbe passed to the slot. Remember primitive types like int and pointers arealways copies. Complex types may or may not be copies depending on thesituation.Realize that if you are passing a pointer between threads you will need manualsynchronization. If you’re allowing copies to be passed then you don’t need toworry about synchronization.

You’ve probably noticed that in this example the old style SIGNAL and SLOTmacros were used in the connect functions. This is on purpose because itsupports more compilers. Also, as mentioned, the example code works with bothQt4 (it is still used) and Qt5. I don’t really like new syntax provided byC++11 either. There are too manynegatives for my liking. In thiscase the fact that automatic connection disconnection isn’t support is prettybig. Nor is overloading signals very clean which makes using multiple versionof finish not a friendly. But this is my personal preference and not requiredfor this threading method.

Contrary to popular belief Qt provides very powerful basic threadingcapabilities. I say basic because I didn’t even mention Qt Concurrent whichprovides a lot of high level threading support. Oh and QThread pool wasn’tmentioned. None of the thread synchronization objects like QMutex weredescribed either.

Even with all of the threading objects that Qt provides it’s still really easyto use threading with QObject workers. For most people this is going to beenough for offload some functionality and keep the UI from blocking. While thisis the method I use quite often there are plenty of other ways. I just happento find this to be the cleanest and easiest.