看看 QGraphicsItem::setTransformOriginPoint()。它采用项目坐标中的位置,因此您可能必须将鼠标位置映射到项目坐标。
我认为这似乎按您的预期工作:
class Scene : public QGraphicsScene
{
public:
QGraphicsItem* item;
Scene(QObject* parent = nullptr)
: QGraphicsScene(parent)
{
setSceneRect(0, 0, 500, 500);
item = addRect(200, 200, 100, 100, QPen(Qt::black), QBrush(Qt::red));
}
void wheelEvent(QGraphicsSceneWheelEvent* event)
{
/*Scale 0.2 each turn of the wheel (which is usually 120.0 eights of degrees)*/
qreal scaleFactor = 1.0 + event->delta() * 0.2 / 120.0;
item->setTransformOriginPoint(item->mapFromScene(event->scenePos()));
item->setScale(item->scale() * scaleFactor);
}
};
int main(int argc, char ** argv)
{
QApplication a(argc, argv);
Scene* scene = new Scene;
QGraphicsView view(scene);
view.resize(600, 600);
view.show();
return a.exec();
}