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;
}
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;
}