0
votes

Simple problem, I just can't open/create a file. It's supposed to save some settings in an xml-file to a given path.

I call the method like this:

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
How does the path look like?Leo Chapiro
well its "./settings.xml"Serious Sammy
Did you tried absolute path? I think you need to post an error for claritySpongeBobFan
I tried an absolute path too. The error is this "QFile::open: File access not specified".Serious Sammy
Also file.open(QIODevice::ReadWrite, QIODevice::Text) is a mistake, you wanted to write file.open(QIODevice::ReadWrite | QIODevice::Text) I thinkSpongeBobFan

1 Answers

2
votes

You don't pass parameters correctly, so you probably invoke a polymorphic version of QFile::open

Try this:

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;
}