Results 1 to 3 of 3

Thread: How to use connect() with a singleton class?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2007
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Question How to use connect() with a singleton class?

    Hi,

    I made a Qt singleton class:

    Qt Code:
    1. // decl
    2. class QFoo : QObject
    3. {
    4. Q_OBJECT
    5. public:
    6. static QFoo* getInstance( QObject* parent=0 );
    7. static void destroy();
    8.  
    9. signal:
    10. void fooSignal();
    11.  
    12. private:
    13. static QFoo* p_instance;
    14.  
    15. QFoo( QObject* parent );
    16. ~QFoo();
    17. };
    18.  
    19. // impl
    20. QFoo* QFoo::p_instance = 0;
    21.  
    22. QFoo* QFoo::getInstance( QObject* parent )
    23. {
    24. if( !p_instance )
    25. p_instance = new QFoo( parent );
    26. return p_instance;
    27. }
    28.  
    29. void QFoo::destroy()
    30. {
    31. if( p_instance )
    32. delete p_instance;
    33. }
    34.  
    35. QFoo::QFoo( QObject* parent ) : QObject( parent )
    36. {
    37. }
    38.  
    39. QFoo::~QFoo()
    40. {
    41. }
    To copy to clipboard, switch view to plain text mode 

    I want to connect its fooSignal() to a slot:
    Qt Code:
    1. QFoo* pFoo = QFoo::getInstance();
    2. connect( pFoo, SIGNAL( fooSignal() ), this, SLOT( aSlot() ) );
    To copy to clipboard, switch view to plain text mode 

    My problem is that I get compiler error with the connect:
    "error C2243: 'type cast'*: conversion from 'QFoo *' to 'const QObject *' exists, but is not accessible"

    Do you have any idea?
    Thanks in advance.

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Wiki edits
    1

    Default Re: How to use connect() with a singleton class?

    If you modify:

    Qt Code:
    1. class QFoo : QObject
    2. {
    3. ....
    4. };
    To copy to clipboard, switch view to plain text mode 

    to
    Qt Code:
    1. class QFoo : public QObject
    2. {
    3. ....
    4. };
    To copy to clipboard, switch view to plain text mode 
    Does it work?

  3. The following user says thank you to saa7_go for this useful post:

    punkypogo (30th July 2010)

  4. #3
    Join Date
    Oct 2007
    Posts
    22
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: How to use connect() with a singleton class?

    Oh yes thanks a lot, I guess I'm not completely awoken

Similar Threads

  1. Qt Singleton Class
    By alexismedina in forum Newbie
    Replies: 3
    Last Post: 22nd June 2010, 17:03
  2. Replies: 8
    Last Post: 9th May 2010, 17:43
  3. Object instance in a singleton class dissapears
    By vieraci in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2009, 00:51
  4. Replies: 12
    Last Post: 18th September 2008, 16:04
  5. function 'connect' always look super class for slots.
    By Intaek Lim in forum Qt Programming
    Replies: 7
    Last Post: 12th March 2007, 14:38

Tags for this Thread

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.