Results 1 to 3 of 3

Thread: How do I create a 2 dimensional array of QGraphicsRectItems?

  1. #1
    Join Date
    Aug 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How do I create a 2 dimensional array of QGraphicsRectItems?

    I need to display a dynamic amount of QGraphicsRectItems in a QGraphicsView in rows and columns, so I decided to create a 2D array, but I always get an error. This is my header:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QGraphicsRectItem>
    5. #include <QMainWindow>
    6. #include <QGraphicsScene>
    7.  
    8. namespace Ui{
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow{
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private:
    20. Ui::MainWindow *ui;
    21. QGraphicsRectItem*** objRect; //This would be my 2D array
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 


    This is how I instance it:

    Qt Code:
    1. objRect = new QGraphicsRectItem**[x];
    2. for(int i = 0; i < x; i++){
    3. objRect[i] = new QGraphicsRectItem*[x];
    4. }
    To copy to clipboard, switch view to plain text mode 


    The code above runs perfectly with no errors. But whenever I try to do something with it, like establishing the size of one of the rectangles, it throws me an error message:

    Qt Code:
    1. objRect[0][0]->setRect(0,0,100,100);
    To copy to clipboard, switch view to plain text mode 


    If I run the program normally, no error message appears, but the window doesn't even appear. I can only see the error message if I run the project in Debug mode.

    This is the error message:

    2017-08-11.jpg

    "The inferior stopped because it received a message from the operating system"

    Signal name: SIGSEGV

    Signal meaning: Segmentation fault

    I've tried a lot of different ways of instancing it, but nothing seems to work.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How do I create a 2 dimensional array of QGraphicsRectItems?

    Hi, think it would be easier to use a standard container like QVector or QList instead of creating the array by yourself.

    Ginsengelf

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How do I create a 2 dimensional array of QGraphicsRectItems?

    So far good, but where are you allocating the object, that is only thing missing.

    Here is a generic example
    Qt Code:
    1. template <typename T>
    2. T *** New2DArray(int rows, int cols)
    3. {
    4. T *** array = new T**[rows];
    5.  
    6. for(int r = 0; r < rows; ++r)
    7. {
    8. array[r] = new T*[cols];
    9.  
    10. for(int c = 0; c < cols; ++c)
    11. array[r][c] = new T;
    12. }
    13.  
    14. return array;
    15. }
    16.  
    17. template <typename T>
    18. void Delete2DArray(T *** array, int rows, int cols)
    19. {
    20. for(int r = 0; r < rows; ++r)
    21. {
    22. for(int c = 0; c < cols; ++c)
    23. delete array[r][c];
    24.  
    25. delete[] array[r];
    26. }
    27.  
    28. delete[] array;
    29. }
    30.  
    31. ...
    32. QGraphicsRectItem *** objRect = New2DArray<QGraphicsRectItem>(rows, cols);
    33. ...
    34. Delete2DArray(array, rows, cols);
    35. ...
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. 2 Dimensional array in QML
    By nestuser in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2012, 08:38
  2. Operations in 2 dimensional array
    By Stanfillirenfro in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2012, 17:50
  3. Replies: 2
    Last Post: 19th December 2011, 00:34
  4. QwtPolar: Plot using a 2-dimensional array
    By hgstoehr in forum Newbie
    Replies: 0
    Last Post: 14th December 2011, 22:39
  5. use QVector as 2 dimensional Array
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2010, 13:31

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.