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:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGraphicsRectItem>
#include <QMainWindow>
#include <QGraphicsScene>
namespace Ui{
class MainWindow;
}
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QGraphicsRectItem>
#include <QMainWindow>
#include <QGraphicsScene>
namespace Ui{
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsRectItem*** objRect; //This would be my 2D array
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
This is how I instance it:
for(int i = 0; i < x; i++){
}
objRect = new QGraphicsRectItem**[x];
for(int i = 0; i < x; i++){
objRect[i] = new QGraphicsRectItem*[x];
}
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:
objRect[0][0]->setRect(0,0,100,100);
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.
Bookmarks