Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why the shape can't be moved
Qt 6.11 is out! See what's new in the release blog

Why the shape can't be moved

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nimingzhe2008
    wrote on last edited by
    #1

    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;
    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      beemaster
      wrote on last edited by
      #2

      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);
      @

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nimingzhe2008
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beemaster
          wrote on last edited by
          #4

          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;
          }
          @

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nimingzhe2008
            wrote on last edited by
            #5

            It doesn't work

            1 Reply Last reply
            0
            • B Offline
              B Offline
              beemaster
              wrote on last edited by
              #6

              Yes it does

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved