Guillermo-Gatoto
11th August 2017, 05:01
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;
}
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
This is how I instance it:
objRect = new QGraphicsRectItem**[x];
for(int i = 0; i < x; i++){
objRect[i] = new QGraphicsRectItem*[x];
}
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);
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:
12543
"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.
#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
This is how I instance it:
objRect = new QGraphicsRectItem**[x];
for(int i = 0; i < x; i++){
objRect[i] = new QGraphicsRectItem*[x];
}
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);
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:
12543
"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.