Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsView Draw line after point selection
Forum Updated to NodeBB v4.3 + New Features

QGraphicsView Draw line after point selection

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 5.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinaykutsa
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        joonhwan
        wrote on last edited by
        #3

        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(...) ?

        joonhwan at gmail dot com

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved