Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

delete a QThread after processing



  • hi,

    I have this method in my class

     void showPreviews(QString path){        
            QFile file(path);
            if(!file.exists()){
                qDebug()<<"fichier introuvable..";
                return;
            }
            QThread *thread = QThread::create([=]{ preProcessFile(path);});
            thread->start();     
            return;
        }
    

    silly question : do i have to delete the thread and where ?
    should i connect finished() to deleteLater ?

    thank you



  •   void showPreviews(QString path){
            qDebug()<<"loading the file for the preview" << path;
            QFile file(path);
            if(!file.exists()){
                qDebug()<<"fichier introuvable..";
            }
    
            QThread *thread = QThread::create([=]{ preProcessFile(path);});
            QObject::connect(thread,&QThread::finished,thread,&QThread::deleteLater);
            QObject::connect(thread,&QThread::destroyed,[](){qDebug()<<"thread Deleted";});
            thread->start();
            return;
        }
    
    


  • @LeLev Yes, you have to delete the thread!

     void showPreviews(QString path){        
            QFile file(path);
            if(!file.exists()){
                qDebug()<<"fichier introuvable..";
                return;
            }
            QThread *thread = QThread::create([=]{ 
                preProcessFile(path);
                QThread::currentThread()->deleteLater();
            });
            thread->start();     
            return;
        }
    

    But I think, the easiest way to process a function in background is to use QtConncurrent::run() ==> https://doc.qt.io/qt-5/qtconcurrentrun.html#

     void showPreviews(QString path){        
            QFile file(path);
            if(!file.exists()){
                qDebug()<<"fichier introuvable..";
                return;
            }
            QtConcurrent::run([=]{ 
                preProcessFile(path);
            });
            return;
        }
    


  • @KroMignon thank you! I will try QtConncurrent::run() also, thank you for the suggerstion


Log in to reply