Hi everyone!
I have some problem with QGraphicsView. I want to create a program that will allow me to draw rectangles on loaded image. I have created my own GraphicsView class that inherits from QGraphicsView and I have overloaded QGraphicsView::mouseReleaseEvent to get the mouse position. I also added 'Widget' from Qt Creator to my form and replaced it with my class. It works fine until I close the window. After that I get an error code (-1073741819). Debugger shows segmentation fault in function QScopedPointer (no idea why). Probably I have to call some function, but I don't know which one and where. I will show you my code (only .cpp). Tell me if you need the header files.
I hope you understand what I mean Could you help me?
MainWindow class:
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow
::MainWindow(QWidget *parent
) : QWidget(parent
), ui
(new Ui
::MainWindow){
ui->setupUi(this);
ui->widget->setScene(&scene);
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->loadButton, SIGNAL(clicked()), this, SLOT(loadImage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadImage()
{
image.load(fileName);
scene.addPixmap(image);
ui->widget->loadImage(image);
ui->widget->show();
}
#include "MainWindow.h"
#include "ui_MainWindow.h"
MainWindow::MainWindow(QWidget *parent) : QWidget(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->widget->setScene(&scene);
connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
connect(ui->loadButton, SIGNAL(clicked()), this, SLOT(loadImage()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadImage()
{
QString fileName = QFileDialog::getOpenFileName();
image.load(fileName);
scene.addPixmap(image);
ui->widget->loadImage(image);
ui->widget->show();
}
To copy to clipboard, switch view to plain text mode
GraphicsView class:
#include "GraphicsView.h"
{
firstRect = true;
}
GraphicsView::~GraphicsView()
{
scene()->clear();
}
void GraphicsView
::loadImage(QPixmap item
) {
image = item;
}
void GraphicsView
::mouseReleaseEvent(QMouseEvent *event
) {
if(point1.isNull())
{
point1.setX(event->x());
point1.setY(event->y());
}
else
{
point2.setX(event->x());
point2.setY(event->y());
int width = point2.x() - point1.x();
int height = point2.y() - point1.y();
if(firstRect)
{
rect.setRect(point1.x(), point1.y(), width, height);
rect.
setPen(QPen(Qt
::red));
firstRect = false;
}
else
{
scene()->removeItem(&rect);
rect.setRect(point1.x(), point1.y(), width, height);
}
scene()->clear();
scene()->addPixmap(image);
scene()->addItem(&rect);
point1.setX(0);
point1.setY(0);
}
event->accept();
}
#include "GraphicsView.h"
GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
{
firstRect = true;
}
GraphicsView::~GraphicsView()
{
scene()->clear();
}
void GraphicsView::loadImage(QPixmap item)
{
image = item;
}
void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if(point1.isNull())
{
point1.setX(event->x());
point1.setY(event->y());
}
else
{
point2.setX(event->x());
point2.setY(event->y());
int width = point2.x() - point1.x();
int height = point2.y() - point1.y();
if(firstRect)
{
rect.setRect(point1.x(), point1.y(), width, height);
rect.setPen(QPen(Qt::red));
firstRect = false;
}
else
{
scene()->removeItem(&rect);
rect.setRect(point1.x(), point1.y(), width, height);
}
scene()->clear();
scene()->addPixmap(image);
scene()->addItem(&rect);
point1.setX(0);
point1.setY(0);
}
event->accept();
}
To copy to clipboard, switch view to plain text mode
Platform: Windows 7
Qt: 4.7.0
Thanks in advance
Kuba
Bookmarks