Results 1 to 6 of 6

Thread: Defining a two dimensional QVector class (Q2DDoubleVector)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Location
    Delft, Netherlands
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Defining a two dimensional QVector class (Q2DDoubleVector)

    Hello,

    I am trying to build a class, that can hold a resizable two
    dimensional array of double values.

    I need this, to hold a matrix of 1e6 rows and 5 or more colums of real-time data.
    When the data aquisition is running I want to avoid to allocate memory, because that can be slow.

    But I want to be able to change the exact number of rows and columns in a configuration file (and
    in a menu option, when the aquisition is not running), and therefore I need a dynamic array.

    The following code crashes:

    Qt Code:
    1. class Q2DDoubleVector : public QVector< QVector<double> > // Overloading Class QVector to allow for 2 dimensional vectors.
    2. {
    3. public:
    4. // constructor
    5. Q2DDoubleVector() : QVector< QVector<double> >(){}
    6. // methods
    7. // matrix resize
    8. void mresize(int rows, int columns);
    9. // destructor
    10. virtual ~Q2DDoubleVector() {}
    11. };
    12.  
    13. void Q2DDoubleVector::mresize(int rows, int columns)
    14. {
    15. this->resize(rows);
    16. for(int r=0; r<rows; r++) {
    17. this[r].resize(columns);
    18. }
    19. }
    20.  
    21. Q2DDoubleVector matrix;
    22. matrix.mresize(ROWS, COLS);
    To copy to clipboard, switch view to plain text mode 

    If I do it in a very similar way, it doesn't crash:

    Qt Code:
    1. QVector<QVector <double> > matrix;
    2.  
    3. matrix.resize(ROWS);
    4. for(int r=0; r < ROWS; r++) {
    5. matrix[r].resize(COLS);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Why does the first approach not work?

    I don't want to implement the resize loop for each 2D matrix, that I use again.

    You can download both projects (the example that works (ArrayTest) and
    the example that doesn't work (ArrayTest2) from the following URL:
    http://db.tt/IUWpBCnY

    Any hints welcome.

    Actually, I was using Delphi in the past, and with Delphi this was very easy:
    Qt Code:
    1. var Matrix: array of array of Double;
    2. begin
    3. SetLength(Matrix, 10, 20)
    4. end;
    To copy to clipboard, switch view to plain text mode 

    Best regards:

    Uwe Fechner

    I am using Ubuntu 11.04 32 bit and QT 4.7.2 .
    Last edited by ufechner; 25th September 2011 at 23:02.

Similar Threads

  1. Replies: 5
    Last Post: 2nd September 2011, 23:11
  2. Replies: 1
    Last Post: 5th February 2011, 03:04
  3. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 12:31
  4. How to implement one-dimensional zoom
    By d_stranz in forum Qwt
    Replies: 0
    Last Post: 7th November 2008, 17:46
  5. Multi Dimensional QList
    By aamer4yu in forum Qt Programming
    Replies: 5
    Last Post: 2nd April 2007, 06:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.