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. rotated qgraphicssceneitem bounding rect
Forum Updated to NodeBB v4.3 + New Features

rotated qgraphicssceneitem bounding rect

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicssceneqgraphicsitemqpolygonqrectrotation
3 Posts 2 Posters 914 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.
  • L Offline
    L Offline
    Loc888
    wrote on last edited by Loc888
    #1

    img.jpg

    Everything like in the image, i want to get the boundingrect of the rotated item, and since it's a pixmap item, i need to use scene pos as x,y, and pixmap size as width and height, but doesn't really matter since with other items same problems occur, i tried to create a QMatrix, convert it to polygon using map to polygon and then get the bounding rect, but the results are random sh*t popping up on the screen, with incorrect position and size... I am not wasteing my time on randomness since not even 1 thing is correct, and the problem seems very trivial.

    I suspect the rect i am using as a source to later transform it into a polygon and apply the same rotation as the item has, is not the same rect before the rotation occurs.

    QPolygonF Graphics_Pixmap::rotated_poly()
    {
        Polygon p;
        p.x(scenePos().x());
        p.y(scenePos().y());
        p.width(m_pixmap_width);
        p.height(m_pixmap_height);
    
        double r = rotation();
        p.set_by_rect(QRectF(scenePos().x(), scenePos().y(), m_pixmap_width, m_pixmap_height));
    
        p.angle(rotation());
        return *dynamic_cast<QPolygonF*>(&p);
    }
    
    void Polygon::update_geometry()
    {
        QRectF r;
        r.setRect(0, 0, m_width, m_height);
        set_by_rect(r);
    
        QMatrix matrix;
        matrix.rotate(m_angle);
        *dynamic_cast<QPolygonF*>(this) = matrix.map(*dynamic_cast<QPolygonF*>(this));
        translate(m_x, m_y);
    }
    
    void Polygon::set_by_rect(const QRectF& rect)
    {
        *dynamic_cast<QPolygonF*>(this) = QTransform().mapToPolygon(rect.toRect());
    }
    

    ANd then at the end just use rotated_poly().boundingRect(), but this doesn't work, the results are near random, and it's a very simple thing, notice in another problem i had this approeach worked exactly how it should, so it would generate a rotated polygon.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Loc888
      wrote on last edited by
      #2

      img2.png

      On the right side there is the item without any rotation, after 45 degree rotation on the left side, the blue figure it's the generated polygon which is trying to imitate the scene object rotation, and the yellow figure, it's the final rect generated by the rotated_poly().boundingRect(), but the "coordiantion" if i can name it this was seems wrong, the yellow rect should be same size as the blue one, and the position should be on the center of the item... Idk what to do with it, i tryied many combinations and this one seems "the best", but still not a valid result.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        ThThoma
        wrote on last edited by
        #3

        Hey, not sure if you found the solution yet. But you can try this:

            QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem;
            pixmapItem->setPixmap(QPixmap(":/images/pixmap.png"));
            _scene->addItem(pixmapItem);
        
            pixmapItem->setRotation(pixmapItem->rotation() + 45.0);
        
            QGraphicsRectItem *boundingRect = new QGraphicsRectItem(pixmapItem->sceneBoundingRect());
            QPen outlinePen;
            outlinePen.setWidth(2);
            outlinePen.setColor(Qt::red);
            boundingRect->setPen(outlinePen);
            _scene->addItem(boundingRect);
        

        I believe what you want is the sceneBoundingRect() and not the boundingRect().

        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