Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How? I show a message, while the task is running
Qt 6.11 is out! See what's new in the release blog

How? I show a message, while the task is running

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 5 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • lincolnL lincoln

    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;
      }
    }
    
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #2

    @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 the waitForFinished() in the slot if you want the user to see it.

    lincolnL 1 Reply Last reply
    1
    • JonBJ JonB

      @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 the waitForFinished() in the slot if you want the user to see it.

      lincolnL Offline
      lincolnL Offline
      lincoln
      wrote on last edited by lincoln
      #3

      @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.");
      
      }
      

      Solitary wolf

      jsulmJ 1 Reply Last reply
      0
      • lincolnL Offline
        lincolnL Offline
        lincoln
        wrote on last edited by
        #4

        Also try a ProgressDialog and QFutureWatcher as follows, but it doesn't compile, I get this error.
        b214f7f4-da09-4cd4-99ff-eba3dea38d4d-image.png

        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;
          }
        
        }
        

        Solitary wolf

        1 Reply Last reply
        0
        • lincolnL lincoln

          @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.");
          
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #5

          @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.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          lincolnL 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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.

            lincolnL Offline
            lincolnL Offline
            lincoln
            wrote on last edited by
            #6

            @jsulm
            Already remove deletelater and the message box is stuck, not responding.

            Solitary wolf

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #7

              Hi,

              What do you mean by not responding ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              lincolnL 1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                What do you mean by not responding ?

                lincolnL Offline
                lincolnL Offline
                lincoln
                wrote on last edited by
                #8

                @SGaist
                I mean when removing box.deleteLater (); as @jsulm mentions, it does not close, and stays there. And I can't close the application either, the entire App is frozen.

                It seems to me that it is best to do it with QFutureWatcher, as I put in the code below, but it does not work either.

                Solitary wolf

                J.HilkJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  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 ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • lincolnL lincoln

                    @SGaist
                    I mean when removing box.deleteLater (); as @jsulm mentions, it does not close, and stays there. And I can't close the application either, the entire App is frozen.

                    It seems to me that it is best to do it with QFutureWatcher, as I put in the code below, but it does not work either.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #10

                    @Lincoln remove the

                    QMessageBox::warning(this,qApp->applicationName(),"Error al comprimir la carpeta.");
                    

                    from compress

                    no gui stuff in other threads.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    lincolnL 1 Reply Last reply
                    2
                    • J.HilkJ J.Hilk

                      @Lincoln remove the

                      QMessageBox::warning(this,qApp->applicationName(),"Error al comprimir la carpeta.");
                      

                      from compress

                      no gui stuff in other threads.

                      lincolnL Offline
                      lincolnL Offline
                      lincoln
                      wrote on last edited by lincoln
                      #11

                      @J-Hilk
                      ok It gives me the same result

                      Solitary wolf

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved