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):
Q_OBJECT
public:
Card
(CardType type, CardValue value,
QSvgRenderer * renderer
);
void scale(qreal sx, qreal sy);
signals:
void clicked(Card * card);
private:
};
class Card : public QGraphicsSvgItem {
Q_OBJECT
public:
Card(CardType type, CardValue value, QSvgRenderer * renderer);
void scale(qreal sx, qreal sy);
QRectF boundingRect() const;
signals:
void clicked(Card * card);
private:
QRectF * rect;
void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
};
To copy to clipboard, switch view to plain text mode
Card
::Card(CardType type, CardValue value,
QSvgRenderer * renderer
){ prepareGeometryChange();
scale(this->scaleRatio, this->scaleRatio);
}
void Card::scale(qreal sx, qreal sy){
this->rect->setWidth(this->rect->width()*sx);
this->rect->setHeight(this->rect->height()*sy);
}
QRectF Card
::boundingRect() const { return *(this->rect);
}
emit clicked(this);
}
Card::Card(CardType type, CardValue value, QSvgRenderer * renderer){
prepareGeometryChange();
this->rect = new QRectF(QGraphicsSvgItem::boundingRect());
scale(this->scaleRatio, this->scaleRatio);
}
void Card::scale(qreal sx, qreal sy){
this->rect->setWidth(this->rect->width()*sx);
this->rect->setHeight(this->rect->height()*sy);
QGraphicsSvgItem::scale(sx, sy);
}
QRectF Card::boundingRect() const {
return *(this->rect);
}
void Card::mouseReleaseEvent(QGraphicsSceneMouseEvent * event){
emit clicked(this);
}
To copy to clipboard, switch view to plain text mode
and the slot connected to clicked():
void BoardWidget::cardClicked(Card * card) {
qDebug() << "Kliknieto" << *card << card->boundingRect();
card->moveBy(10, 10);
}
void BoardWidget::cardClicked(Card * card) {
qDebug() << "Kliknieto" << *card << card->boundingRect();
card->moveBy(10, 10);
}
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?
Bookmarks