Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt QGraphicsItem:模拟不是左上角的项目原点_Qt_Qgraphicsitem_Qgraphicspixmapitem - Fatal编程技术网

Qt QGraphicsItem:模拟不是左上角的项目原点

Qt QGraphicsItem:模拟不是左上角的项目原点,qt,qgraphicsitem,qgraphicspixmapitem,Qt,Qgraphicsitem,Qgraphicspixmapitem,我的应用程序正在使用Qt 我有一个正在继承的类 在这些项目上应用变换(例如,旋转)时,项目的原点(或轴心点)始终位于左上角 我想更改这个原点,例如,当设置项目的位置时,这将实际更改pixmap的中心 或者,如果应用旋转,旋转的原点将是pixmap的中心 我还没有找到一种直接用Qt实现的方法,所以我想这样重新实现: QVariant JGraphicsPixmapItem::itemChange(GraphicsItemChange Change, const QVariant& rVal

我的应用程序正在使用Qt

我有一个正在继承的类

在这些项目上应用变换(例如,旋转)时,项目的原点(或轴心点)始终位于左上角

我想更改这个原点,例如,当设置项目的位置时,这将实际更改pixmap的中心

或者,如果应用旋转,旋转的原点将是pixmap的中心

我还没有找到一种直接用Qt实现的方法,所以我想这样重新实现:

QVariant JGraphicsPixmapItem::itemChange(GraphicsItemChange Change, const QVariant& rValue)
{
    switch (Change)
    {
    case QGraphicsItem::ItemPositionHasChanged:
        // Emulate a pivot point in the center of the image
        this->translate(this->boundingRect().width() / 2,
                        this->boundingRect().height() / 2);
        break;
    case QGraphicsItem::ItemTransformHasChanged:
        break;
    }
    return QGraphicsItem::itemChange(Change, rValue);
}
我认为这是可行的,因为提到一个项目的位置和它的变换矩阵是两个不同的概念

但它不起作用

有什么想法吗?

void QGraphicsItem::旋转(qreal角度) 旋转当前项目 顺时针变换角度度 围绕它的起源。翻译 一个任意点(x,y),你需要 将平移和旋转与

例如:

// Rotate an item 45 degrees around (0, 0).
item->rotate(45);

// Rotate an item 45 degrees around (x, y).
item->setTransform(QTransform().translate(x, y).rotate(45).translate(-x, -y));

您需要创建一个旋转函数,将对象平移到父对象的(0,0)角进行旋转并将对象移动到原始位置。

您想得太多了。QGraphicsPixmapItem已经内置了此功能。参见方法


因此,要将项目原点设置在其中心,只需执行
setOffset(-0.5*QPointF(width(),height())
每次设置pixmap时。

我实际上在文档中提到了这一点,但我在问题中没有提到我想使用QGraphicsSiteManimation,因为它没有setTransformAt()方法,只有setPosAt、setRotationAt等。你完全正确:我没有看到这个偏移属性。这正是我所需要的。我的应用程序正在按预期工作。谢谢