QGraphicsView Draw line after point selection
vinaykutsa last edited by
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
explicit MainWindow(QWidget *parent = 0);
void DrawLine(int x1, int y1, int x2, int y2);
//This method to be used to draw the line after selecting the points.
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
void mouseDoubleClickEvent(QMouseEvent *event);
void PointSelected(int a, int b, int c, int d);
int x1, y1, x2, y2;
mouseDoubleClickEvent is as below
static int pCount = 0;
if(event->type() == QMouseEvent::MouseButtonDblClick)
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.
painter.drawLine(x1, y1, x2, y2);
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.
Asperamanca last edited by
The pixmapItem and the pixmap are two different things. Changing the pixmap will not cause the pixmapItem to update, it will retain it's own copy of the pixmap.
While it's not really elegant, calling setPixmap again after changing the pixmap might at least make it work.
joonhwan last edited by
If your 'view' is centeral widget of QMainWidget, you will not see any line which is drawed in QMainWidget::paintEvent() override function. Instead, how about just doing it in your QGraphicsView derived GraphicsView::paintEvent() or just scene->addLine(...) ?