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