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. Selection of Graphics Items
Forum Updated to NodeBB v4.3 + New Features

Selection of Graphics Items

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 177 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.
  • C Offline
    C Offline
    Calivernon
    wrote on last edited by Calivernon
    #1

    Hello,

    I am struggling to use:
    mousePressEvent,
    QGraphicsSceneMouseEvent::scenePos() and
    QGraphicsSceneMouseEvent::itemAt().

    In my application I am using a main widget with a graphicsScene and a graphicsView (only one view), by clicking on the scene with the mouse I am drawing a new hexagon or selecting an hexagon, more precisely when clicking in an empty part of the scene it creates a new hexagon (which is automatically selected), if I click on an already existing hexagon it selects the hexagon under the mouse, the problem is that it does not always detect the hexagon under the mouse (sometimes mouse is over an hexagon but it creates a new one over it, sometimes it is in a blank part but it does not create a new hexagon instead it selects another hexagon).

    I overrode the mousePressEvent of the customized graphicsScene:

    void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
    {
        if (mouseEvent->button() == Qt::LeftButton)
        {
            QPointF pt = mouseEvent->scenePos();
            QGraphicsItem* pHexagonUnderMouse = itemAt(pt, QTransform());
            qDebug() << "\n1 Press" << pt;
    
            if (pHexagonUnderMouse == Q_NULLPTR)
            {
                qDebug() << "2 Creation  " << pt << " " << mouseEvent->scenePos();
                clearSelection();
                GetMainWindow()->CreateHexagonOnScene(QPoint(pt.x(), pt.y()));
            }
            else
            {
                qDebug() << "2 Selection " << pt << " " << mouseEvent->scenePos();
                clearSelection();
                GetMainWindow()->SetSelection(static_cast<Hexagon*>(pHexagonUnderMouse));
            }
            GetMainWindow()->UpdateScene();
        }
    }
    

    I am not sure to use correctly the itemAt(pt, QTransform()) to detect if there is a QGraphicsItem under the mouse as I don't know what kind of QTransform to use, I am neither sure how to use scenePos(), maybe I should use it with a function such as mapFromItem .... (I don't understand all those functions so far).

    Thank you for your help.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Calivernon
      wrote on last edited by
      #2

      Ok just found out that my BBox was not correct (the boundingRect()), so it is ok now it is working.
      Thx, bye bye

      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