I wanted to run few functions using different threads or QProcess
or something which is more effective and also provide good performance. I am trying to build one gui for calibrator and as soon as I press calibrate button it needs to calibrate. For calibrating, I have some function to do that. However it takes some time to do this calibration process. Until my calibration is done my gui stays unresponsive. So what i am trying to do here is I would like to run that function in some parallel process or threads or some other things.
#include "widget.h"
#include "ui_widget.h"
#include <iostream>
#include <sstream>
#include <QtConcurrent/QtConcurrent>
#include <QFuture>
using namespace Eigen;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->lineEdit->setReadOnly(true);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_pressed()
{
ui->plainTextEdit->appendHtml("<div style='color: green;'> Calibrating ..... </div>");
QString body = ui->comboBox->currentText();
int body = body.toInt();
/*
I want to run calib function here in separate thread or QtConcurrent so my gui will stay resposive even my function takes some time close to 1 min to do calibration
*/
}
void Widget::printReceivedBody(int x)
{
}
void Widget::Calib(int x)
{
printReceivedBody(int x)
}
I checked some questions in stack and also in qt but unable to fix my problem.I found few questions but are trying to run executable. I can do via executable also but i would like to try above approach instead of going with executable.
I went to qt Documentaion but that documentation gives me more confusion :-(
Can someone suggest me how to do this?
A QProcess
is meant to run an external process (an executable if it may be clearer). This is not what you need in my humble opinion.
What you may need is to use QThread
to run your function of Calibration.
As you have already looked at the documentation, let us provide you some examples of how to execute functions using QThread
in QT.
// execute a lambda
void MainWindow::onButton1Click()
{
qDebug()<< "clicked";
qDebug() << " the main thread id = " << QThread::currentThread();
QThread* l_thread = QThread::create([&]()
{
qDebug() << "Running Thread " << QThread::currentThreadId() << " to emit signal only ";
//emit dummy signal (for instance to refresh GUI
//emit sigShowHide( !this->ui->pushButton_2->isVisible());
});
l_thread->start();
}
// example of an external function to execute
void test(int value1, int &value2)
{
value2 = value1 + 1;
return;
}
void MainWindow::onButton2Click()
{
auto func = std::bind(test,value1, std::ref(value2));
QThread* qthread = QThread::create(std::bind(test,value1, std::ref(value2)));
qthread ->start();
}
// execute a method defined in MainWindow
void MainWindow::count()
{
qDebug()<< "Counting";
}
void MainWindow::onButton3Click()
{
ui->label->setText("Starting to count");
auto function = std::bind(&MainWindow::count, this);
QThread* l_thread = QThread::create(function);
l_thread->start();
}
Update: As for you (in the updated question), you may run you calibration like the following
void Widget::on_pushButton_pressed()
{
ui->plainTextEdit->appendHtml("<div style='color: green;'> Calibrating ..... </div>");
QString body = ui->comboBox->currentText();
int body = body.toInt();
/*
I want to run calib function here in separate thread or QtConcurrent so my gui will stay resposive even my function takes some time close to 1 min to do calibration
*/
// LIKE THIS
// for instance you can to call Widget::Calib with the arugment int x=15
int x =15;
auto func = std::bind(&Widget::Calib, this, x));
QThread* qthread = QThread::create(func);
qthread ->start();
// if you want to wait for the QThread to finish before continuing, you can add
// qthread.wait()
}
Only for information, here is an example of the usage of a QProcess
to launch a windows command line cmd.exe
and execute somme copy
commands with arguments
#include <QCoreApplication>
#include <Qdebug>
#include <QObject>
#include <QProcess>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QProcess* processus = new QProcess();
QStringList args;
QString path("/path_accentué");
args << "/C" << "copy" << "/toto/sfx.exe" << path;// "F:\\path_accentué";
processus->start("cmd", args);
if (!processus->waitForStarted())
{
qDebug() << "Could not launch the process";
}
processus->write(s.c_str());
if (!processus->waitForFinished(-1))
{
qDebug() << "Finished";
}
delete processus;
return app.exec();
}