PDA

View Full Version : Signal / Slot from QGraphicsItem



steg90
2nd October 2007, 14:55
Hi,

I have the following code declared in my QGraphicsScene derived class:



connect( pItem, SIGNAL(ShowOptions()),this,SLOT(ShowCanOptions()) );


Where pItem is an object derived from QGraphicsItem.

I get a conversion error on pItem - does this have to be derived from QObject?

Regards,
Steve

QCasper
2nd October 2007, 15:01
Yes, your item class have to be derived from QObject too. Note that you have to inherit from QObject first and then from QGraphicsIiem:

class MyGraphicsItem : public QObject, public QGraphicsItem {
Q_OBJECT
...
};

marcel
2nd October 2007, 15:02
Yes, and make sure QObject is first in the base class list.
Also don't forget to add the Q_OBJECT macro.

steg90
2nd October 2007, 15:07
Thanks Marcel, I did try that but still get the type conversion error.

This is my derived graphics item class :



class CCanGraphicsItem : public QObject, public QGraphicsItem
{
Q_OBJECT

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);
setFlag(QGraphicsItem::ItemIsMovable, 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; }

signals:
void ShowOptions();

protected:
void mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )
{
QMessageBox::information(0, "This", m_strText );
emit ShowOptions();
}

void contextMenuEvent(QContextMenuEvent *event)
{
}

QRectF m_rect;
QColor m_color;
QString m_strText;
QFont* m_pFont;
};



and the full code that tries to do the signal/slot connection :



void CanScene::AddBoxItem( int x, int y, QString strText, ITEMS item )
{
QGraphicsItem* pItem = NULL;

switch( item )
{
case CAN :
addItem( pItem = new CCanGraphicsItem( x, y, strText ) );
QObject::connect( pItem, SIGNAL(ShowOptions()),this,SLOT(ShowCanOptions()) );
break;
// etc...
}
}


Something I'm obviously doing wrong?!

Regards,
Steve

marcel
2nd October 2007, 15:15
Because the pItem is actually only a QGraphicsItem. It does not now about your signal.
Change the type of pItem to CCanGraphicsItem.

steg90
2nd October 2007, 15:23
Ooops :p

How I didn't notice that?!

Regards,
Steve

wysota
2nd October 2007, 15:25
I don't think this is a problem, at least not during compile time.

steg90: What exactly is the error?

marcel
2nd October 2007, 15:30
The error was most likely QObject::connect no such signal...etc

wysota
2nd October 2007, 15:33
I wouldn't call that a conversion error :) I expected a "Cannot convert ..." message.

steg90
2nd October 2007, 16:00
Hi guys,

Exact error was :

\CanScene.cpp(17) : error C2664: 'bool QObject::connect(const QObject *,const char *,const QObject *,const char *,Qt::ConnectionType)' : cannot convert parameter 1 from 'QGraphicsItem *' to 'const QObject *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Regards,
Steve

wysota
2nd October 2007, 16:31
Oh, yes in that case changing the type will help, because QGraphicsItem is not a QObject thus it can't be used with connect().