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 Mouse click event help
Forum Update on Monday, May 27th 2025

QGraphicsView Mouse click event help

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.5k Views
  • 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.
  • M Offline
    M Offline
    Marsh
    wrote on 29 Apr 2020, 13:54 last edited by Marsh
    #1

    Hi,

    I am new to QT and looking for someone to help.

    I have a program where I need to draw a shape on top of an image using a mouse click/drag/release. Currently this is the closest piece of code I have to drawing the shape onto it however it is in QGraphicsView and I am unsure on how to detect a mouse click in that view.

    void edit::mousePressEvent(QMouseEvent *event){
        QPointF mousePoint = ui->graphicsView->mapToScene(event->pos());
        qreal x;
        qreal y;
        mousePoint.setX(x);
        mousePoint.setY(y);
    
        QPen pen(Qt::black);
        pen.setWidth(3);
        QGraphicsRectItem *rectangle;
        rectangle = scene->addRect(x,y,200,50,pen);//x,y,width,height
        rectangle->setFlag(QGraphicsItem::ItemIsMovable);
    }
    

    I have been reading a lot online but there seems to be so many ways to do it and I'm finding it so overwhelming. I have been coding primarily in a QDialog class and I am pretty sure from what I have seen online I need to make a separate class with QGraphicsItem but I am unsure how I would link/setup these classes together once I have done it. can anyone provide me with some sort of explanation on how to do this and what the process? I would greatly appreciate it.

    I would greatly appreciate any help with this :)

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 29 Apr 2020, 14:02 last edited by
      #2

      Hi
      Normally you would have
      mousePressEvent -> starting the shape. (we store the start point)
      mouseMoveEvent -> we drag to define size. we update current pos and redraw.
      mouseReleaseEvent -> we release mouse and now make the rect as we have end point.

      like here
      http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php
      Here they use the scene, though. not the view. Should work the same.

      M 1 Reply Last reply 29 Apr 2020, 14:04
      2
      • M mrjj
        29 Apr 2020, 14:02

        Hi
        Normally you would have
        mousePressEvent -> starting the shape. (we store the start point)
        mouseMoveEvent -> we drag to define size. we update current pos and redraw.
        mouseReleaseEvent -> we release mouse and now make the rect as we have end point.

        like here
        http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php
        Here they use the scene, though. not the view. Should work the same.

        M Offline
        M Offline
        Marsh
        wrote on 29 Apr 2020, 14:04 last edited by
        #3

        @mrjj Ok thank you for replying so quickly, that link looks very helpful, I'll give it a try :)

        M P 2 Replies Last reply 29 Apr 2020, 14:08
        0
        • M Marsh
          29 Apr 2020, 14:04

          @mrjj Ok thank you for replying so quickly, that link looks very helpful, I'll give it a try :)

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 29 Apr 2020, 14:08 last edited by
          #4

          @Marsh
          well you are on right track
          but the QPointF mousePoint = ui->graphicsView->mapToScene(event->pos());
          should be a member of the view (QPointF mousePoint; .h, in class) . so we can use the value in
          mouseRelease to define the rect.
          Or if you just using plain View, then add to the class that has the view.
          anyway, the code in example is mostly finished, you just want rect and not line :)

          1 Reply Last reply
          1
          • M Marsh
            29 Apr 2020, 14:04

            @mrjj Ok thank you for replying so quickly, that link looks very helpful, I'll give it a try :)

            P Online
            P Online
            Pl45m4
            wrote on 29 Apr 2020, 15:47 last edited by
            #5

            @Marsh

            You also might want to check out the Scribble Example.
            QWidget is used there for drawing, but the mouseEvents are the same. You can use them for your QGraphicsView or get at least some inspiration :)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            M 1 Reply Last reply 29 Apr 2020, 18:28
            3
            • P Pl45m4
              29 Apr 2020, 15:47

              @Marsh

              You also might want to check out the Scribble Example.
              QWidget is used there for drawing, but the mouseEvents are the same. You can use them for your QGraphicsView or get at least some inspiration :)

              M Offline
              M Offline
              Marsh
              wrote on 29 Apr 2020, 18:28 last edited by
              #6

              @Pl45m4 Thank you this resource looks promising!

              1 Reply Last reply
              0

              1/6

              29 Apr 2020, 13:54

              • Login

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