Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QProgressDialog along with QDomDocument save function

Tags:

c++

qt

To make a long story 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 its

QDomDocument somedoc;

//create the xml file, elements, etc. 


QFile io(fileName);
QTextStream out(&io);
doc.save(out,4);
io.close();

I want to be able to show the progress of the save using the QProgressDialog class, but I'm having a hard time figuring it out. Is there a way I can incrementally check to see if the file is through processing and just update the progress? Any suggestions? Thanks.

like image 523
inspiration Avatar asked Apr 09 '15 18:04

inspiration


1 Answers

Firstly, I thought that we can find answer in Qt source code, but it was not so simple, so I found easier solution, just use toString() method and write it as usual file. For example:

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 look at the source code of QDomDocument::save(), you can find it in

qt-everywhere-opensource-src-5.4.1.zip\qt-everywhere-opensource-src-5.4.1\qtbase\src\xml\dom

or just on the GitHub.

like image 105
Kosovan Avatar answered Nov 01 '22 18:11

Kosovan