Results 1 to 11 of 11

Thread: Signal / Slot from QGraphicsItem

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

    Default Signal / Slot from QGraphicsItem

    Hi,

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

    Qt Code:
    1. connect( pItem, SIGNAL(ShowOptions()),this,SLOT(ShowCanOptions()) );
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jul 2007
    Location
    Russia, Ul'yanovsk
    Posts
    20
    Qt products
    Qt4
    Platforms
    Windows
    Thanked 1 Time in 1 Post

    Default 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
    ...
    };
    Last edited by QCasper; 2nd October 2007 at 14:06.

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default 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.

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

    Default 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 :

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

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

    Qt Code:
    1. void CanScene::AddBoxItem( int x, int y, QString strText, ITEMS item )
    2. {
    3. QGraphicsItem* pItem = NULL;
    4.  
    5. switch( item )
    6. {
    7. case CAN :
    8. addItem( pItem = new CCanGraphicsItem( x, y, strText ) );
    9. QObject::connect( pItem, SIGNAL(ShowOptions()),this,SLOT(ShowCanOptions()) );
    10. break;
    11. // etc...
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Something I'm obviously doing wrong?!

    Regards,
    Steve

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default 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.

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

    Default Re: Signal / Slot from QGraphicsItem

    Ooops

    How I didn't notice that?!

    Regards,
    Steve

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

    Default 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?

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 541 Times in 521 Posts

    Default Re: Signal / Slot from QGraphicsItem

    The error was most likely QObject::connect no such signal...etc

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

    Default Re: Signal / Slot from QGraphicsItem

    I wouldn't call that a conversion error I expected a "Cannot convert ..." message.

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

    Default 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

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

    Default 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().

Similar Threads

  1. Replies: 2
    Last Post: 16th August 2007, 00:20
  2. templated signal or slot
    By QPlace in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2007, 11:50
  3. signal not getting communicated to slot
    By quickNitin in forum Qt Programming
    Replies: 17
    Last Post: 2nd June 2006, 04:56
  4. Manually send signal to slot
    By donmorr in forum Qt Programming
    Replies: 1
    Last Post: 29th May 2006, 15:03
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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
  •  
Qt is a trademark of The Qt Company.