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. Save/Read struct to/from File
Qt 6.11 is out! See what's new in the release blog

Save/Read struct to/from File

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 9.4k Views 2 Watching
  • 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
    droelf
    wrote on last edited by
    #1

    I have to save multiple pairs of (QPointF, Int) in a file and need to read them again.

    This is my idea right now:

     typedef pair<QPointF,int> vertex_info;   
    
     struct graph_info{
           QList<vertex_info> all_vertices;
     };
    
    
    void MainWindow::on_button_save_clicked(){
       QString s = this->ui->lineEdit->text();
       this->ui->lineEdit->clear();
    
       struct graph_info my_graph;
    
       vmap::iterator itr = myLogic->set.begin();
       while(itr != myLogic->set.end()){
            v = itr->second;
           my_graph.all_vertices.append(vertex_info(v->pos,v->type));
        
           itr++;
        }
    
       QFile new_graph(s);
       new_graph.open(QIODevic::WriteOnly);
    
        
    }
    

    How to save/read my struct properly?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi,

      I suggest to create QDataStream::operator<<() and QDataStream::operator>>() for your structure

      QDataStream& operator<<(QDataStream& stream, const graph_info& info) 
      {
       ...
      }
      
      QDataStream& operator>>(QDataStream& stream, graph_info& info) 
      {
       ...
      }
      

      In that case you can save your data

      QFile file("file.dat");
      file.open(QIODevice::WriteOnly);
      QDataStream out(&file);   // we will serialize the data into the file
      out << my_graph;
      

      and read

      QFile file("file.dat");
      file.open(QIODevice::ReadOnly);
      QDataStream in(&file);    // read the data serialized from the file
      in >> my_graph;  
      

      UPDATE: a complete different approach is to save your data in text format (for example JSON). Here you'll find a great example

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      D 1 Reply Last reply
      0
      • M mcosta

        Hi,

        I suggest to create QDataStream::operator<<() and QDataStream::operator>>() for your structure

        QDataStream& operator<<(QDataStream& stream, const graph_info& info) 
        {
         ...
        }
        
        QDataStream& operator>>(QDataStream& stream, graph_info& info) 
        {
         ...
        }
        

        In that case you can save your data

        QFile file("file.dat");
        file.open(QIODevice::WriteOnly);
        QDataStream out(&file);   // we will serialize the data into the file
        out << my_graph;
        

        and read

        QFile file("file.dat");
        file.open(QIODevice::ReadOnly);
        QDataStream in(&file);    // read the data serialized from the file
        in >> my_graph;  
        

        UPDATE: a complete different approach is to save your data in text format (for example JSON). Here you'll find a great example

        D Offline
        D Offline
        droelf
        wrote on last edited by
        #3

        @mcosta

        thx for the fast answer, but how does the inside of these QDataStream declarations look like?

        And what happens exactly in line:

              in >> my_graph;
        

        The Stream "in" looks into the file "file.dat" and reads it.

        Sry i am kind of new but a task at my University forces me to find an answer to this problem :/

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          you have to implements the << and >> operators for your struct.
          Read the QDataStream Documentation to understand how implement them

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            droelf
            wrote on last edited by
            #5

            I came to this point:

            struct vertex_info{
                 QPointF pos;
                 int type;
            };
            
            
            struct graph_info{
                QList<vertex_info> all_vertices;
             };
            
            
             QDataStream &operator<<(QDataStream &out, const vertex_info &v){
            
                   out << v.pos << v.type;
                  return out;
             }
            
            
            
             QDataStream &operator<<(QDataStream &out, const graph_info &g){
            
                   int n = g.all_vertices.size();
            
                   for(int i=0; i<n ;i++){
                           out<<g.all_vertices.at(i);
                    }
            
                    return out;
              }
            
            
            
            
            
            
            
                 QDataStream &operator>>(QDataStream &in, graph_info &g){
                         QList<vertex_info> L;
            
            
                         return in;
                  }
            
            
            
                  QDataStream &operator>>(QDataStream &in, vertex_info &v){
            
            
            
                          return in;
                   }
            
            
            
               void MainWindow::on_button_save_clicked(){
                        QString s = this->ui->lineEdit->text();
                        this->ui->lineEdit->clear();
            
                        vmap::iterator itr = myLogic->set.begin();
                        graph_info my_graph = new graph_info();
            
            
                        vertex_info vinfo;
            
                       while(itr != myLogic->set.end()){
                               vinfo.first = itr->second->pos;
                               vinfo.second = itr->second->type;
                               my_graph.all_vertices.append(vinfo);
                               itr++;
                        }
            
            
                       QFile file(s);
                       file.open(QIODevice::WriteOnly);
                       QDataStream stream(&file);
                       stream << my_graph;
            
                       file.close();
                    }
            

            Any suggestions or solutions? :/

            1 Reply Last reply
            0
            • D Offline
              D Offline
              droelf
              wrote on last edited by
              #6

              Anyone? :/

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                Hi,

                you have to decide the format of your file; we got you information how to use properly the QT classes but you should thing about the format.

                Once your problem is solved don't forget to:

                • Mark the thread as SOLVED using the Topic Tool menu
                • Vote up the answer(s) that helped you to solve the issue

                You can embed images using (http://imgur.com/) or (http://postimage.org/)

                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