Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

no member named 'write' in 'QString'



  • I want to write some datas in a json file. I have written a code for it. I got this error in
    fileJson.write part. What is my mistake?

    QString fileJson;
    
    fileJson = QFileDialog::getSaveFileName(nullptr,
                                            ("Choose"), ".json", ("Choosen*.json)"));
    
    qDebug() << fileJson;
    if (fileJson.isNull())
    {
        qDebug()<< "Dosya seçilmedi";
        return;
    }
    if (!fileJson.contains(".up"))
        fileJson.append(".up");
    
    
    QFile file(fileJson);
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file);   // we will serialize the data into the file
    out << QString("Uçankaya Rota Dosyası");
    out << (qint32)0xffffffff;    
    
    
     file.resize(0);
    
    
    ROTA r;
    ROTA_NOKTA *rn;
    QList<Point*> nlar=rt->points();
    int v_i;
    
    r = rt->rotani_soyle();
    
    v_i= sizeof(r);
    r.nkt_say = nlar.count();
    rn=((ZPNokta *)(nlar.at(0)))->rota_nokta();
    r.nkt_bsl = rn->no;
    
    v_i = sizeof(*rn);
    for (int k=0;k<nlar.count(); k++)
    {
        rn=((ZPNokta *)(nlar.at(k)))->rota_nokta();
    
    }
    
    
    QJsonObject root;
    QJsonObject rota;
    rota.insert("rota no", r.no);
    rota.insert("baslangic nokta", r.nkt_bsl);
    
     root.insert("Rota", rota);
     QJsonDocument doc;
     doc.setObject(root);
    
     fileJson.write(doc.toJson());
     file.close();

  • Lifetime Qt Champion

    @suslucoder said in no member named 'write' in 'QString':

    What is my mistake?

    What your mistake is is explained in the error message...



  • @jsulm when i run just the writing part, it is okey. I had the write in QString. I didnt understand


  • Lifetime Qt Champion

    @suslucoder said in no member named 'write' in 'QString':

    I didnt understand

    What did you not understand? QString has no method called "write", so how is this line going to work if fileJson is a QString:

    fileJson.write(doc.toJson());
    file.close();
    

    ?!
    If you want to write to a file use QFile, not QString...
    Did you spend even a second to find your mistake? I really wonder...


  • Moderators

    @suslucoder said in no member named 'write' in 'QString':

    QFile file(fileJson);
    file.open(QIODevice::WriteOnly);
    QDataStream out(&file); // we will serialize the data into the file
    out << QString("Uçankaya Rota Dosyası");
    out << (qint32)0xffffffff;

    file.resize(0

    you can replace that with

    QFile file(fileJson);
    if(!file.open(QIODevice::WriteOnly)
          return;
    

    its the same thing + you exit if the file could not be opened

    fileJson.write(doc.toJson());
    file.close();

    Heres your error, fileJson is a string, you probably meant to write file.write(doc.toJson());



  • @J-Hilk said in no member named 'write' in 'QString':

    Heres your error, fileJson is a string, you probably meant to write file.write(doc.toJson());

    I realized it now. Thank you so much



  • @suslucoder said in no member named 'write' in 'QString':

    file.open(QIODevice::WriteOnly);

    As I wrote you about this in another of your topics, JSON files are text, and I would expect you to open with file.open(QIODevice::WriteOnly | QIODevice::Text);.

    Although I realize the JSON parsing of a file will work OK with or without QIODevice::Text, if you are under Windows (most people seem to be if they don't mention which OS they are) using the default JSON output format and you look at the file in, say, Notepad, I would have thought you will see it looks "wrong", without proper newlines (\r\n) all over the place? It may not matter for JSON, but you might be advised to get used to using this flag whenever you write/read a text file.



  • @JonB Im on ubuntu. Thank you for your warning. I fixed it



  • @suslucoder said in no member named 'write' in 'QString':

    Im on ubuntu

    :) Oh, OK, then it doesn't actually matter as there is no "text vs binary" mode/format under Linux! Nonetheless I think it's good practice to put QIODevice::Text in, just in case you ever port to Windows...


Log in to reply