Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QGraphicsItem move object only through X axis

Tags:

c++

qt

I have a problem moving my object only through x axis. I know you need to something with function QVariant itemChange ( GraphicsItemChange change, const QVariant & value ). I found something like this:

QVariant CircleItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange)
            return QPointF(pos().x(), value.toPointF().y());
    return QGraphicsItem::itemChange( change, value );
}

But it doesn't work. I'm new to Qt, so I don't know, how to change this thing.Here's code to my GraphicsItem:

#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 );
}

Thanks for answering.

like image 643
Oleksandr Verhun Avatar asked Apr 05 '14 14:04

Oleksandr Verhun


1 Answers

Read documentation of QGraphicsItem::ItemPositionChange. It says:

The item's position changes. This notification is sent if the ItemSendsGeometryChanges flag is enabled, and when the item's local position changes, relative to its parent (i.e., as a result of calling setPos() or moveBy()). The value argument is the new position (i.e., a QPointF). You can call pos() to get the original position. Do not call setPos() or moveBy() in itemChange() as this notification is delivered; instead, you can return the new, adjusted position from itemChange(). After this notification, QGraphicsItem immediately sends the ItemPositionHasChanged notification if the position changed.

And in our code I don't see you have set this ItemSendsGeometryChanges flag, so correct constructor like this:

CircleItem::CircleItem() // where is parent parameter?
{
    RectItem = new RoundRectItem();
    MousePressed = false;
    setFlag(ItemIsMovable | ItemSendsGeometryChanges);
}
like image 134
Marek R Avatar answered Oct 11 '22 13:10

Marek R