steg90
2nd October 2007, 11:44
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 :
class CCanGraphicsItem : public QGraphicsItem
{
public:
CCanGraphicsItem( int x, int y, QString strText ) : m_rect( 10, 10, 200, 100 ), m_color( QColor(255,255,255,255) )
{
m_strText = strText;
setPos( x, y );
m_pFont = new QFont("Times", 20, QFont::Bold );
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// painter->setRenderHint(QPainter::Antialiasing, true);
painter->setFont( *m_pFont );
painter->setPen(Qt::SolidLine);
painter->setBrush(m_color);
painter->drawRoundRect( m_rect );//10, 10, 100, 100);
painter->drawText( 20, 20, 190, 90, Qt::AlignCenter, m_strText );//, m_rect );
}
QRectF boundingRect() const { return m_rect; }
protected:
void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
{
QMessageBox::information(0, "Debug", m_strText );
}
void contextMenuEvent(QContextMenuEvent *event)
{
}
QRectF m_rect;
QColor m_color;
QString m_strText;
QFont* m_pFont;
};
Regards,
Steve
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 :
class CCanGraphicsItem : public QGraphicsItem
{
public:
CCanGraphicsItem( int x, int y, QString strText ) : m_rect( 10, 10, 200, 100 ), m_color( QColor(255,255,255,255) )
{
m_strText = strText;
setPos( x, y );
m_pFont = new QFont("Times", 20, QFont::Bold );
setFlag(QGraphicsItem::ItemIsMovable, true);
setFlag(QGraphicsItem::ItemIsSelectable, true);
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
// painter->setRenderHint(QPainter::Antialiasing, true);
painter->setFont( *m_pFont );
painter->setPen(Qt::SolidLine);
painter->setBrush(m_color);
painter->drawRoundRect( m_rect );//10, 10, 100, 100);
painter->drawText( 20, 20, 190, 90, Qt::AlignCenter, m_strText );//, m_rect );
}
QRectF boundingRect() const { return m_rect; }
protected:
void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
{
QMessageBox::information(0, "Debug", m_strText );
}
void contextMenuEvent(QContextMenuEvent *event)
{
}
QRectF m_rect;
QColor m_color;
QString m_strText;
QFont* m_pFont;
};
Regards,
Steve