简体   繁体   English

相对于另一个放置顶层对象

[英]Positioning a top-level object relative to another

I need to position a top-level object so that it always remains in a position relative to another top-level object. 我需要放置一个顶层对象,以便它始终相对于另一个顶层对象保持位置。 As an example, the rectangle in the image below should stick to the "front" of the ellipse: 例如,下图中的矩形应粘贴到椭圆的“前面”:

正确的定位示例

When rotated 180 degrees, it should look like this: 旋转180度时,它应如下所示:

正确的定位示例旋转了180度

Instead, the position of the rectangle is incorrect: 而是,矩形的位置不正确:

定位不正确

Please run the example below (the use of QGraphicsScene is for demonstration purposes only, as the actual use case is in physics). 请运行以下示例( QGraphicsScene的使用仅出于演示目的,因为实际用例在物理学中)。

#include <QtWidgets>

class Scene : public QGraphicsScene
{
    Q_OBJECT
public:
    Scene()
    {
        mEllipse = addEllipse(0, 0, 25, 25);
        mEllipse->setTransformOriginPoint(QPointF(12.5, 12.5));

        QGraphicsLineItem *line = new QGraphicsLineItem(QLineF(0, 0, 0, -12.5), mEllipse);
        line->setPos(12.5, 12.5);

        mRect = addRect(0, 0, 10, 10);
        mRect->setTransformOriginPoint(QPointF(5, 5));

        line = new QGraphicsLineItem(QLineF(0, 0, 0, -5), mRect);
        line->setPos(5, 5);

        connect(&mTimer, SIGNAL(timeout()), this, SLOT(timeout()));
        mTimer.start(5);
    }

public slots:
    void timeout()
    {
        mEllipse->setRotation(mEllipse->rotation() + 0.5);

        QTransform t;
        t.rotate(mEllipse->rotation());

        qreal relativeX = mEllipse->boundingRect().width() / 2 - mRect->boundingRect().width() / 2;
        qreal relativeY = -mRect->boundingRect().height();

        mRect->setPos(mEllipse->pos() + t.map(QPointF(relativeX, relativeY)));
        mRect->setRotation(mEllipse->rotation());
    }

public:
    QTimer mTimer;
    QGraphicsEllipseItem *mEllipse;
    QGraphicsRectItem *mRect;
};

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    QGraphicsView view;
    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    view.setScene(new Scene);
    view.resize(200, 200);
    view.show();

    return app.exec();
}

#include "main.moc"

Note that the position of the rectangle is not always the same, but it should always remain in the same position relative to the ellipse. 请注意,矩形的位置并不总是相同的,但相对于椭圆,它应始终保持相同的位置。 For example, it may start off in this position: 例如,它可能从以下位置开始:

替代定位示例

But it should stay in that relative position when rotated: 但是旋转时它应该保持在相对位置:

替代定位示例旋转180度

If you want the two objects to keep the same relative position, they need to rotate around the same origin point. 如果要使两个对象保持相同的相对位置,则它们需要围绕相同的原点旋转。

Here your circle rotates around its center (the point 12.5, 12.5), but your rectangle rotates around another origin (5,5) instead of the circle's center (12.5, 12.5). 在这里,圆绕其中心(点12.5、12.5)旋转,但是矩形绕另一个原点(5,5)而不是圆的中心(12.5、12.5)旋转。

If you fix the origin, it'll work as you expect: 如果您固定了原点,它将按预期工作:

mRect->setTransformOriginPoint(QPointF(12.5, 12.5));

Even if the rectangle starts off with an offset: 即使矩形以偏移量开始:

mRect = addRect(-10, 0, 10, 10); // Start 10 units to the left

结果截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM