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. [SOLVED]mousePress event QGraphicsView
QtWS25 Last Chance

[SOLVED]mousePress event QGraphicsView

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 5.0k 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.
  • BlackMambaB Offline
    BlackMambaB Offline
    BlackMamba
    wrote on last edited by
    #1

    Hello,

    I have a subclassed QGraphicsView and added mousePressEvent. When clicked on the view, it creates a QGraphicsItem at the same location (via QGraphicsScene). This QGraphicsItem has also a mousePressEvent.

    I want that when you click on the item created, it doesnt activate the event on the scene. (otherwise it creates a second item at the click location).

    How could I achieve that?

    Cheers,

    1 Reply Last reply
    0
    • BlackMambaB Offline
      BlackMambaB Offline
      BlackMamba
      wrote on last edited by
      #2

      Still no idea? Raven is not around? :)

      Cheers,

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        patience is the key ;)
        your thread just has 25 views so far.

        Did i understand you correct that you re-implemented the mousePressEvent() handler of the QGraphicsView subclass? Meaning QGraphicsView::mousePressEvent()?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          i assume you did... so try the following:

          QGraphicsScene is responsible for forwarding the events to the items.
          So you should subclass the scene and re-implement the mousePressEvent() like this:
          @
          void MyGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent )
          {
          QGraphicsScene::mousePressEvent ( mouseEvent );
          if( mouseEvent->isAccepted() )
          return;

           //if the event has not been processed by an item create a new item here
          

          }
          @

          This only works if you have accepted the mouseEvent in your item!

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • BlackMambaB Offline
            BlackMambaB Offline
            BlackMamba
            wrote on last edited by
            #5

            Hello raven :)

            Thanks for your answer !

            [quote author="raven-worx" date="1381411476"]patience is the key ;) your thread just has 25 views so far. Did i understand you correct that you re-implemented the mousePressEvent() handler of the QGraphicsView subclass? Meaning QGraphicsView::mousePressEvent()?[/quote]

            yes I subclassed like that.

            I will try to subclass the scene as you mentionned it. My question is : is that enough to subclass the scene by adding mousePressEvent without subclassing the view or do I need both? (subclass scene and view)

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              only the scene.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • BlackMambaB Offline
                BlackMambaB Offline
                BlackMamba
                wrote on last edited by
                #7

                Dear raven, I tried :

                @
                void XCustomGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * event )
                {
                QGraphicsScene::mousePressEvent ( event );
                if( event->isAccepted() )
                {
                return;
                }
                else{
                emit signalCreateRectangle(event->pos());
                qDebug() << event->pos();
                qDebug() << this->width();
                qDebug() << this->height();
                update();
                QGraphicsScene::mousePressEvent(event);
                }
                //if the event has not been processed by an item create a new item here
                }
                @

                and the qDebug() << event->pos(); only displays QPointF(0,0), wherever I click ...

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  use event->scenePos()

                  and also don't call the base class implementation a second time.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • BlackMambaB Offline
                    BlackMambaB Offline
                    BlackMamba
                    wrote on last edited by
                    #9

                    Dear raven,

                    it works if I add :

                    @QGraphicsScene::mousePressEvent ( event );@

                    at the end and not at the beginning. However, when moving the item around, it creates a second rectangle when I click to select it ...
                    It only works if I dont move the item ...

                    Any idea? :)

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      [quote author="BlackMamba" date="1381426485"]
                      it works if I add :

                      @QGraphicsScene::mousePressEvent ( event );@

                      at the end and not at the beginning.
                      [/quote]
                      This doesnt make sense.

                      [quote author="BlackMamba" date="1381426485"]
                      However, when moving the item around, it creates a second rectangle when I click to select it ...
                      [/quote]
                      I guess that is...when you create a rect item every time you receive a mouse press event?

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • BlackMambaB Offline
                        BlackMambaB Offline
                        BlackMamba
                        wrote on last edited by
                        #11

                        It works ! Just needed to add update somewhere else that it draws properly.
                        Indeed, QGraphicsScene::mousePressEvent ( event ); should be at the beginning.

                        Thank you so much raven you help me a lot :)

                        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