Results 1 to 3 of 3

Thread: QGraphicsItem::itemIsMovable

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QGraphicsItem::itemIsMovable

    Hi,

    As part of my derived QGraphicsItem class I override the paint event to draw my object. I thought by setting the itemIsMovable flag would allow the item to be moved when user clicks and drags it? Am I missing something? Do I have to do more work in the paint event to allow the item to be moved?

    Here is the code for the class :

    Qt Code:
    1. class CCanGraphicsItem : public QGraphicsItem
    2. {
    3. public:
    4. CCanGraphicsItem( int x, int y, QString strText ) : m_rect( 10, 10, 200, 100 ), m_color( QColor(255,255,255,255) )
    5. {
    6. m_strText = strText;
    7. setPos( x, y );
    8. m_pFont = new QFont("Times", 20, QFont::Bold );
    9. setFlag(QGraphicsItem::ItemIsMovable, true);
    10. setFlag(QGraphicsItem::ItemIsSelectable, true);
    11. }
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    13. {
    14. // painter->setRenderHint(QPainter::Antialiasing, true);
    15. painter->setFont( *m_pFont );
    16. painter->setPen(Qt::SolidLine);
    17. painter->setBrush(m_color);
    18. painter->drawRoundRect( m_rect );//10, 10, 100, 100);
    19. painter->drawText( 20, 20, 190, 90, Qt::AlignCenter, m_strText );//, m_rect );
    20. }
    21.  
    22. QRectF boundingRect() const { return m_rect; }
    23.  
    24. protected:
    25. void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
    26. {
    27. QMessageBox::information(0, "Debug", m_strText );
    28.  
    29. }
    30.  
    31. void contextMenuEvent(QContextMenuEvent *event)
    32. {
    33. }
    34.  
    35. QRectF m_rect;
    36. QColor m_color;
    37. QString m_strText;
    38. QFont* m_pFont;
    39. };
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Steve

  2. #2
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsItem::itemIsMovable

    Hi,

    Seems that if I move the itemIsMovable into the paint event it works...

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsItem::itemIsMovable

    It'd be nice if you called base class implementations of events from within your event handlers. Otherwise it doesn't really matter what flags you set if Qt can't process them.

  4. The following user says thank you to wysota for this useful post:

    ajo (27th November 2012)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.