长话短说,我有一个程序,它使用QDomDocument类创建一个xml文件,然后使用save()函数将其保存到文本流对象中。所以基本上
QDomDocument somedoc;
//create the xml file, elements, etc.
QFile io(fileName);
QTextStream out(&io);
doc.save(out,4);
io.close();
我希望能够使用QProgressDialog类显示保存的进度,但我很难弄清楚它。是否有一种方法可以逐步检查文件是否经过处理并只更新进度?有什么建议吗?谢谢。
发布于 2015-04-10 06:43:11
首先,我认为我们可以在Qt
源代码中找到答案,但并不是那么简单,所以我找到了更简单的解决方案,只需使用toString()
方法并像往常一样编写它。例如:
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();
如果您想查看QDomDocument::save()
的源代码,可以在
qt-everywhere-opensource-src-5.4.1.zip\qt-everywhere-opensource-src-5.4.1\qtbase\src\xml\dom
https://stackoverflow.com/questions/29546650
复制相似问题