PDA

View Full Version : Program Crashes while creating a LineItem dinamically



21altf
29th October 2011, 22:51
Guys, I need somehelp.

I want to create a line kinda like it works in MS Paint. It's suposse to show it moving around b4 fixing its final size.

anywyas. Here is the code, hope you guys can catch something I'm not.

The slot FinalPosition works just fine. It grabs mouseMoveEvent from QGraphicsView and replaces it. When I Trigger New Life in my MainWindow the program chrases. I'm sure the while function is helpin this crash happens, but I simply cant think of anyway to implement this.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsLineItem>
#include <QEvent>
#include <QDropEvent>
#include <QSignalMapper>
#include <QMouseEvent>
#include <QtDebug>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);


view = new QGV;
MainWindow::setCentralWidget(view);
view->setMouseTracking(true);
QObject::connect(ui->action_Line,SIGNAL(triggered()),this,SLOT(criarLin ha()));
QObject::connect(view,SIGNAL(changeFinalPosition(q real,qreal)),this,SLOT(FinalPosition(qreal,qreal)) );



i = 0;
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;

scene = new QGraphicsScene;
//scene->installEventFilter(view);
view->setScene(scene);




}

MainWindow::~MainWindow()
{
delete ui;
}

// Pulbic Functions
void MainWindow::createLine()
{
int chave = 1;
QGraphicsLineItem *linha = new QGraphicsLineItem(x1,y1,x2,y2);
scene->addItem(linha);

qDebug() << "Entrei" << i;

while((chave == 1))
{
if(i == 1)
{
linha->setLine(x1,y1,x2,y2);
}
else if (i == 2)
{
linha->setLine(x1,y1,x2,y2);
chave = false;
i=0;
}
}

qDebug() << "saÃ*";

}

void MainWindow::mousePressEvent(QMouseEvent *qme1)
{
QMouseEvent *event = static_cast<QMouseEvent *>(qme1);


if((event->button() == Qt::LeftButton))
{
if(i == 0)
{
x1 = dynamic_cast<QMouseEvent*>(event)->x();
y1 = dynamic_cast<QMouseEvent*>(event)->y();
i=1;

}
else if (i == 1)
{
x2 = dynamic_cast<QMouseEvent*>(event)->x();
y2 = dynamic_cast<QMouseEvent*>(event)->y();
i=2;
}

}
}




//Public Slots
void MainWindow::criarLinha()
{
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
i = 0;
qDebug() << "pronto pra começar:" << x1 << y1 << x2 << y2;
createLine();
}

void MainWindow::FinalPosition(qreal x, qreal y)
{
if (!(x2 == x))
x2 = x;
else if (!(y2 == y))
y2 = y;

}

Santosh Reddy
30th October 2011, 02:24
insufficient information.

which line is it crashing?
post the stack dump?

21altf
30th October 2011, 10:52
It doesnt specify a line. The program just collapses when I execute it.

norobro
30th October 2011, 17:48
It doesnt specify a line. The program just collapses when I execute it.You need to run it in the debugger.

You are drawing on the scene so why not handle the mouse events in the view? Something like this:
#include <QtGui>

class GraphicsView : public QGraphicsView
{
Q_OBJECT
public:
GraphicsView(): drawFlag(false), start(0,0), item(0){}
void mousePressEvent(QMouseEvent *event){
if(event->button()!= Qt::LeftButton) return;
drawFlag=true;
start.setX(mapToScene(event->pos()).x());
start.setY(mapToScene(event->pos()).y());
}
void mouseMoveEvent(QMouseEvent *event){
if(!drawFlag) return;
if(item) scene()->removeItem(item);
item =scene()->addLine(start.x(),start.y(),mapToScene(event->pos()).x(),mapToScene(event->pos()).y(),QPen(Qt::red));
}
void mouseReleaseEvent(QMouseEvent *event){
if(drawFlag) {
drawFlag=false;
scene()->addLine(start.x(),start.y(),mapToScene(event->pos()).x(),mapToScene(event->pos()).y(),QPen(Qt::red));
}
}
private:
bool drawFlag;
QPointF start;
QGraphicsLineItem *item;
};

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
scene.setSceneRect(0,0,view.size().width(),view.si ze().height());
view.show();
return a.exec();
}
#include "main.moc"

21altf
31st October 2011, 12:01
Thanks!

It works now ;D!