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. How to create array of objects at runtime.
Forum Updated to NodeBB v4.3 + New Features

How to create array of objects at runtime.

Scheduled Pinned Locked Moved General and Desktop
14 Posts 8 Posters 11.6k 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.
  • A Offline
    A Offline
    agupta
    wrote on last edited by
    #1

    Hi All

    In my project I need to read a file and each line in a file consists of two values and these two values needs to be stored in the object, and all the object need to be inserted into the vector.

    My file is something like this:
    4,5
    6,5
    ...
    ...
    ...

    so for first line I have to create an object and store the value 4 and 5 in private member of the class.

    I don't know how many lines are there in my file. As per my understanding I have to create an array of objects (object for each line) at runtime and store the values in it and intsert these objects in a vector.

    Please let me know if anybody can help me on this.

    Thank You,
    Aditya Gupta

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      Mmm, QList is not enough for you here?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        agupta
        wrote on last edited by
        #3

        Hello Denis

        Sorry, I have not used QT much.
        Can you please give me some example, how to use QList in this situation

        Thanks!!!

        Thank You,
        Aditya Gupta

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          Please, look "here":http://doc.qt.nokia.com/4.7/qlist.html . You will find a bunch of examples there.

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

            Hello Denis

            I still have a doubt....

            After reading the data(line by line) from file, I have to store them in my class member variables for each object and move this object in QVector... So how can i do this using QList?

            Thank You,
            Aditya Gupta

            1 Reply Last reply
            0
            • L Offline
              L Offline
              LinusA
              wrote on last edited by
              #6

              [quote author="agupta" date="1311226392"]Hi All

              In my project I need to read a file and each line in a file consists of two values and these two values needs to be stored in the object, and all the object need to be inserted into the vector.

              My file is something like this:
              4,5
              6,5
              ...
              ...
              ...
              [/quote]
              First you have to decide whether you need a QList or a QVector. In your case, I'd probably prefer a QVector. See here fore more details: http://doc.qt.nokia.com/4.7-snapshot/containers.html

              Now, there is a nice handy thing called QPair http://doc.qt.nokia.com/latest/qpair.html.

              In your example, simply do:
              @
              QVector< QPair<int, int> > myPairVec;

              // if you know the number of lines in your file, pre-allocate
              // the vector for best speed:
              myPairVec.reserve(numLines);

              // now add to the vector
              for(int i = 0; i < numLines; ++i) {
              // read line from file
              // parse line
              // put values into integers x and y

              // now we can add to the vector
              myPairVec.append(qMakePair(x, y));
              }//end for

              // if you want to access elements, do so by
              cout << "first x = " << myPairVec.at(0).first << endl;
              cout << "last y = " << myPairVec.last().second << endl;
              @

              1 Reply Last reply
              0
              • L Offline
                L Offline
                LinusA
                wrote on last edited by
                #7

                Oh, and you could've done the same thing with a QList of course. The difference is that QList has one more level of "indirection", whereas QVector stores its element in one continuous memory block. This influences performance (good or bad, depends). But for your case: Probably doesn't matter.

                qMakePair is a useful helper that will create a QPair<T1, T2>, based on the types of the arguments...

                So you wouldn't have to create an object. On the other hand, the members .first and .second are public, and in your first post you said you needed private members...

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  [quote author="LinusA" date="1311228084"]So you wouldn't have to create an object. On the other hand, the members .first and .second are public, and in your first post you said you needed private members...[/quote]

                  Of course, a QPair can always be used as a private member variable in an object too, should that use case be appropriate.

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    yanbellavance
                    wrote on last edited by
                    #9

                    you can also do this:

                    @struct myPair{
                    int val1;
                    int val2;
                    };

                    QList <myPair> myPairList;@

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      agupta, could you tell us a bit more about this object you need to store your values in? Is it an object you already have? If so, what does it look like? Note that not all objects are suitable to store as value in a container. You may need to store a pointer to the object on the heap instead. QObject and everything derived from it is such a type of object...

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        saravanavelu
                        wrote on last edited by
                        #11

                        i have two tables internaldelivery and products.in internaldelivery table(pid,pname,qty,unit,preparedby,givento,date)is there.i have inserted (pname,qty,unit,preparedby,givento,date)in internaldelivery table.in internaldeliverytable (pname) and also in products table (name) is same.how can i get the pid value to insert the internaldeliverytable. Please find and say the solution for me.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          saravanavelu
                          wrote on last edited by
                          #12

                          anybody say that qt is end or not

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

                            [quote author="saravanavelu" date="1378978443"]anybody say that qt is end or not[/quote]

                            I have no clue what you are asking, or how it is relevant to this topic.

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              [quote author="saravanavelu" date="1378978443"]anybody say that qt is end or not[/quote]

                              Hi saravanavelu,
                              Do you mean that Qt is dead or still working. If that is you question, then
                              let me tell you that Qt is now acquired by Digia from Nokia. AFAIK Qt is now one of the most used IDE in the world. Qt is has released its new version, ie Qt 5.1.1 recently. Also developments are going on different OS like Android, iOS, Linux, Windows and many more.
                              Many companies are nowadays using Qt as their development platforms.
                              Check the Qt website and you can find lots of changes have occurred in the recent years.

                              Regards
                              Ansif

                              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