Why the shape can't be moved
-
The book Foundations of Qt development offer an custom widget example which is used to move shape.But When I compiled it,only the custom widget can be moved and shape can't be moved.Here is the code
handleitem.h
@#ifndef HANDLEITEM_H
#define HANDLEITEM_H#include <QGraphicsItem>
class HandleItem;
class HandleItem : public QGraphicsItem
{
public:
enum HandleRole
{
CenterHandle,
RightHandle,
TopHandle
};HandleItem( QGraphicsItem *item, QGraphicsScene *scene, QColor color, HandleRole role = CenterHandle );
void paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget );
QRectF boundingRect() const;protected:
void mousePressEvent( QGraphicsSceneMouseEvent *event );
void mouseReleaseEvent( QGraphicsSceneMouseEvent *event );QVariant itemChange( GraphicsItemChange change, const QVariant &data );
private:
QGraphicsItem *m_item;HandleRole m_role;
QColor m_color;bool m_pressed;
};#endif // HANDLEITEM_H
@handleitem.cpp
@#include "handleitem.h"
#include <QPainter>
#include <QPointF>HandleItem::HandleItem( QGraphicsItem *item, QGraphicsScene *scene, QColor color, HandleItem::HandleRole role ) : QGraphicsItem( 0, scene )
{
m_role = role;
m_color = color;m_item = item;
m_pressed = false;
setZValue( 100 );setFlag( ItemIsMovable );
}void HandleItem::paint( QPainter *paint, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
paint->setPen( m_color );
paint->setBrush( m_color );QRectF rect = boundingRect();
paint->drawEllipse( rect );
}
QRectF HandleItem::boundingRect() const
{
QPointF point = m_item->boundingRect().center();return QRectF( point-QPointF(5, 5), QSize( 10, 10 ) );
}
QVariant HandleItem::itemChange( GraphicsItemChange change, const QVariant &data )
{
if( change == ItemPositionChange && m_pressed )
{
QPointF movement = data.toPoint() - pos();m_item->moveBy( movement.x(), movement.y() ); return QGraphicsItem::itemChange( change, pos()+movement );
}
return QGraphicsItem::itemChange( change, data );
}void HandleItem::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
m_pressed = true;
QGraphicsItem::mousePressEvent( event );
}void HandleItem::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
{
m_pressed = false;
QGraphicsItem::mouseReleaseEvent( event );
}
@main.cpp
@#include <QApplication>#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>#include "handleitem.h"
int main( int argc, char **argv )
{
QApplication app( argc, argv );QGraphicsScene scene( 0, 0, 200, 200 );
QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( 10, 10, 50, 100 ), 0, &scene );
QGraphicsEllipseItem *elItem = new QGraphicsEllipseItem( QRect( 80, 40, 100, 80 ), 0, &scene );HandleItem *crh = new HandleItem( rectItem, &scene, Qt::red, HandleItem::CenterHandle);
HandleItem *ceh = new HandleItem( elItem, &scene, Qt::green, HandleItem::CenterHandle);
QGraphicsView view;
view.setScene( &scene );
view.show();int res = app.exec();
delete crh;
delete ceh;
return res;
}
@ -
If you want to move standard graphics items, you need to set ItemIsMovable flag
@
QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( 10, 10, 50, 100 ), 0, &scene );
QGraphicsEllipseItem *elItem = new QGraphicsEllipseItem( QRect( 80, 40, 100, 80 ), 0, &scene );
rectItem->setFlag(QGraphicsItem::ItemIsMovable);
elItem->setFlag(QGraphicsItem::ItemIsMovable);
@ -
[quote author="beemaster" date="1351260718"]If you want to move standard graphics items, you need to set ItemIsMovable flag
[/quote]
I add
@m_item->setFlag(ItemIsMovable);@
to constructor,but it also doesn't work. -
You should add it to the main.cpp
Full main.cpp:
@
#include <QApplication>#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>
#include <QGraphicsEllipseItem>#include "handleitem.h"
int main( int argc, char **argv )
{
QApplication app( argc, argv );QGraphicsScene scene( 0, 0, 200, 200 );
QGraphicsRectItem *rectItem = new QGraphicsRectItem( QRect( 10, 10, 50, 100 ), 0, &scene );
QGraphicsEllipseItem *elItem = new QGraphicsEllipseItem( QRect( 80, 40, 100, 80 ), 0, &scene );rectItem->setFlag(QGraphicsItem::ItemIsMovable);
elItem->setFlag(QGraphicsItem::ItemIsMovable);HandleItem *crh = new HandleItem( rectItem, &scene, Qt::red, HandleItem::CenterHandle);
HandleItem *ceh = new HandleItem( elItem, &scene, Qt::green, HandleItem::CenterHandle);
QGraphicsView view;
view.setScene( &scene );
view.show();int res = app.exec();
delete crh;
delete ceh;
return res;
}
@ -
It doesn't work