![](/img/trans.png)
[英]How to make QProgressDialog stay when process function is complete?
[英]How to use QProgressDialog along with QDomDocument save function
总而言之,我有一个程序,它使用QDomDocument类创建一个xml文件,然后使用save()函数将其保存到文本流对象。 所以基本上它
QDomDocument somedoc;
//create the xml file, elements, etc.
QFile io(fileName);
QTextStream out(&io);
doc.save(out,4);
io.close();
我希望能够使用QProgressDialog类显示保存的进度,但我很难搞清楚它。 有没有办法我可以逐步检查文件是否通过处理,只是更新进度? 有什么建议么? 谢谢。
首先,我认为我们可以在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-比比皆是 - 开源-SRC-5.4.1 \\ qtbase的\\ src \\ XML \\ DOM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.