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 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();
    
    jsulmJ J.HilkJ JonBJ 3 Replies Last reply
    0
    • D deleted286

      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();
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      3
      • jsulmJ jsulm

        @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 last edited by
        #3

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

        jsulmJ 1 Reply Last reply
        0
        • D deleted286

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #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

            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.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on 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
            1
            • J.HilkJ J.Hilk

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

                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();
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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
                1
                • JonBJ JonB

                  @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 last edited by
                  #8

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

                  JonBJ 1 Reply Last reply
                  1
                  • D deleted286

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #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

                    • Login

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