Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. no member named 'write' in 'QString'
QtWS25 Last Chance

no member named 'write' in 'QString'

Scheduled Pinned Locked Moved Solved General and Desktop
qt5jsonwrite
9 Posts 4 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted286
    wrote on 4 Feb 2021, 08:01 last edited by
    #1

    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();
    
    J J J 3 Replies Last reply 4 Feb 2021, 08:03
    0
    • D deleted286
      4 Feb 2021, 08:01

      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();
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Feb 2021, 08:03 last edited by
      #2

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

      What is my mistake?

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 4 Feb 2021, 08:05
      3
      • J jsulm
        4 Feb 2021, 08:03

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

        What is my mistake?

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

        D Offline
        D Offline
        deleted286
        wrote on 4 Feb 2021, 08:05 last edited by
        #3

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

        J 1 Reply Last reply 4 Feb 2021, 08:07
        0
        • D deleted286
          4 Feb 2021, 08:05

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 4 Feb 2021, 08:07 last edited by jsulm 2 Apr 2021, 08:08
          #4

          @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...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          7
          • D deleted286
            4 Feb 2021, 08:01

            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();
            
            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 4 Feb 2021, 08:10 last edited by
            #5

            @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());


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            D 1 Reply Last reply 4 Feb 2021, 08:12
            1
            • J J.Hilk
              4 Feb 2021, 08:10

              @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());

              D Offline
              D Offline
              deleted286
              wrote on 4 Feb 2021, 08:12 last edited by
              #6

              @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

              1 Reply Last reply
              0
              • D deleted286
                4 Feb 2021, 08:01

                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();
                
                J Online
                J Online
                JonB
                wrote on 4 Feb 2021, 09:19 last edited by JonB 2 Apr 2021, 09:20
                #7

                @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.

                D 1 Reply Last reply 4 Feb 2021, 11:30
                1
                • J JonB
                  4 Feb 2021, 09:19

                  @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.

                  D Offline
                  D Offline
                  deleted286
                  wrote on 4 Feb 2021, 11:30 last edited by
                  #8

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

                  J 1 Reply Last reply 4 Feb 2021, 11:36
                  1
                  • D deleted286
                    4 Feb 2021, 11:30

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

                    J Online
                    J Online
                    JonB
                    wrote on 4 Feb 2021, 11:36 last edited by JonB 2 Apr 2021, 11:37
                    #9

                    @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...

                    1 Reply Last reply
                    0

                    9/9

                    4 Feb 2021, 11:36

                    • Login

                    • Login or register to search.
                    9 out of 9
                    • First post
                      9/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved