Qt Connect Signal Slot Thread

  

Permalink

Function to relate the signal to the slot. Qt's signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. Since signals and slots are type-safe, type errors are reported as warnings and do not cause crashes to occur. For example, if a Quit button's. In this case could be a little annoying and useless to 'break' the flow of the code for manage the asynchronous signal to waiting for. In this case could be better to wait synchronously the signal and using the fantastic Qt is a very easy task as usual.

Join GitHub today

GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Sign up

Qthread Signal Slot

Find file Copy path
Qt Connect Signal Slot Thread
Cannot retrieve contributors at this time
/*
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

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

Qt Connect Class Slots To Designer Signals

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.

Look into the src/examples/ folder.