How? I show a message, while the task is running
-
Hi guys, I have the following code.
A method compress (), where I do the task of compressing an entire directory, I call this method from my button, with a QtConcurrent, as you can see. so far so good.
My question is, how can I display a message while compressing the folder is running.
I am using QuaZIP, for the compression process.
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_pushButton_5_clicked(); private: Ui::Widget *ui; void compress(); public: }; #endif // WIDGET_H#include "widget.h" #include "ui_widget.h" #include <QMessageBox> #include <QDebug> #include <JlCompress.h> #include <QtConcurrent/QtConcurrent> #include <QFuture> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_5_clicked() { QGuiApplication::setOverrideCursor(Qt::WaitCursor); QFuture<void> future=QtConcurrent::run(this,&Widget::compress); future.waitForFinished(); QGuiApplication::restoreOverrideCursor(); QMessageBox::information(this,qApp->applicationName(),"The folder was compressed successfully."); } void Widget::compress() { if(!JlCompress::compressDir("C:/Users/Mypc/Desktop/iconos.zip", "D:/images/iconos")){ QMessageBox::warning(this,qApp->applicationName(),"Error compressing folder."); return; } } -
Hi guys, I have the following code.
A method compress (), where I do the task of compressing an entire directory, I call this method from my button, with a QtConcurrent, as you can see. so far so good.
My question is, how can I display a message while compressing the folder is running.
I am using QuaZIP, for the compression process.
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_pushButton_5_clicked(); private: Ui::Widget *ui; void compress(); public: }; #endif // WIDGET_H#include "widget.h" #include "ui_widget.h" #include <QMessageBox> #include <QDebug> #include <JlCompress.h> #include <QtConcurrent/QtConcurrent> #include <QFuture> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); } Widget::~Widget() { delete ui; } void Widget::on_pushButton_5_clicked() { QGuiApplication::setOverrideCursor(Qt::WaitCursor); QFuture<void> future=QtConcurrent::run(this,&Widget::compress); future.waitForFinished(); QGuiApplication::restoreOverrideCursor(); QMessageBox::information(this,qApp->applicationName(),"The folder was compressed successfully."); } void Widget::compress() { if(!JlCompress::compressDir("C:/Users/Mypc/Desktop/iconos.zip", "D:/images/iconos")){ QMessageBox::warning(this,qApp->applicationName(),"Error compressing folder."); return; } }@lincoln said in How? I show a message, while the task is running:
QFuture<void> future=QtConcurrent::run(this,&Widget::compress); future.waitForFinished();Display it after the
run(), and don't do thewaitForFinished()in the slot if you want the user to see it. -
@lincoln said in How? I show a message, while the task is running:
QFuture<void> future=QtConcurrent::run(this,&Widget::compress); future.waitForFinished();Display it after the
run(), and don't do thewaitForFinished()in the slot if you want the user to see it.@JonB
I did this, but now when the backup finishes, the entire application closes.void Widget::on_pushButton_5_clicked() { QMessageBox box; // box->setWindowFlags(Qt::Dialog | Qt::SplashScreen); box.setStandardButtons(QMessageBox::NoButton); box.setText("Realizando una tarea!"); box.setWindowTitle(qApp->applicationName()); box.setIcon(QMessageBox::Information); box.setAttribute(Qt::WA_DeleteOnClose); QFutureWatcher<void> watcher; QFuture<void> future=QtConcurrent::run(this,&Widget::compress); watcher.setFuture(future); QGuiApplication::setOverrideCursor(Qt::WaitCursor); QObject::connect(&watcher,&QFutureWatcher<void>::finished,[&](){ box.close(); box.deleteLater(); QGuiApplication::restoreOverrideCursor(); }); box.exec(); QMessageBox::information(this,qApp->applicationName(),"Carpeta comprimirda."); } -
Also try a ProgressDialog and QFutureWatcher as follows, but it doesn't compile, I get this error.

void Widget::on_pushButton_5_clicked() { QStringList files; QDir dir("D:/images/iconos/uigenericicon"); files=dir.entryList(QStringList() << "*.jpg" << "*.JPG"<<"*.PNG",QDir::Files); qInfo()<<files; QProgressDialog pDialog; pDialog.setLabelText("Porecessing!!"); QFutureWatcher<void> watcher; QObject::connect(&pDialog,SIGNAL(canceled()),&watcher,SLOT(cancel())); QObject::connect(&watcher,SIGNAL(finished()),&pDialog,SLOT(reset())); QObject::connect(&watcher,SIGNAL(progressRangeChanged(int,int)),&pDialog,SLOT(setRange(int,int))); QObject::connect(&watcher,SIGNAL(progressValueChanged(int)),&pDialog,SLOT(setValue(int))); watcher.setFuture(QtConcurrent::map(files,&Widget::compress)); pDialog.exec(); if(watcher.isCanceled()){ QMessageBox::information(this,qApp->applicationName(),"You canceled."); }else{ QMessageBox::information(this,qApp->applicationName(),"All done."); } } void Widget::compress(QStringList &files) { if(!JlCompress::compressFiles("C:/Users/Lincoln/Desktop/iconos.zip",files)){ QMessageBox::warning(this,qApp->applicationName(),"Error al comprimir la carpeta."); return; } } -
@JonB
I did this, but now when the backup finishes, the entire application closes.void Widget::on_pushButton_5_clicked() { QMessageBox box; // box->setWindowFlags(Qt::Dialog | Qt::SplashScreen); box.setStandardButtons(QMessageBox::NoButton); box.setText("Realizando una tarea!"); box.setWindowTitle(qApp->applicationName()); box.setIcon(QMessageBox::Information); box.setAttribute(Qt::WA_DeleteOnClose); QFutureWatcher<void> watcher; QFuture<void> future=QtConcurrent::run(this,&Widget::compress); watcher.setFuture(future); QGuiApplication::setOverrideCursor(Qt::WaitCursor); QObject::connect(&watcher,&QFutureWatcher<void>::finished,[&](){ box.close(); box.deleteLater(); QGuiApplication::restoreOverrideCursor(); }); box.exec(); QMessageBox::information(this,qApp->applicationName(),"Carpeta comprimirda."); }@lincoln said in How? I show a message, while the task is running:
box.deleteLater();
Don't delete stack allocated objects!
box will be deleted automatically as soon as on_pushButton_5_clicked() finishes. -
@lincoln said in How? I show a message, while the task is running:
box.deleteLater();
Don't delete stack allocated objects!
box will be deleted automatically as soon as on_pushButton_5_clicked() finishes. -
Hi,
What do you mean by not responding ?
-
-
Are you sure that compress is called ?
How many times ?
How fast ?
Any runtime warning related to signals and slots ?
Why are you using map since your compress method takes a list as parameter ? -
-
@Lincoln remove the
QMessageBox::warning(this,qApp->applicationName(),"Error al comprimir la carpeta.");from compress
no gui stuff in other threads.