Creating thread for each function of a class
Hi there. I'm just a beginner in Qt and I have been wrapping my head about this question for quite a while. Could you please help me out?
I have got a class, named, let's say,
Code:
class BaseClass: public Thread
I have a bunch of functions and I want each function to run in a separate thread. All information I have stumbled across so far claims that if I want to run a function in a separate thread I have to create a class
that inherits QThread and override QThread::run() function. But I hope there is another solution apart from creating a class which inherits QThread for each function in BaseClass. I would appreciate any ideas on this. Thank you so much in advance.
Re: Creating thread for each function of a class
Re: Creating thread for each function of a class
Thank you, it is exactly what I was craving for:)
Re: Creating thread for each function of a class
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this->ui->button1, SIGNAL(clicked()), this, SLOT(startThread()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::startThread()
{
QFuture<void> f1 = QtConcurrent::run(mbox);
f1.waitForFinished();
}
void mbox()
{
for (int i=0; i < 100000; i++)
{
qDebug() << i;
}
}
After this, GUI dies. I don't know what to do with it. Furthermore, there is a limit of input arguments, which is 5. What am I to do if there has to be more than that? Might the problem of hanging be in using f1.waitForFinished()?
Re: Creating thread for each function of a class
What do you mean by dies? Does your application crash or is it just not respondable? The later would be because of waitForFinished() since your code does nothing else than normally call the mbox function with blocking the gui thread. If you need more arguments use a QVariantList.
Re: Creating thread for each function of a class
Using QFutureWatcher is asynchronous. So connect the finished signal of f1 to a slot where you can go on, after the work in the function is done.
Re: Creating thread for each function of a class
Quote:
Originally Posted by
Lykurg
What do you mean by dies? Does your application crash or is it just not respondable? The later would be because of waitForFinished() since your code does nothing else than normally call the mbox function with blocking the gui thread. If you need more arguments use a QVariantList.
No, it doesn't respond. So, I excluded "waitFor" and GUI does respond. Without "wait" I would use final signal and some slot as you suggested and go on with whatever is needed and it will work, won't it? :) You meant QList<QVariant>, didn't you? Thank you.
Re: Creating thread for each function of a class
Quote:
Originally Posted by
Astrologer
You meant QList<QVariant>, didn't you? Thank you.
Yes, since Qt does a typedef, QVariantList == QList<QVariant>. (QVariant::QVariantList)
Re: Creating thread for each function of a class
Quote:
Originally Posted by
Lykurg
Using [QTCLASS]So connect the finished signal of f1 to a slot where you can go on, after the work in the function is done.
If I do like this:
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this->ui->button1, SIGNAL(clicked()), this, SLOT(startThread()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::startThread()
{
QFuture<void> f1 = QtConcurrent::run(this, &MainWindow::mbox);
//f1.waitForFinished();
}
void MainWindow::mbox()
{
for (int i=0; i < 20000; i++)
{
qDebug() << i;
}
emit done();
}
void MainWindow::done()
{
}
It apparently tries and create widget from within the thread and fails. Could you please hint me?
Re: Creating thread for each function of a class
First you have to declare done as a slot then I meant to use the QFutureWatcher signal finished! E.g.:
Code:
QFutureWatcher<void> watcher;
connect(&watcher, SIGNAL(finished()), this, SLOT(done()));
//...
QFuture<void> f1 = QtConcurrent::run(this, &MainWindow::mbox);
watcher.setFuture(f1);
(not tested, but you see the idea!)
Re: Creating thread for each function of a class
Yes, I do. I think I will peep in again just in case. Thank you for your patience and great help ;)