How to use QProgressDialog with the QDomDocument save function

In short, I have a program that uses the QDomDocument class to create an xml file, and then uses the save () function to save it to a text stream object. So basically his

QDomDocument somedoc; //create the xml file, elements, etc. QFile io(fileName); QTextStream out(&io); doc.save(out,4); io.close(); 

I want to show save progress using the QProgressDialog class, but it's hard for me to understand it. Is there a way by which I can step by step check whether the file is viewing the processing and just updating the progress? Any suggestions? Thank you

+6
source share
1 answer

Firstly, I thought that we could find the answer in the Qt source code, but it was not so simple, so I found an easier solution, just use the toString() method and write it as a regular file. For instance:

 QStringList all = doc.toString(4).split('\n');//4 is intent int numFiles = all.size(); QProgressDialog *progress = new QProgressDialog("Copying files...", "Abort Copy", 0, numFiles, this); progress->setWindowModality(Qt::WindowModal); QFile file("path"); file.open(QIODevice::WriteOnly); progress->show(); QTextStream stream(&file); for (int i = 0; i < numFiles; i++) { progress->setValue(i); if (progress->wasCanceled()) break; stream << all.at(i) << '\n'; QThread::sleep(1);//remove these lines in release, it is just example to show the process QCoreApplication::processEvents(); } progress->setValue(numFiles); file.close(); 

If you want to see the source code of QDomDocument::save() , you can find it in

qt-everywhere-opensource-src-5.4.1.zip \ Qt-everywhere open-source -src-5.4.1 \ qtbase \ SRC \ XML \ home

or just on github.

+3
source

Source: https://habr.com/ru/post/984914/


All Articles