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. QGraphicsScene left click selects an item AND drags a rubberband.

QGraphicsScene left click selects an item AND drags a rubberband.

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.2k 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.
  • H Offline
    H Offline
    Hristo Konstantinov
    wrote on last edited by Hristo Konstantinov
    #1

    I'm trying to implement MS Excel-like selection style in my QGraphicsScene. The items are selectable by left click, but if I want to select them via rubberband, I have to start the selection outside of the item's bounds. Is there any way to select the item and drag a rubber band in the same time if the left mouse button is still not released?

    My scene is custom. I tried overriding the mousePressed event but with very little luck. Then I tried selecting the items via overriding mouseReleased (so that mousePressed can be reserved for the rubber band), but due to the nature of the rest of the UI (some other widgets are updated upon selection/deselection) it produces annoying flickering, because the Pressed event deselects, and the Released one selects the item again. Any ideas?

    Edit: This is the closest I got:

    void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    
        if (event->button() != Qt::LeftButton) 
           {
        
                event->accept();
                return;
            }
        
            QGraphicsItem* item = itemAt(event->scenePos(), QTransform());
            
           if (item != nullptr && item->isSelected()) return;
               else QGraphicsScene::mousePressEvent(event);
    }
    

    Now I get the rubberband as long as the item is selected, but I have to select it first with a click, and then drag the selectionbox.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hristo Konstantinov
      wrote on last edited by Hristo Konstantinov
      #2

      Exactly 8 months later, I finally managed to make it work the way that I want, so I'm posting the answer.

      Subclass both QGraphicsView and QGraphicsScene. For both of them mousePressEvent has to be reimplemented as follows:

      QGraphicsView subclass:

      void CustomView::mousePressEvent(QMouseEvent* event)
      {
      	QGraphicsView::mousePressEvent(event);
      	QGraphicsView::mousePressEvent(event);
             //yes, just call it twice
             // we're gonna lie to the view that the event is not handled >:)
      }
      

      QGraphicsScene subclass:

      void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent* event)
      {
          if (event->button() != Qt::LeftButton) {
              event->accept();
              return;
          }
      
          QGraphicsItem* item = itemAt(event->scenePos(), QTransform());
      
          if (item != nullptr && !item->isSelected())
          {
              item->setSelected(1);
          }
      }
      

      Debugging the code, I noticed, the last line - item->setSelected(1) is called on each of the two calls in the CustomView. However on the first call, the item is immediately deselected, since the event is not handled and the GraphicsView starts the rubberband selection, deselecting the item. The second call is where the item is permently selected, since the rubberband already exists. Not a very elegant solution, but it works like a charm. If anyone knows a better way, feel free to share it.

      1 Reply Last reply
      1

      • Login

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