0

简单的问题,我只是无法打开/创建文件。它应该将 xml 文件中的一些设置保存到给定的路径。

我这样调用方法:

xmlwriter->write_settings("./settings.xml");

int XmlWriter::write_settings(QString path)
{
    qDebug() << "Path is: " + path;

    QDomDocument document;

    QDomElement root = document.createElement("settings");
    document.appendChild(root);

    QDomElement node;
    node.setAttribute("name", "Its me!");
    node.setAttribute("series", "25");
    node.setAttribute("PMT", "200");

    root.appendChild(node);

    QFile file(path);
    if(!file.open(QIODevice::ReadWrite, QIODevice::Text))
    {
        qDebug() << "Opening file failed!";
        return 1;
    }
    else
    {
        QTextStream stream(&file);
        stream << document.toString();
        file.close();
        qDebug() << "wrote file to " + path;
        return 0;
    }
}
4

1 回答 1

2

您没有正确传递参数,因此您可能会调用的多态版本QFile::open

尝试这个:

QFile file(path);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
      qDebug() << "Opening file failed!";
     return 1;
}
else
{
    QTextStream stream(&file);
    stream << document.toString();
    file.close();
    qDebug() << "wrote file to " + path;
    return 0;
}
于 2013-08-16T14:44:49.853 回答