Signal / Slot from QGraphicsItem
Hi,
I have the following code declared in my QGraphicsScene derived class:
Code:
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
Re: Signal / Slot from QGraphicsItem
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
...
};
Re: Signal / Slot from QGraphicsItem
Yes, and make sure QObject is first in the base class list.
Also don't forget to add the Q_OBJECT macro.
Re: Signal / Slot from QGraphicsItem
Thanks Marcel, I did try that but still get the type conversion error.
This is my derived graphics item class :
Code:
{
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 );
}
{
// 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;
}
signals:
void ShowOptions();
protected:
{
emit ShowOptions();
}
{
}
};
and the full code that tries to do the signal/slot connection :
Code:
void CanScene
::AddBoxItem( int x,
int y,
QString strText, ITEMS item
) {
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
Re: Signal / Slot from QGraphicsItem
Because the pItem is actually only a QGraphicsItem. It does not now about your signal.
Change the type of pItem to CCanGraphicsItem.
Re: Signal / Slot from QGraphicsItem
Ooops :p
How I didn't notice that?!
Regards,
Steve
Re: Signal / Slot from QGraphicsItem
I don't think this is a problem, at least not during compile time.
steg90: What exactly is the error?
Re: Signal / Slot from QGraphicsItem
The error was most likely QObject::connect no such signal...etc
Re: Signal / Slot from QGraphicsItem
I wouldn't call that a conversion error :) I expected a "Cannot convert ..." message.
Re: Signal / Slot from QGraphicsItem
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
Re: Signal / Slot from QGraphicsItem
Oh, yes in that case changing the type will help, because QGraphicsItem is not a QObject thus it can't be used with connect().