Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?

How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?

Scheduled Pinned Locked Moved Solved Qt for Python
17 Posts 3 Posters 3.1k 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.
  • D Offline
    D Offline
    darrenleeleelee1
    wrote on last edited by darrenleeleelee1
    #1

    I want to create those thing in QGraphicsSence .(Something like this)
    3c5b8150-ce50-4386-bd25-cbb5abac2128-image.png
    I don't want to use QPainter to do that because I need them to be clickable, movable.(I already know how to make QGraphicsItem to be movable).
    But I don't know how to do with QGraphicsItem to be created by mouse.

    JonBJ 1 Reply Last reply
    0
    • D darrenleeleelee1

      @JonB And I also wondering I should override mousePressedEvent in QGraphicsScence or QGraphicsView, both of them have mousePressedEvent but I don't know which to override.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #7

      @darrenleeleelee1
      If you want to create those "freehand" letter drawings as a GraphicsItem you would have to add it as a QGraphicsPathItem composed from a multi-item QPainterPath. Unless you are prepared to do it as a QGraphicsPixmapItem, and lose the ability to manipulate the points.

      It probably doesn't matter which of QGraphicsScence or QGraphicsView to catch the mouse event, so long as it works. I did refer you to the DiagramScene example which seems to use the scene's mouse event.

      D 1 Reply Last reply
      0
      • D darrenleeleelee1

        I want to create those thing in QGraphicsSence .(Something like this)
        3c5b8150-ce50-4386-bd25-cbb5abac2128-image.png
        I don't want to use QPainter to do that because I need them to be clickable, movable.(I already know how to make QGraphicsItem to be movable).
        But I don't know how to do with QGraphicsItem to be created by mouse.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsSence by mouse?:

        But I don't know how to do with QGraphicsItem to be created by mouse.

        In some shape or form, you respond to mouse events by creating QGraphicsItem/Objects and adding then to the scene.

        However, if you want the user to be able to produce fixed shapes like you show, it seems to me you are going to want a "toolbar" where he can pick the desired shape. Unless you really mean freehand drawing, but then they won't be regular shapes. Maybe Diagram Scene Example is useful for the approach.

        D 1 Reply Last reply
        1
        • JonBJ JonB

          @darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsSence by mouse?:

          But I don't know how to do with QGraphicsItem to be created by mouse.

          In some shape or form, you respond to mouse events by creating QGraphicsItem/Objects and adding then to the scene.

          However, if you want the user to be able to produce fixed shapes like you show, it seems to me you are going to want a "toolbar" where he can pick the desired shape. Unless you really mean freehand drawing, but then they won't be regular shapes. Maybe Diagram Scene Example is useful for the approach.

          D Offline
          D Offline
          darrenleeleelee1
          wrote on last edited by darrenleeleelee1
          #3

          @JonB Yes, I will use button to do the tool bar.
          But I not sure how to implemet.
          Sorry I not sure what is your In some shape or form, you respond to mouse events by creating QGraphicsItem/Objects and adding then to the scene.mean. You mean I can create QGraphicsItem by mouse?

          JonBJ 1 Reply Last reply
          0
          • D darrenleeleelee1

            @JonB Yes, I will use button to do the tool bar.
            But I not sure how to implemet.
            Sorry I not sure what is your In some shape or form, you respond to mouse events by creating QGraphicsItem/Objects and adding then to the scene.mean. You mean I can create QGraphicsItem by mouse?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @darrenleeleelee1
            Yes. It's not that Qt does that for you, it's that you write the code. You know how to act on a mouse down or click or whatever. You know how to create a new QGraphicsItem and add it to the scene. So, for example, when the mouse is downed or clicked you could get its position, create a graphics item, and set its position to the mouse coordinates.

            In that example I referred you to, for instance, there is:

            The mousePressEvent() function handles mouse press event's different depending on which mode the DiagramScene is in. We examine its implementation for each mode:

              void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
              {
                  if (mouseEvent->button() != Qt::LeftButton)
                      return;
            
                  DiagramItem *item;
                  switch (myMode) {
                      case InsertItem:
                          item = new DiagramItem(myItemType, myItemMenu);
                          item->setBrush(myItemColor);
                          addItem(item);
                          item->setPos(mouseEvent->scenePos());
                          emit itemInserted(item);
                          break;
            

            We simply create a new DiagramItem and add it to the scene at the position the mouse was pressed. Note that the origin of its local coordinate system will be under the mouse pointer position.

            D 2 Replies Last reply
            2
            • JonBJ JonB

              @darrenleeleelee1
              Yes. It's not that Qt does that for you, it's that you write the code. You know how to act on a mouse down or click or whatever. You know how to create a new QGraphicsItem and add it to the scene. So, for example, when the mouse is downed or clicked you could get its position, create a graphics item, and set its position to the mouse coordinates.

              In that example I referred you to, for instance, there is:

              The mousePressEvent() function handles mouse press event's different depending on which mode the DiagramScene is in. We examine its implementation for each mode:

                void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
                {
                    if (mouseEvent->button() != Qt::LeftButton)
                        return;
              
                    DiagramItem *item;
                    switch (myMode) {
                        case InsertItem:
                            item = new DiagramItem(myItemType, myItemMenu);
                            item->setBrush(myItemColor);
                            addItem(item);
                            item->setPos(mouseEvent->scenePos());
                            emit itemInserted(item);
                            break;
              

              We simply create a new DiagramItem and add it to the scene at the position the mouse was pressed. Note that the origin of its local coordinate system will be under the mouse pointer position.

              D Offline
              D Offline
              darrenleeleelee1
              wrote on last edited by darrenleeleelee1
              #5

              @JonB thanks, I will try it.
              I also wondering could I create somthing like this.(pen)
              d163bfde-9ab1-4f22-a2c3-da2f3b983aab-image.png
              I know how to do in QPainter, But I want create as QGraphicsItem.

              1 Reply Last reply
              0
              • JonBJ JonB

                @darrenleeleelee1
                Yes. It's not that Qt does that for you, it's that you write the code. You know how to act on a mouse down or click or whatever. You know how to create a new QGraphicsItem and add it to the scene. So, for example, when the mouse is downed or clicked you could get its position, create a graphics item, and set its position to the mouse coordinates.

                In that example I referred you to, for instance, there is:

                The mousePressEvent() function handles mouse press event's different depending on which mode the DiagramScene is in. We examine its implementation for each mode:

                  void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
                  {
                      if (mouseEvent->button() != Qt::LeftButton)
                          return;
                
                      DiagramItem *item;
                      switch (myMode) {
                          case InsertItem:
                              item = new DiagramItem(myItemType, myItemMenu);
                              item->setBrush(myItemColor);
                              addItem(item);
                              item->setPos(mouseEvent->scenePos());
                              emit itemInserted(item);
                              break;
                

                We simply create a new DiagramItem and add it to the scene at the position the mouse was pressed. Note that the origin of its local coordinate system will be under the mouse pointer position.

                D Offline
                D Offline
                darrenleeleelee1
                wrote on last edited by
                #6

                @JonB And I also wondering I should override mousePressedEvent in QGraphicsScence or QGraphicsView, both of them have mousePressedEvent but I don't know which to override.

                JonBJ 1 Reply Last reply
                0
                • D darrenleeleelee1

                  @JonB And I also wondering I should override mousePressedEvent in QGraphicsScence or QGraphicsView, both of them have mousePressedEvent but I don't know which to override.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @darrenleeleelee1
                  If you want to create those "freehand" letter drawings as a GraphicsItem you would have to add it as a QGraphicsPathItem composed from a multi-item QPainterPath. Unless you are prepared to do it as a QGraphicsPixmapItem, and lose the ability to manipulate the points.

                  It probably doesn't matter which of QGraphicsScence or QGraphicsView to catch the mouse event, so long as it works. I did refer you to the DiagramScene example which seems to use the scene's mouse event.

                  D 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @darrenleeleelee1
                    If you want to create those "freehand" letter drawings as a GraphicsItem you would have to add it as a QGraphicsPathItem composed from a multi-item QPainterPath. Unless you are prepared to do it as a QGraphicsPixmapItem, and lose the ability to manipulate the points.

                    It probably doesn't matter which of QGraphicsScence or QGraphicsView to catch the mouse event, so long as it works. I did refer you to the DiagramScene example which seems to use the scene's mouse event.

                    D Offline
                    D Offline
                    darrenleeleelee1
                    wrote on last edited by
                    #8

                    @JonB Sorry, maybe I was misled you, I want a pen that I can draw anything I want, kind of doodle?Just one stroke when the mouse released the stroke become an object.
                    9b58f080-e4d8-4044-b1ee-997b79512eb3-image.png
                    kind of this.

                    JonBJ 1 Reply Last reply
                    0
                    • D darrenleeleelee1

                      @JonB Sorry, maybe I was misled you, I want a pen that I can draw anything I want, kind of doodle?Just one stroke when the mouse released the stroke become an object.
                      9b58f080-e4d8-4044-b1ee-997b79512eb3-image.png
                      kind of this.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #9

                      @darrenleeleelee1
                      You might get some clues from https://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html in combination with https://forum.qt.io/topic/94586/draw-line-on-mouse-click & https://forum.qt.io/topic/94622/need-help-with-qgraphicsitem-painter .

                      D 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @darrenleeleelee1
                        You might get some clues from https://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html in combination with https://forum.qt.io/topic/94586/draw-line-on-mouse-click & https://forum.qt.io/topic/94622/need-help-with-qgraphicsitem-painter .

                        D Offline
                        D Offline
                        darrenleeleelee1
                        wrote on last edited by
                        #10

                        @JonB ok, sorry I still have some question.
                        b1171391-02f1-4ada-8f59-7ccd1d7873f4-image.png
                        78314a77-dc71-4146-b4a4-21c256d7be11-image.png
                        How can I do like I pressed mouse and hold, to draw a line until I released mouse, the line will become an object.

                        JonBJ 1 Reply Last reply
                        0
                        • D darrenleeleelee1

                          @JonB ok, sorry I still have some question.
                          b1171391-02f1-4ada-8f59-7ccd1d7873f4-image.png
                          78314a77-dc71-4146-b4a4-21c256d7be11-image.png
                          How can I do like I pressed mouse and hold, to draw a line until I released mouse, the line will become an object.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #11

                          @darrenleeleelee1
                          I don't know what to say, it's just code you need to write. Note where the mouse goes down, note where the mouse is released and at that stage create a QGraphicsLineItem or whatever between those two points.

                          D 1 Reply Last reply
                          2
                          • JonBJ JonB

                            @darrenleeleelee1
                            I don't know what to say, it's just code you need to write. Note where the mouse goes down, note where the mouse is released and at that stage create a QGraphicsLineItem or whatever between those two points.

                            D Offline
                            D Offline
                            darrenleeleelee1
                            wrote on last edited by
                            #12

                            @JonB But if I create the QGraphicsLineItem at mousedReleasd, when I holding the mouse pressed, I can't see the line.

                            jsulmJ 1 Reply Last reply
                            0
                            • D darrenleeleelee1

                              @JonB But if I create the QGraphicsLineItem at mousedReleasd, when I holding the mouse pressed, I can't see the line.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?:

                              But if I create the QGraphicsLineItem at mousedReleasd, when I holding the mouse pressed

                              How can you get mouse released event if you hold mouse button pressed? Can you please explain better?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              D 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?:

                                But if I create the QGraphicsLineItem at mousedReleasd, when I holding the mouse pressed

                                How can you get mouse released event if you hold mouse button pressed? Can you please explain better?

                                D Offline
                                D Offline
                                darrenleeleelee1
                                wrote on last edited by
                                #14

                                @jsulm I want to override mouseMoveEvent to get my mouse position when my mouse is holding.

                                1 Reply Last reply
                                0
                                • D Offline
                                  D Offline
                                  darrenleeleelee1
                                  wrote on last edited by darrenleeleelee1
                                  #15

                                  @jsulm I am thinking if I can change the existing QGraphicsLineItem to keep update to where my mouse position, and the start position is the moused click position.But I don't know how to adjust existing QGraphicsLineItem.

                                  JonBJ 1 Reply Last reply
                                  0
                                  • D darrenleeleelee1

                                    @jsulm I am thinking if I can change the existing QGraphicsLineItem to keep update to where my mouse position, and the start position is the moused click position.But I don't know how to adjust existing QGraphicsLineItem.

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #16

                                    @darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?:

                                    But I don't know how to adjust existing QGraphicsLineItem.

                                    Again, I don't know what to say. You have QGraphicsLineItem::setLine(). I don't see the problems you see.

                                    D 1 Reply Last reply
                                    1
                                    • JonBJ JonB

                                      @darrenleeleelee1 said in How to Create QGraphicsItem(Lines, Rectangles, etc.) in QGraphicsScence by mouse?:

                                      But I don't know how to adjust existing QGraphicsLineItem.

                                      Again, I don't know what to say. You have QGraphicsLineItem::setLine(). I don't see the problems you see.

                                      D Offline
                                      D Offline
                                      darrenleeleelee1
                                      wrote on last edited by
                                      #17

                                      @JonB You are right, sorry I am thinking too complicated

                                      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