Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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
C++ QGraphicsItem仅通过X轴移动对象_C++_Qt - Fatal编程技术网

C++ QGraphicsItem仅通过X轴移动对象

C++ QGraphicsItem仅通过X轴移动对象,c++,qt,C++,Qt,我无法仅通过x轴移动对象。我知道您需要使用函数QVariant-itemChange(GraphicsItemChange-change,const-QVariant&value)来更改某些内容。我发现了这样的东西: QVariant CircleItem::itemChange(GraphicsItemChange change, const QVariant &value) { if (change == ItemPositionChange) ret

我无法仅通过x轴移动对象。我知道您需要使用函数
QVariant-itemChange(GraphicsItemChange-change,const-QVariant&value)
来更改某些内容。我发现了这样的东西:

QVariant CircleItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange)
            return QPointF(pos().x(), value.toPointF().y());
    return QGraphicsItem::itemChange( change, value );
}
CircleItem::CircleItem() // where is parent parameter?
{
    RectItem = new RoundRectItem();
    MousePressed = false;
    setFlag(ItemIsMovable | ItemSendsGeometryChanges);
}
但它不起作用。我是Qt新手,所以我不知道如何更改它。下面是我的GraphicsSitem的代码:

#include "circleitem.h"

CircleItem::CircleItem()
{
    RectItem = new RoundRectItem();
    MousePressed = false;
    setFlag( ItemIsMovable );
}

void CircleItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if( MousePressed )
    {
        painter->setBrush( QBrush( QColor( 0, 0, 255 ) ) );
        painter->setPen( QPen( QColor( 0, 0, 255 ) ) );
    }
    else
    {
        painter->setBrush( QBrush( QColor( 255, 255, 255 ) ) );
        painter->setPen( QPen( QColor( 255, 255, 255 ) ) );
    }
    painter->drawEllipse( boundingRect().center(), boundingRect().height() / 4 - 7, boundingRect().height() / 4 - 7 );
}

QRectF CircleItem::boundingRect() const
{
    return RectItem->boundingRect();
}

void CircleItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    MousePressed = true;

    update( QRectF().x(), boundingRect().y(), boundingRect().width(), boundingRect().height() );
    QGraphicsItem::mousePressEvent( event );

}

void CircleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    MousePressed = false;

    update( QRectF().x(), boundingRect().y(), boundingRect().width(), boundingRect().height() );
    QGraphicsItem::mouseReleaseEvent( event );

}

QVariant CircleItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange)
            return QPointF(pos().x(), value.toPointF().y());
    return QGraphicsItem::itemChange( change, value );
}
感谢您的回答。

请阅读的文档。它说:

项目的位置发生变化。如果 ItemSendsGeometryChanges标志已启用,并且当项目的本地 相对于其父级的位置变化(即,由于调用 setPos()或moveBy()。value参数是新位置(即a QPointF)。您可以调用pos()来获取原始位置。不要打电话 itemChange()中的setPos()或moveBy()与此通知相同 交付;相反,您可以从返回新的调整位置 itemChange()。发出此通知后,QGraphicsItem会立即发送 如果位置更改,则ItemPositionHasChanged通知

在我们的代码中,我看不到您设置了
itemssendsgeometrychanges
标志,因此请更正如下构造函数:

QVariant CircleItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange)
            return QPointF(pos().x(), value.toPointF().y());
    return QGraphicsItem::itemChange( change, value );
}
CircleItem::CircleItem() // where is parent parameter?
{
    RectItem = new RoundRectItem();
    MousePressed = false;
    setFlag(ItemIsMovable | ItemSendsGeometryChanges);
}
谢谢回答,它起作用了(只是我太头晕了)等一些问题在一段时间内回答:)