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. How Does A Graphics Item Know When The Mouse Is "Over" It?
Forum Updated to NodeBB v4.3 + New Features

How Does A Graphics Item Know When The Mouse Is "Over" It?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 5.9k 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.
  • T Offline
    T Offline
    texas
    wrote on last edited by
    #1

    As the documentation says in regards to setCursor(), "The mouse cursor will assume this shape when it's over this item".

    In my scene implementation I am debugging with something like:

    @
    void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent)
    {
    ...
    QPointF pt = mouseEvent->scenePos();
    QPointF mpt = my_item->mapFromScene(pt);
    QRectF bb = my_item->boundingRect();
    QPainterPath shape = my_item->shape();
    qDebug() << "pt=" << pt
    << "mpt=" << mpt
    << "bb contains=" << bb->contains(mpt)
    << "shape contains=" << shape.contains(mpt)
    << "myitem contains=" << my_item->contains(mpt)
    << "items=" << items(pt,Qt::IntersectsItemShape,
    Qt::AscendingOrder).size();

    QGraphicsScene::mouseMoveEvent( mouseEvent);
    

    }
    @

    When I move the mouse over my_item, I get:

    • bb contains mpt
    • my_item contains mpt
    • my_item's shape contains mpt

    However, for the size of items I get zero :(

    • No hover event on my item occurs
    • setCursor() doesn't work

    Other Hints:

    • I think it has to do with my_item->setParentItem(0) maybe.
    • Sometimes I do get the hover event on my_item... i just don't see any rhyme or reason to it
    1 Reply Last reply
    0
    • L Offline
      L Offline
      ludde
      wrote on last edited by
      #2

      Cannot see anything wrong in the code above (except that it should be bb.contains(mpt), but that's just a typo I assume). You might want to test what itemAt() returns as well, because that is probably the function used internally to set the cursor. (Or, you could have a look in the source code to see exactly how it determines which cursor to use.)
      Regarding hover events, I assume you have called setAcceptHoverEvents(true).

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        ZapB
        wrote on last edited by
        #3

        You need to call

        @
        QGraphicsItem::setAcceptsHoverEvents( true );
        @

        on your items. For performance reasons items do not accept hover events by default.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply
        0
        • T Offline
          T Offline
          texas
          wrote on last edited by
          #4

          Thanks for the replies.

          I did set setAcceptHoverEvents(true).

          And yes, that was a typo.
          And yes, the items() or itemAt() is how I realized that the internal function is not doing the contains() for shape as I assumed it might.

          I wish I knew what the items()/itemAt() was doing internally. Suppose I can get the source for it... but afraid it'd take me forever to wade through it.

          Honestly, I think I've inadvertently done something else and this must be some side effect. Oh, the pain.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            texas
            wrote on last edited by
            #5

            I read also, in regards to itemAt() that "This function is deprecated and returns incorrect results if the scene contains items that ignore transformations."

            My scene has no items that I've explicitly set to ignore transforms.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              Can you provide a small compilable example that reproduces the problem please?

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • T Offline
                T Offline
                texas
                wrote on last edited by
                #7

                I am going to try upgrading to the latest Qt 4.7.3. If this doesn't work, I may try to come up with an example. The thing is, for the most part it all works. When I do the following it stops working:
                @my_item->setParentItem(0);@

                Also, it's not that it is completely broken, sometimes I do get the hover event over my_item. It's like I have inadvertently done something to the scene that is causing the internal algorithm to fail.

                If I could live with my_item not being a top level item it'd be okay... but really the fact that it's not working like I expect tells me I am missing something important.

                Thanks again for the response. Just knowing somebody is there helps. I was at my wits end on this problem.

                1 Reply Last reply
                0
                • Z Offline
                  Z Offline
                  ZapB
                  wrote on last edited by
                  #8

                  OK good luck!

                  Nokia Certified Qt Specialist
                  Interested in hearing about Qt related work

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    texas
                    wrote on last edited by
                    #9

                    Well, the upgrade to 4.7.3 didn't work.

                    However, I was able to "fix" it. I told the scene to use the linear indexing scheme and it worked. I am guessing I need to do some sort of refresh on the bsp indexer when I reparent my_item as a toplevel item. Anyhow, here's the "fix":

                    @my_scene->setItemIndexMethod(QGraphicsScene::NoIndex);@

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      texas
                      wrote on last edited by
                      #10

                      I ran into this problem again. I wish I knew what I was doing wrong. I am using events to cause graphic item position changes. It's like the scene's BspTreeIndex needs to refresh it's cache. I am guessing that Qt assumes an order and I am violating it. Anyhow, I will drop back to QGraphicsScene::NoIndex. Hopefully I won't get too much of a performance penalty.

                      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