Results 1 to 4 of 4

Thread: QGraphicsSvgItem, problem with scaling (invalid bounding rect)

  1. #1
    Join Date
    Mar 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsSvgItem, problem with scaling (invalid bounding rect)

    Hello.
    As I suprisingly noticed, calling scale() method on QGraphicsSvgItem doesn't change its bounding rect. To fix it, I made my own class inheriting from it which updates the bounding rect when needed. The problem is the Qt finds it somehow invalid, for example when i call the moveBy() method on the item, only the part of the item is moved. Furthermore, mouseReleaseEvent() catches only clicks in that area. what could be the reason of this behaviour?

    Screenshot

    I'm quite sure the bounding rect returned is correct, I even checked with screen ruler and it turned out to be right.

    Here is some code (I left only important parts):
    Qt Code:
    1. class Card : public QGraphicsSvgItem {
    2. Q_OBJECT
    3.  
    4. public:
    5. Card(CardType type, CardValue value, QSvgRenderer * renderer);
    6. void scale(qreal sx, qreal sy);
    7. QRectF boundingRect() const;
    8.  
    9. signals:
    10. void clicked(Card * card);
    11.  
    12. private:
    13. QRectF * rect;
    14.  
    15. void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
    16.  
    17. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Card::Card(CardType type, CardValue value, QSvgRenderer * renderer){
    2. prepareGeometryChange();
    3. this->rect = new QRectF(QGraphicsSvgItem::boundingRect());
    4. scale(this->scaleRatio, this->scaleRatio);
    5. }
    6.  
    7. void Card::scale(qreal sx, qreal sy){
    8. this->rect->setWidth(this->rect->width()*sx);
    9. this->rect->setHeight(this->rect->height()*sy);
    10. QGraphicsSvgItem::scale(sx, sy);
    11. }
    12.  
    13. QRectF Card::boundingRect() const {
    14. return *(this->rect);
    15. }
    16.  
    17. void Card::mouseReleaseEvent(QGraphicsSceneMouseEvent * event){
    18. emit clicked(this);
    19. }
    To copy to clipboard, switch view to plain text mode 
    and the slot connected to clicked():
    Qt Code:
    1. void BoardWidget::cardClicked(Card * card) {
    2. qDebug() << "Kliknieto" << *card << card->boundingRect();
    3. card->moveBy(10, 10);
    4. }
    To copy to clipboard, switch view to plain text mode 
    The bounding rect of the card in cardClicked() matches with the real size, its shape too. What could be the reason of the problem?

  2. #2
    Join Date
    Jul 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsSvgItem, problem with scaling (invalid bounding rect)

    Hello.
    The boundingRect() method returns the bounding rectangle in item coordinates, so it is not affected by transformations such as scale, rotation...
    If you want to get the bounding rectangle in scene coordinates you have to use the family of methods mapToScene(...).

    I hope this answers your question.

    Regards,

    Sebastien.

  3. #3
    Join Date
    Mar 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsSvgItem, problem with scaling (invalid bounding rect)

    Ah. How it was possible I couldn't notice it back then... Thank you for your help.

  4. #4
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsSvgItem, problem with scaling (invalid bounding rect)

    Hi,

    I have a question related to this post. I am writing a graphical application that require scale of graphic items. In the application the user drags and drops polygon from a toolbar (like Dia does). When the polygon lands into the scene it has an standard size. I need to implement the scale of the items. Im using the setScale function. The problem that I have is when user want to scale an already scaled item. For example if I scale an item to 2.0 the result is a item twice its original size, if I do 2.0 again the function uses the original polygon and not the scaled one.

    I tried to work around the problem with the following code:
    Qt Code:
    1. //Scaled polygon
    2. QPolygonF spoly;
    3.  
    4. //One item in the scene
    5. tnkPolygonSymbol *item;
    6.  
    7. //Gets one item from the scene
    8. item = qgraphicsitem_cast<tnkPolygonSymbol *>(scenes[currentPage]->items()[0]);
    9.  
    10. //Scale the item to double
    11. item->setScale(2.0);
    12.  
    13. //Gets the scaled item from the scene
    14. spoly = item->mapToScene(item->polygon());
    15.  
    16. //Restores the scale to the original size
    17. item->setScale(1.0);
    18.  
    19. //Replaces the items polygon with the scaled polygon
    20. item->setPolygon(spoly);
    To copy to clipboard, switch view to plain text mode 

    However this does not work as the resulting polygon has different coordinate values than the original. The result of the process is shown in Image 1:


    After the process the bounding rectangle is double is size but the resized polygon is away from its bounding rectangle. This may happen because the coordinate values of the spoly are not the same as the item in the scene. I tried to set the coordinates of spoly with moveTo, CeterOn, etc and nothing seems to work.

    Any ideas will be appreciated.

    Carlos.

Similar Threads

  1. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.