PDA

View Full Version : Draw Line: QGraphicsView



vinaykusta
3rd January 2012, 10:54
I'm trying to draw a line based on the points selected using the mouse double click event.
Mainwindow is as below:

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

public slots:
void DrawLine(int x1, int y1, int x2, int y2);

private slots:
//This method to be used to draw the line after selecting the points.
void on_pushButton_clicked();

private:
GraphicsView *view;
QGraphicsScene *scene;
QGraphicsPixmapItem *pixmapItem;
QPixmap *pixmap;
};

Image is displayed in the mainwindow:

view = new GraphicsView();
scene = new QGraphicsScene(0, 0, 800, 600);
pixmapItem = new QGraphicsPixmapItem();
pixmap = new QPixmap(800,600);
QImage img;
img.load("D:/NPOL_Test_Modules/Qt/4.7.1/QGraphicsViewMouseEvent/Winter.jpg"))
QPainter painter;
painter.begin(pixmap);
painter.drawImage(0,0,img);
painter.end();

pixmapItem->setPixmap(*pixmap);
scene->addItem(pixmapItem);
view->setScene(scene);

connect(view, SIGNAL(PointSelected(int,int,int,int)), this, SLOT(DrawLine(int,int,int,int)));

GraphicsView is implemented as below:


class GraphicsView: public QGraphicsView
{
Q_OBJECT

public:
explicit GraphicsView();

protected:
void mouseDoubleClickEvent(QMouseEvent *event);

signals:
void PointSelected(int a, int b, int c, int d);

private:
int x1, y1, x2, y2;
};

mouseDoubleClickEvent is as below

static int pCount = 0;
if(event->type() == QMouseEvent::MouseButtonDblClick)
{
event->accept();
pCount++;
if(pCount == 1)
{
x1 = QCursor::pos().x();
y1 = QCursor::pos().y();
}
else if(pCount == 2)
{
pCount = 0;
x2 = QCursor::pos().x();
y2 = QCursor::pos().y();
emit PointSelected(x1, y1, x2, y2);
}
}

I'm not able to drawline... I have tried using QPainter as below in the DrawLine function of Mainwindow.


QPainter painter;
painter.begin(pixmap);
painter.drawLine(x1, y1, x2, y2);
painter.end();

Please let me know if my approach is right.
My intension is to draw the line on the image once points are selected using the mouse event.
I'll be thankfull if any one can please guide on how I can achieve this.

wysota
3rd January 2012, 11:24
Your approach is wrong. If you are using Graphics View then use QGraphicsScene::addLine() to add a line to the scene. And don't use QCursor::pos(), instead use QGraphicsSceneMouseEvent::scenePos() you get in the event handler.

vinaykusta
3rd January 2012, 12:38
Thank you for the guidance.

I'm now able to draw the line.

I have a scenario where my image being displayed keep changing. A sequence of image.


// Set Up is as below
pixmap = new QPixmap(800, 600);
pixmap->fill(Qt::black);
pixmapItem = new QGraphicsPixmapItem();

scene = new QGraphicsScene(0,0,798,598);
scene->addItem(pixmapItem);

ui->liveFeedView = new QGraphicsView();
ui->liveFeedView->setScene(scene);

Furtherm, Image update is done periodically as below :



// Load image data in the buffer to a QImage object and udpate the pixmap area
// Set the QGraphicsView to display QGraphicsScene updated with an updated graphics item
if(rawImg.loadFromData(buf->imgData[i]))
{
QPainter painter;
painter.begin(pixmap);
painter.drawImage(225,85,rawImg);
painter.end();
pixmapItem->setPixmap(*pixmap);

ui->liveFeedView->setScene(scene);
}

As suggested, In case I use QGraphicsScene addline function... I think I might loose the line on every update.
In such case, what can be another alternative.
How can I achieve this line drawing and retain the line, even though the image keeps changing or refreshing.

Even in the above case of frequent update to image display, I would like to know if its the correct approach.
Thank you very much.

wysota
3rd January 2012, 16:26
If you update just the existing pixmap item, you will not loose anything.

vinaykusta
4th January 2012, 12:47
Thank you again.
Please guide on How to clear/erase the line ?

wysota
4th January 2012, 18:04
Please guide on How to clear/erase the line ?

The line is represented by the item you have added to the scene by calling QGraphicsScene::addLine(). If you delete the object, the line will go away. I suggest you read a bit about Graphics View before you start using it.