Qt Connect Signal Slot Thread
Qt supports these signal-slot connection types: Auto Connection (default) If the signal is emitted in the thread which the receiving object has affinity then the behavior is the same as the Direct Connection. Otherwise, the behavior is the same as the Queued Connection.' Direct Connection The slot is invoked immediately, when the signal is emitted. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread. The slot get called in its living thread, which is the sub-thread. Thanks to a mechanism called queued connections, it is safe to connect signals and slots across different threads. If all the across threads communication are done though queued connections, the usual multithreading precautions such as QMutex will no longer need to be taken.
Qt does not provide support for using the Signals & Slots mechanism in combination with C++ templates. See: https://doc.qt.io/qt-5/why-moc.html
However: It is actually possible to do this, when one is willing to keep track of the things normally handledby the QObject::connect() function and 'call' slots with QMetaObject::invokeMethod().
Doing this has some nice side effects like not having to call qRegisterMetaType() or using the Q_DECLARE_METATYPE macro.
For a more detailed explanation take a look at the documentation of the invokeInContext() function in Magic.h
Furthermore does Qt provide mechanisms for threading, as being described here: https://doc.qt.io/qt-5/threads-technologies.html#choosing-an-appropriate-approach
The case 'Have an object living in another thread that can perform different tasks upon request and/or can receive new data to work with'
has the proposed solution 'Subclass a QObject to create a worker. Instantiate this worker object and a QThread. Move the worker to the new thread. Send commands or data to the worker object over queued signal-slot connections'
which implies not being able to use C++ templates, when relying solely on the native Signals & Slots mechanism.
But since I am lazy and don't like Copy & Paste, this framework provides a solution for handling that case in combination with C++ templates.
The main idea is to have 'Tasks' and 'Results', which are being described by template parameters 'T' and 'R' in this framework.
A Worker, which is running in his own thread, receives a task and responds with a result.
In general do you need to inherit from the Processor template class, the Worker template classand implement at least the pure virtual methods to create the functionality you want and give instances of thosenew classes to a Controller. That's it.
In order to be able to start working on new tasks, change the number of threads to use etc. you need a communication channelto the instance of the Processor.
You may use Qt Signals & Slots or use the invokeInContext() function Signals & Slots mechanism of this framework.The first example in src/examples/one/
covers all of that.
The second example in src/examples/two/
focuses only on the Signals & Slots system of this framework.
A third example in src/examples/three/
shows usage of the threading architecture + Signals and Slots of this frameworkcompletely without using Qt Signals and Slots.
Everything you need is documented in the CMakeLists.txt
.
If you don't know what to do with such a file, you should use a search engine to find out.
Use the Doxyfile
to generate the documentation via Doxygen
.
Qt Connect Signal Slot Thread Chart
Look into the src/examples/
folder.
Join GitHub today
Free zeus casino slot machine pitcher. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upQt Connect Signal Slot Thread Free
/* |
Copyright 2013 Fabien Pierre-Nicolas. |
- Primarily authored by Fabien Pierre-Nicolas |
This file is part of simple-qt-thread-example, a simple example to demonstrate how to use threads. |
This example is explained on http://fabienpn.wordpress.com/qt-thread-simple-and-stable-with-sources/ |
This program is free software: you can redistribute it and/or modify |
it under the terms of the GNU General Public License as published by |
the Free Software Foundation, either version 3 of the License, or |
(at your option) any later version. |
This progra is distributed in the hope that it will be useful, |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
GNU General Public License for more details. |
You should have received a copy of the GNU General Public License |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
*/ |
#include'mainwindow.h' |
#include'ui_mainwindow.h' |
#include<QDebug> |
MainWindow::MainWindow(QWidget *parent) : |
QMainWindow(parent), |
ui(new Ui::MainWindow) |
{ |
ui->setupUi(this); |
// The thread and the worker are created in the constructor so it is always safe to delete them. |
thread = newQThread(); |
worker = newWorker(); |
worker->moveToThread(thread); |
connect(worker, SIGNAL(valueChanged(QString)), ui->label, SLOT(setText(QString))); |
connect(worker, SIGNAL(workRequested()), thread, SLOT(start())); |
connect(thread, SIGNAL(started()), worker, SLOT(doWork())); |
connect(worker, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection); |
} |
MainWindow::~MainWindow() |
{ |
worker->abort(); |
thread->wait(); |
qDebug()<<'Deleting thread and worker in Thread '<<this->QObject::thread()->currentThreadId(); |
delete thread; |
delete worker; |
delete ui; |
} |
voidMainWindow::on_startButton_clicked() |
{ |
// To avoid having two threads running simultaneously, the previous thread is aborted. |
worker->abort(); |
thread->wait(); // If the thread is not running, this will immediately return. |
worker->requestWork(); |
} |
Copy lines Copy permalink