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 to stop click propagation in QGraphicsScene without making ItemIsSelectable?
Qt 6.11 is out! See what's new in the release blog

How to stop click propagation in QGraphicsScene without making ItemIsSelectable?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 599 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Besides three slices, QGraphicsItem, I've an ellipse and a text in the scene:

    ellipse = new QGraphicsEllipseItem();
    ellipse->setBrush(Qt::white);
    ellipse->setPen(Qt::NoPen);
    text = new QGraphicsTextItem(ellipse);
    

    This ellipse with text is added/removed in the scene dynamically on hoverEvents of slices. Without any modification in the constructor of PieView, QGraphicsView, this is what happens when mouse is over the ellipse or clicked:

    x1.gif

    so even though the ellipse is opaque, slices beneath the ellipse receives hover...Event and when I click on the ellipse, those slices receive mousePressEvent. If I change the definition of the ellipse like this:

    ellipse = new QGraphicsEllipseItem();
    ellipse->setBrush(Qt::white);
    ellipse->setPen(Qt::NoPen);
    ellipse->setAcceptHoverEvents(true);
    ellipse->setAcceptedMouseButtons(Qt::AllButtons);
    ellipse->setFlag(QGraphicsItem::ItemStopsClickFocusPropagation);
    text = new QGraphicsTextItem(ellipse);
    

    slices beneath the ellipse don't receive hover...Event as long as the mouse is over the ellipse BUT they receive mousePressEvent:

    x2.gif

    One way that I've found works is make the ellipse selectable like this:

    ellipse->setFlags(QGraphicsItem::ItemIsSelectable);`
    

    BUT that introduces unnecessary complicacy in code. Is there any other way/flag to stop click propagation when I click on the opaque ellipse?

    JonBJ 1 Reply Last reply
    0
    • ? A Former User

      Besides three slices, QGraphicsItem, I've an ellipse and a text in the scene:

      ellipse = new QGraphicsEllipseItem();
      ellipse->setBrush(Qt::white);
      ellipse->setPen(Qt::NoPen);
      text = new QGraphicsTextItem(ellipse);
      

      This ellipse with text is added/removed in the scene dynamically on hoverEvents of slices. Without any modification in the constructor of PieView, QGraphicsView, this is what happens when mouse is over the ellipse or clicked:

      x1.gif

      so even though the ellipse is opaque, slices beneath the ellipse receives hover...Event and when I click on the ellipse, those slices receive mousePressEvent. If I change the definition of the ellipse like this:

      ellipse = new QGraphicsEllipseItem();
      ellipse->setBrush(Qt::white);
      ellipse->setPen(Qt::NoPen);
      ellipse->setAcceptHoverEvents(true);
      ellipse->setAcceptedMouseButtons(Qt::AllButtons);
      ellipse->setFlag(QGraphicsItem::ItemStopsClickFocusPropagation);
      text = new QGraphicsTextItem(ellipse);
      

      slices beneath the ellipse don't receive hover...Event as long as the mouse is over the ellipse BUT they receive mousePressEvent:

      x2.gif

      One way that I've found works is make the ellipse selectable like this:

      ellipse->setFlags(QGraphicsItem::ItemIsSelectable);`
      

      BUT that introduces unnecessary complicacy in code. Is there any other way/flag to stop click propagation when I click on the opaque ellipse?

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

      @Emon-Haque
      I saw also your other post about this. I don't claim to know what is going on, but I would suggest you look at the eventFilter on QGraphicsScene/View. Everything ought to go through there, so you can debug to see what is going on and (presumably) filter events to stop propagation etc.

      ? 1 Reply Last reply
      0
      • JonBJ JonB

        @Emon-Haque
        I saw also your other post about this. I don't claim to know what is going on, but I would suggest you look at the eventFilter on QGraphicsScene/View. Everything ought to go through there, so you can debug to see what is going on and (presumably) filter events to stop propagation etc.

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @JonB, It's hard to control the selection mechanism of scene. Depending on where one clicks, scene automatically selects/deselects item. So instead of using the setSelected/isSelected in Slice, I've creted isChecked/setChecked to control the movements of Slice and removed ItemIsSelectable flag from Slice constructor. In PieView made these changes:

        scene->setSceneRect(0,0,200,200);
        ellipse = new QGraphicsEllipseItem();
        ellipse->setBrush(Qt::white);
        ellipse->setPen(Qt::NoPen);
        ellipse->setAcceptHoverEvents(true);
        ellipse->setFlag(QGraphicsItem::ItemIsSelectable);
        text = new QGraphicsTextItem(ellipse);
        text->setAcceptHoverEvents(false);
        

        the weird movement of slices, when mouse first entered, was solved by setting setSceneRect. With these changes, drawings and slice movements are almost perfect. A rectangular dashed border becomes visible when I click on the ellipse:

        x3.gif

        That's weird! In some places I've seen people suggested to subclass the ellipse and override the paint BUT that's a lot for such a simple thing. Is there any other way to remove that border around the ellipse?

        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