Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. How to write and read a QList with binary files
Forum Updated to NodeBB v4.3 + New Features

How to write and read a QList with binary files

Scheduled Pinned Locked Moved Brainstorm
16 Posts 3 Posters 8.9k Views 1 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.
  • M Offline
    M Offline
    Max13
    wrote on last edited by
    #4

    Yes, it's written in the doc:

    bq. This function requires the value type to implement operator>>()

    You have to define the operator, natively the compiler doesn't know how to write your type:

    @QDataStream & operator<<(QDataStream & stream, const MyType & myVar)@

    And don't forget, the pointer is just a pointer (so it's just an address), you should dereference it, but it's up to you when you define your operator<<(). If your QList is defined with pointers, you will define operator<<() with pointer as the second type.

    We all have started by asking questions. Then after some time, we can begin answering them.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #5

      You need to define the stream operators for your class
      @
      QDataStream & operator>>(QDataStream & in, car* aCar)
      QDataStream & operator<<(QDataStream & out, car* aCar)
      @
      QList will call the stream operator for each its element.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ricardo210
        wrote on last edited by
        #6

        Hey, I do what you say but now gives me this error

        QDataStream& car::operator>>(QDataStream&, car*)' must take exactly one argument
        QDataStream & operator>>(QDataStream & in, car* aCar);
        ^

        1 Reply Last reply
        0
        • R Offline
          R Offline
          ricardo210
          wrote on last edited by
          #7

          and sorry for all this questions and stuff is just I'm new in this

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by
            #8

            If you define operator>> as a member of a class then you need only one parameter QDataStream&
            If you define operator as an external function then you need two.
            QList uses external functions because QList does not know anything about its content except of a type.
            Move the operator declarations out of car class and it should work.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #9

              [quote author="ricardo210" date="1410893187"]and sorry for all this questions and stuff is just I'm new in this[/quote]
              This forum was created to help the developers who uses Qt to find the answers on their ultimate questions. So, without the questions the forum will die.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                ricardo210
                wrote on last edited by
                #10

                what woul happend if i want to write a qlist of objects who have a qlist inside them on the binary file?

                should I overload all the <<and>> for all the classes?

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  ricardo210
                  wrote on last edited by
                  #11

                  hey, guys I almost end my program but givesme an error qhen i tried to take out a qlist from one of my variables car here is the code who is giving me problesma;
                  @QDataStream& operator>>(QDataStream &out, car *&list)
                  {
                  QString w,x,y,z;
                  out>>w;
                  out>>x;
                  out>>y;
                  out>>z;
                  out>>list->register;
                  list->plate=w;
                  list->brand=x;
                  list->=mileage.toDouble();
                  list->=cylinder_capacity.toDouble();
                  return out;
                  }
                  @
                  Here is it where I overload

                  @QDataStream& operator<<(QDataStream &out, QList<gasoline*> &list)
                  {
                  while (!out.atEnd())
                  {
                  gasoline* petrol = new gasoline();
                  out >> petrol;
                  list.append(petrol);
                  }
                  return out;
                  }@

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    ricardo210
                    wrote on last edited by
                    #12

                    when i tried to read te file givesme an error

                    @ QFile read("register.txt");
                    if(read.exists()){
                    if (read.open(QIODevice::ReadOnly)){;
                    QDataStream in(&read);
                    in >> cars;
                    read.close();
                    }
                    }@
                    here is when i am reading
                    cars is my qlist of car

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andreyc
                      wrote on last edited by
                      #13

                      [quote]but givesme an error[/quote]
                      Could you post an error message here.

                      I see couple things here

                      • these lines should give an error on compilation
                        @
                        list->=mileage.toDouble();
                        list->=cylinder_capacity.toDouble();
                        @

                      • here you defined operator for output but trying to read from the stream
                        @
                        QDataStream& operator<<(QDataStream &out, QList<gasoline*> &list)
                        {
                        ...
                        out >> petrol;
                        ...
                        }
                        @

                      You don't need to define stream operator for QList<gasoline*>
                      As "Max13 said":http://qt-project.org/forums/viewthread/47469/#194374 you need to define a stream operator for gasoline.
                      QList will use it.

                      1 Reply Last reply
                      0
                      • R Offline
                        R Offline
                        ricardo210
                        wrote on last edited by
                        #14

                        ok thanks for the information i just do what you said but still when i run the program qt says:
                        The program has unexpectedly finished.
                        and I dont know if this error is because I'm savin wrong at the end of the program or I'm reading wrong the binary file at the start

                        here is where I write
                        @QFile file("register.txt");
                        if (file.open(QIODevice::WriteOnly))
                        {
                        QDataStream out(&file);
                        out << cars;
                        file.close();
                        }@

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          ricardo210
                          wrote on last edited by
                          #15

                          it seem what the error is in this line
                          @QDataStream& operator>>(QDataStream &out, car *&list)
                          {
                          QString w,x,y,z;
                          out>>w;
                          out>>x;
                          out>>y;
                          out>>z;
                          out>>list->register; //<--- It seems this is the line with the error
                          list->plate=w;
                          list->brand=x;
                          list->mileage=y.toDouble();
                          list->cylinder_capacity=z.toDouble();
                          return out;
                          }@
                          in this part register is a qlist too

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            Max13
                            wrote on last edited by
                            #16

                            When you want to write/read a QList using operator>>()/operator<<(), as the doc says, the type of the QList must also implement the SAME operator.

                            Which means that except Qt's included types (quint, QString, QByteArray, ...), you need to provide such an operator for your type.

                            I see you have provided one, and you're pointing out a problem when using it on another QList (line 8: list->register). Is there a stream operator defined for the type of element in list->register ?

                            We all have started by asking questions. Then after some time, we can begin answering them.

                            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