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. The best way to store some data in a matrix
QtWS25 Last Chance

The best way to store some data in a matrix

Scheduled Pinned Locked Moved General and Desktop
12 Posts 7 Posters 19.9k 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.
  • L Offline
    L Offline
    lycis
    wrote on 2 Feb 2011, 11:55 last edited by
    #2

    If you just need five columns, you could possibly create your matrix as a list of arrays, something like:

    @QList<QVariant*> matrix@

    if every list item contains an array of size 5 you'll have five columns. Furthermore if you'r matrix is static (like 5x5 matrix) you may implement it as an more dimensional array only, without any QList.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 2 Feb 2011, 11:59 last edited by
      #3

      One general way would be to do it as follows:
      @
      QList< QList<data> > matrix;
      @
      where data represents the data type to store, in general QVariant.

      If you have always the same types and know the types before implementing, I would make a structure for one row, like this:

      @
      struct MyRow
      {
      int first;
      QString second;
      QDateTime third;
      ...
      }

      QList< MyRow > matrix;
      

      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on 2 Feb 2011, 12:13 last edited by
        #4

        Please do not use C style arrays in Qt or in C++ in general. It is almost always not what you want! The container classes of Qt, stdlib or boost are easier to use and much more save!

        You can easily achieve something similar with Qt classes like this:

        @
        QList<QList<QVariant> > matrix;
        @

        Respect the space between the two >, it is a must otherwise the compiler interprets it as operator>> and bails out.

        Though, this solution is suboptimal, as it does not constrain your matrix, i.e. it is not forced to be 5x5, it's not even possible to constraint every inner list to have the same amount of entries.

        I'd propose writing your own small class, with a simple storage, (e.g. QVarLengthArray or QVector). Put all in one single container and calculate the offset like this:

        @
        int matrixSize = 5;
        QVector<MyMatrixEntry> matrixStorage(matrixSize * matrixSize);

        MyMatrixEntry MyMatrix::getEntry(int row, int col)
        {
        int offset = row * matrixSize + col;
        return matrixStorage[offset];
        }
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on 2 Feb 2011, 13:10 last edited by
          #5

          Thanks for the solutions,

          I can't use a structure because I also need to to put it in a QBuffer with the << operator :

          @
          QBuffer buffer;
          buffer.open(QBuffer::ReadWrite);
          QDataStream out(&buffer);
          out << myMatrix;
          @

          Probably the best solution is:
          @
          QList<QList<QVariant> > myMatrix;
          @
          but as said Volker I can't be sure of the column number .

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on 2 Feb 2011, 13:21 last edited by
            #6

            It's easy to write your own operators for this:

            @
            QDataStream &operator<<(QDataStream &out, const MyMatrix &matrix)
            {
            out << "MyMatrix";
            out << 1; // version
            out << matrix.matrixData;
            return out;
            }

            QDataStream &operator>>(QDataStream &in, MyMatrix &matrix)
            {
            QString label;
            in >> label;
            if(label != "MyMatrix")
            return in;

            int version;
            in >> version;
            if(version != 1)
                return in; // or do some conversion stuff
            
            in >> matrix.matrixData;
            return in;
            

            }
            @

            EDIT: Fixed errors

            PS:
            Don't forget to allow accessing the data members by the operators in your matrix class:

            @
            class MyMatrix {
            friend QDataStream &operator<<(QDataStream &, const MyMatrix &);
            friend QDataStream &operator>>(QDataStream &, MyMatrix &);
            // rest of your class
            };
            @

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • ZlatomirZ Offline
              ZlatomirZ Offline
              Zlatomir
              wrote on 2 Feb 2011, 13:26 last edited by
              #7

              You can use struct/class, because you can overload the operator<< and operator>> and then use that to serialize your struct.

              If you think that your matrix will get big the second solution Volker gave you is better than QList<QList<T> >
              LE: too late, Volker was faster ;)

              https://forum.qt.io/category/41/romanian

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on 2 Feb 2011, 13:29 last edited by
                #8

                This is interesting...
                So I can implement a MyMatrix class store the data in some way (list, struct ecc..) and implement in it my operators ( <<, >> ) .
                Very useful, Thanks.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  goetz
                  wrote on 2 Feb 2011, 13:33 last edited by
                  #9

                  [quote author="Luca" date="1296653343"]This is interesting...
                  So I can implement a MyMatrix class store the data in some way (list, struct ecc..) and implement in it my operators ( <<, >> ) .
                  Very useful, Thanks.[/quote]

                  That's standard C++ basics. See the "C++ FAQ on Operator Overloading":http://www.parashift.com/c++-faq-lite/operator-overloading.html. You can even create your own + - * and / operators on the matrix.

                  I would really suggest to go with your own class, as you can enforce the users to store only 5x5 data. With the lists users can store any arbitrary two dimensional data. There are neither constraints on the number of rows nor the number of columns. Worse, each row can have a different number of columns.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    luca
                    wrote on 2 Feb 2011, 13:38 last edited by
                    #10

                    [quote author="Volker" date="1296653620"][quote author="Luca" date="1296653343"]This is interesting...
                    So I can implement a MyMatrix class store the data in some way (list, struct ecc..) and implement in it my operators ( <<, >> ) .
                    Very useful, Thanks.[/quote]

                    That's standard C++ basics. See the "C++ FAQ on Operator Overloading":http://www.parashift.com/c++-faq-lite/operator-overloading.html. You can even create your own + - * and / operators on the matrix.

                    I would really suggest to go with your own class, as you can enforce the users to store only 5x5 data. With the lists users can store any arbitrary two dimensional data. There are neither constraints on the number of rows nor the number of columns. Worse, each row can have a different number of columns.[/quote]

                    Thanks, I know about operator overloading but I never used it. It's time to begin.

                    I'll try with my own class.

                    Thanks again.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kowtham
                      wrote on 23 Feb 2011, 15:27 last edited by
                      #11

                      Hi Volker...Thanks a lot. I had implemented the same logic but never kept << & >> operator overloading function as friend while declaring it. It works now as expected.

                      Thanks again :).

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on 23 Feb 2011, 15:41 last edited by
                        #12

                        I just implemented a set of template classes to implement different types of matrices. Works well for me. It also depends a lot on your use case what approach for a matrix works best. Is your matrix symetric? It is sparse or dense? You need to consider that kind of questions if you build one. Just blindly recommending

                        @QList<QList<QVariant> > myMatrix@

                        isn't a good idea IMHO.

                        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