Search code examples
jsonqtqfile

QIODevice::read : device not open


I finally found a path file QFile would accept using QFile.exist() and a healthy dose of trial and error.

I want to know why the following works:

#include <QFile>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>

QString path = QDir::currentPath();     // Get current dir
path.append("/noteLibrary.json");

QFile file(path);           // Give QFile current dir + path to file
if (!file.exists()) {       // Check to see if QFile found the file at given file_path
    qDebug() << "NO FILE HERE";
}
qDebug() << path;           // See what path was finally successful
file.open(QIODevice::ReadOnly);      // Continue parsing document to confirm everything else is functioning normally.
QByteArray rawData = file.readAll();

// Parse document
QJsonDocument doc(QJsonDocument::fromJson(rawData));

// Get JSON object
QJsonObject json = doc.object();

// Access properties
qDebug() << json["die"].toString();     // Should output "280C4"

Successful output:

"/home/pi/noteLibrary.json"
"280C4"

But the following does NOT work:

#include <QFile>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>

QFile file("/home/pi/noteLibrary.json");           // Give QFile current dir + path to file
if (!file.exists()) {       // Check to see if QFile found the file at given file_path
    qDebug() << "NO FILE HERE";
}

//qDebug() << path;           // See what path was finally successful
file.open(QIODevice::ReadOnly);      // Continue parsing document to confirm everything else is functioning normally.
QByteArray rawData = file.readAll();

// Parse document
QJsonDocument doc(QJsonDocument::fromJson(rawData));

// Get JSON object
QJsonObject json = doc.object();

// Access properties
qDebug() << json["die"].toString();     // Should output "280C4"

Error output:

NO FILE HERE
QIODevice::read (QFile, "/home/pi/Desktop/noteLibrary.json"): device not open
""

Why would QFile treat these differently? Is this a QString format issue? Or is the fact that I'm deploying this remotely to a Raspberry Pi 3 possibly to blame?


Solution

  • Regardless of what was wrong with my code above, with the below code giving QFile the absolute path does work equally to creating a QString with currentPath(). I must have had something else wrong, my mistake!

    noteLibrary.json

    {"note": [{
                 "profile": "C4",
                 "die": "280C4",
                 "pressure": 800,
                 "position": 10000
             },
             {
                 "profile": "CC4",
                 "die": "2280C4",
                 "pressure": 8800,
                 "position": 110000
             }
    
        ],
        "test": {
            "profile": "CCC4",
            "die": "22280C4",
            "pressure": 88800,
            "position": 1110000
        }
    }
    

    main.cpp excerpt

        QFile file("/home/pi/noteLibrary.json");
        if (!file.exists()) qDebug() << "NO FILE FOUND";
        file.open(QIODevice::ReadOnly);
        QByteArray rawData = file.readAll();
        QJsonDocument doc(QJsonDocument::fromJson(rawData));    // Parse document
        QJsonObject jObj = doc.object();    // Get JSON object
        qDebug() << jObj["test"];
    

    Application Output

    QJsonValue(object,QJsonObject({"die":"22280C4","position":1110000,"pressure":88800,"profile":"CCC4"}))
    

    Seems odd that it displays property values in alphabetical order, not the order listed in the document.