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. Dynamically update a custom line class in custum QGraphicsScene subclass
Forum Updated to NodeBB v4.3 + New Features

Dynamically update a custom line class in custum QGraphicsScene subclass

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

    Hi everybody,
    I am trying to implement a custom QGraphicsScene (FloorplanScene) to which I want to add different objects (subclasses of FloorplanItem, which itself is a subclass of QGraphicsItem). My first object should be a simple line that the user can dynamically draw onto the scene when they hold the mouse button pressed. After the insertion of the line, I want the whole bounding rectangle region to react to mouse clicks, such that the line can be selected and moved.

    Although the bounding box implementation is correct all the time (I'm sure of this because of a reference visualization), the area in which the line is selectable does not ressemble the bounding box. The respective code snippets look as follows:

    //floorplanscene.cpp
    void FloorplanScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent)
    {
    
        FloorplanItem* item;
        item = FloorplanItemFactory::makeFloorplanItem(myItemType, myItemMenu);
        item->initOnInsertion(mouseEvent->scenePos());
        addItem(item);
        itemCurrentlyInserted = item;
    
        QGraphicsScene::mousePressEvent(mouseEvent);
    }
    
    void FloorplanScene::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent)
    {
        if (itemCurrentlyInserted != nullptr) {
            itemCurrentlyInserted->updateOnInsertion(mouseEvent->scenePos());
        }
    
         QGraphicsScene::mouseMoveEvent(mouseEvent);
    }
    
    void FloorplanScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* mouseEvent)
    {
        if (itemCurrentlyInserted != nullptr) {
            itemCurrentlyInserted->updateOnInsertion(mouseEvent->scenePos());
            emit itemInserted(itemCurrentlyInserted);
            itemCurrentlyInserted = nullptr;
        }
    }
    

    In my described case, FloorplanItemFactory returns an instance of MyLine*; its superclass is FloorplanItem and FloorplanItem's superclass is QGraphicsItem. The Line code looks as follows.

    MyLine::MyLine(QMenu* contextMenu, QGraphicsItem* parent) :
                           FloorplanItem(contextMenu, parent)
    {
        myGeometry = QLineF(QPointF(0,0), QPointF(0,0));
    
        setFlag(QGraphicsItem::ItemIsMovable, true);
        setFlag(QGraphicsItem::ItemIsSelectable, true);
        setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    }
    
    void MyLine::setLineStart(QPointF const& start)
    {
        myGeometry.setP1(start);
    }
    
    void MyLine::setLineEnd(QPointF const& end)
    {
        myGeometry.setP2(end);
    }
    
    /* virtual */ QRectF MyLine::boundingRect() const
    {
        QPointF topLeft(std::min(myGeometry.p1().x(), myGeometry.p2().x()), std::min(myGeometry.p1().y(), myGeometry.p2().y()));
        return QRectF(topLeft, QSizeF(std::abs(myGeometry.dx()), std::abs(myGeometry.dy())));
    }
    
    /* virtual */ void MyLine::updateOnInsertion(QPointF const& mouseScenePos)
    {
        setLineEnd(mouseScenePos-pos());
    }
    
    /* virtual */ void MyLine::initOnInsertion(QPointF const& mouseScenePos)
    {
        setLineStart(QPointF(0,0));
        setLineEnd(QPointF(0,0));
        setPos(mouseScenePos);
    }
    
    /* virtual */ void MyLine::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
    {
        QPen pen(Qt::black, 2);
        painter->setPen(pen);
        painter->drawLine(myGeometry);
    
        //debug draw of bounding rectangle - looks fine
        painter->setPen(QPen(Qt::red, 3));
        painter->drawRect(boundingRect());
    
        //draw selection rectangle when selected
        FloorplanItem::paint(painter, option, widget);
    }
    

    Now when I place a line in the scene, the area in which the line is selectable is much smaller than the bounding box. However, when I have selected the line in this small and wrong region and move the line a bit around, everything is fine again for the next selection. Apart from that, when I go to initOnInsertion and call setLineEnd(QPointF(2000,2000)) just shortly at the beginning, I do not experience that problem anymore (so maybe some caching stuff going on?).

    I hope you can help me out, I would be very glad :)

    Regards from Germany,
    Tim

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You should take a look at the QGraphicsLineItem::boudingRect implementation to implement yours.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lopatim
        wrote on last edited by
        #3

        Hi there,
        thank you for your quick reply and your warm welcoming words. I have copied the implementation of QGraphicsLineItem::boundingRect and adapted the variable names to mine:

        /* virtual */ QRectF MyLine::boundingRect() const
        {
            const qreal x1 = myGeometry.p1().x();
            const qreal x2 = myGeometry.p2().x();
            const qreal y1 = myGeometry.p1().y();
            const qreal y2 = myGeometry.p2().y();
            qreal lx = qMin(x1, x2);
            qreal rx = qMax(x1, x2);
            qreal ty = qMin(y1, y2);
            qreal by = qMax(y1, y2);
            return QRectF(lx, ty, rx - lx, by - ty);
        }
        

        Unfortunately, I am still experiencing the same problem as before. Just a tiny subrectangle of the line's bounding rectangle results in selecting the object although a debug plot of the bounding rectangle indicates everything is correct.

        Do you have another idea what could cause this problem?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Did you check that boundingRect returns a sensible value ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lopatim
            wrote on last edited by lopatim
            #5

            Yes, the bounding rectangle parameters (x,y,width,height) look reasonable in my eyes. For a diagonal line from top left to bottom right (see attachment), the bounding rectangle's top left corner is (0, 0) as I defined the first line point to be in (0, 0) in object coordinates and the width and height values are also reasonable. The red rectangle in the picture is the debug visualization of the bounding box, which looks correct in my opinion. However, roughly spoken, just clicks in the large top left square of the background grid result in the line being selected. Clicks everywhere else within the red rectangle are ignored. When I once move the line by clicking and dragging in the top left large sqare, then everything is fine for the next selections.

            Screenshot

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lopatim
              wrote on last edited by lopatim
              #6

              I think I've finally found the solution after comparing the QGraphicsLineItem code with mine line by line. Before I change the geometry of my line in setLineStart and setLineEnd, I need to call prepareGeometryChange() first, e.g.

              void MyLine::setLineStart(QPointF const& start)
              {
                  prepareGeometryChange();
                  myGeometry.setP1(start);
              }
              

              Seems to be a noobie mistake, but yes, I am new to Qt. Thank you for your support :)

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Don't worry, we all did/do mistakes like that one ;)

                Glad you found out and thanks for sharing !

                Since you have it working now, please mark the thread as solved using the "Topic Tool" button so other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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