7

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.

1 Answer 1

3

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.