Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QGraphicsItem: any convenience function like setRect()?
I wanted to resize different types of QGraphicsItem without subclassing them.
It seems like ONLY QGraphicsRectItem and QGraphicsEllipseItem have the member "setRect()" to resize them from the outside.
Are there any ways I can achieve this?
"void QGraphicsItem::setScale":http://doc.qt.digia.com/qt/qgraphicsitem.html#setScale can be used to resize/rescale a QGraphicsItem
[quote author="utcenter" date="1358744881"]"void QGraphicsItem::setScale":http://doc.qt.digia.com/qt/qgraphicsitem.html#setScale can be used to resize/rescale a QGraphicsItem[/quote]
Sorry, but that only have 1 parameter. I need 2 dimensions - length and width... or a QRectF parameter. Any other options?
The scale method is designed to proportionally scale the size of the item, a scale of 1 is the normal size, a scale of 2 the item is twice as big, a scale of 0.5 the item is twice as small.
If you want to use non-proportional scaling, you have to use a transform matrix, in which you can incorporate the custom "length and width" - however doing disproportional transformation may distort and make your item ugly looking.
Actually, I was able to use QTransform, and distortion was my big problem. It even distort the childItems, which is not my goal. My only goal is to resize the parent item while maintaining the sizes of the child items. Using scale and QTransform would be a bad idea! AFAIK
QGraphicsRectItem::setRect() and QGraphicsEllipseItem::setRect() makes my goal achievable but for other QGraphicsItem it fails.
Well, it figures, rect and ellipse item use a qrect for drawing. If you go for that approach and incorporate a QRect inside of your item and center your custom painting around it, you can add the setRect method and only alter the dimensions of the parent item just as you want to.
I guess subclassing the QGraphicsItems (pixmap, text, etc.) would be a better choice then.
or can we ask for a feature request for this?
QGraphicsItem is a generic class, created for the purpose of being subclassed, so I would go with that. The type of functionality you want is not standard to QGraphicsItem's typical usage scenario, so I doubt a feature request will do any good. Just subclass, add a QRect member and use it to dimension the paint event, add the setRect() method and you have the functionality you want.
steno last edited by
QGraphicsWidget doesn't have a setRect, but it does have a setGeometry, which is the same thing. That might be an option worth looking into.