【发布时间】:2018-10-02 13:53:05
【问题描述】:
我正在尝试将数据保存到 Qt 中的 XML 文件。到处都被告知我需要编写这样的检查:
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to open";
}
else
{
//write to file;
}
我总是得到“无法打开”的结果。我只是找不到任何关于它的东西。如果我不使用此检查,我会收到这种错误:QIODevice::write (QFile, "D:/logs.xml"): device not open。我只是不知道该怎么办。这是我在教程中尝试使用的整个代码:
QDomDocument document;
// Making the root element
QDomElement root = document.createElement("Dorms");
// Adding the root element to the docuemnt
document.appendChild(root);
QFile file;
file.setFileName("D:/logs.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "Failed to open";
}
else
{
QTextStream stream(&file);
stream << document.toString();
file.close();
qDebug() << "Done";
}
有谁知道我做错了什么?
【问题讨论】: