QFile,QFile :: open:未指定文件访问,打开文件失败

时间:2013-08-16 14:24:03

标签: qt file

简单问题,我只是无法打开/创建文件。它应该将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;
    }
}

1 个答案:

答案 0 :(得分:1)

您没有正确传递参数,因此您可能会调用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;
}