There is a need is that GUI graphical interface when uploading files to the server, need to use the zip command line packaging, because the file is a lot of time, the zip command packaging needs to calculate a long period, so the calculation of a large number of tasks to separate the background thread more appropriate, and then the task is completed, the signal slot mechanism to notify the foreground
The UI thread processes the results. So this thread needs to be destroyed, not the same as the previous background thread that has been running to receive network data.
Task class H files for compressed files:
1#include <QObject>2#include <QStringList>3 4 classZipupdatepackagetask: PublicQobject5 {6 Q_object7 8 Public:9 Zipupdatepackagetask ();Ten~Zipupdatepackagetask (); One A PublicSlots: - voiddozip (); - voidBezipfiles (Constqstringlist&files); the Signals: - voidzipfinished (); - voidZippackageinfo (Constqstring& FileName,Constqstring& MD5,Constqstring&size); - + Private: - BOOLbezipfilesisempty (); + voidDeletehistoryzip (Constqstring&targetpackage); A voidTobezipfilesarguments (QString &files); at voidStartziptask (QString zipexepath, QString targetpackage, QString files); -QString GetFileMD5 (Constqstring&targetzippackage); -QString GetFileSize (Constqstring&targetzippackage); - Private: - qstringlist m_files; -};
To compress a CPP file for a task class:
1#include <QCoreApplication>2#include <QFile>3#include <QFileInfo>4#include <QDir>5#include <QProcess>6#include <QCryptographicHash>7 8 Zipupdatepackagetask::zipupdatepackagetask ()9 {Ten } One Azipupdatepackagetask::~Zipupdatepackagetask () - { - } the - voidzipupdatepackagetask::d ozip () - { -QString Appdir = qapp->Applicationdirpath (); +QString Zipexepath = QString ("%1/%2"). Arg (appdir). Arg ("Zip.exe"); - if(Qfile::exists (zipexepath) &&!bezipfilesisempty ()) + { A atQString targetzippackage = QString ("%1/%2"). Arg (appdir). Arg ("Update.zip"); - QString bezipfiles; - - tobezipfilesarguments (bezipfiles); - Deletehistoryzip (targetzippackage); - Startziptask (Zipexepath, Targetzippackage, bezipfiles); in - to if(Qfile::exists (targetzippackage)) + { -QString size =GetFileSize (targetzippackage); theQString MD5 =getFileMD5 (targetzippackage); * $ emit Zippackageinfo (targetzippackage, MD5, size);Panax Notoginseng - } the + A } the + emit zipfinished (); - } $ $ voidZipupdatepackagetask::bezipfiles (Constqstringlist&files) - { - m_files.clear (); theM_files =files; - }Wuyi the BOOLZipupdatepackagetask::bezipfilesisempty () - { Wu returnm_files.isempty (); - } About $ voidZipupdatepackagetask::d Eletehistoryzip (Constqstring&targetpackage) - { - if(Qfile::exists (targetpackage)) - { A Qdir dir; + Dir.remove (targetpackage); the } - } $ the voidZipupdatepackagetask::tobezipfilesarguments (QString &files) the { the for(auto File:m_files) the { - files.append (file); inFiles.append (" "); the } the } About the voidZipupdatepackagetask::startziptask (QString zipexepath, QString targetpackage, QString files) the { theqprocess* zipprocesss =Newqprocess; + //zip A [zip_package_path_name] [File1path] ... [Filenpath] -QString command = QString ("%1 a%2%3"). Arg (Zipexepath). Arg (targetpackage). Arg (files); theZipprocesss->start (command);BayiZipprocesss->waitforfinished (-1); the } the -QString ZIPUPDATEPACKAGETASK::GETFILEMD5 (Constqstring&targetzippackage) - { the QString MD5; the QFile file (targetzippackage); the if(File.Open (qfile::readonly)) the { - Qcryptographichash Hash (QCRYPTOGRAPHICHASH::MD5); the if(Hash.adddata (&file)) the { theMD5 =QString (Hash.result (). Tohex ());94 } the the file.close (); the }98 returnMD5; About } - 101QString Zipupdatepackagetask::getfilesize (Constqstring&targetzippackage)102 {103 qfileinfo Fileinf (targetzippackage);104 returnQstring::number (Fileinf.size ()); the}
Here is the slot function that creates this background compression thread, and when the button clicked on the compressed upload, the following slot function is called:
1 voidstatisticswidget::slotuploadpackage ()2 {3Qthread *zipthread =NewQthread;4zipupdatepackagetask* task =NewZipupdatepackagetask ();5 6 7 qstringlist bezipfiles;8 for(intListRow =0; ListRow < Ui->updatefilelist->count (); ++ListRow)9 {TenBezipfiles << Ui->updatefilelist->item (ListRow)text (); One } A -Task->bezipfiles (bezipfiles); -Task->Movetothread (zipthread); the -Connect (Zipthread, &qthread::started, Task, &zipupdatepackagetask::d ozip); - - Connect (Task, SIGNAL (zipfinished ()), Zipthread, SLOT (Quit () )); +Connect (task, &zipupdatepackagetask::zippackageinfo, This, &statisticswidget::slotzipfinished,qt::queuedconnection); - + //automatically delete thread and task object when the work was done: A Connect (Zipthread, SIGNAL (finished ()), Task, SLOT (Deletelater () )); at Connect (Zipthread, SIGNAL (finished ()), Zipthread, SLOT (Deletelater () )); -Zipthread->start (); -}
Note that the above code is a temporary task and thread, and when the task is completed, it is automatically deleted due to the corresponding signal slot set. The task thread finishes what it does, sends out a zippackageinfo signal and destroys it. The UI thread can only write a slot function to receive the finished signal and handle it.
Creates a temporary background thread in QT5.