Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, May 2003
Can static classes emit signals?


Message 1 in thread

Hello,
can classes which only consists of static functions emit signals? (Qt 3.1)
Thx,
Florian


Message 2 in thread


I would suspect not, but a singleton can.

-Craig.


On Fri, 2003-05-09 at 09:17, Florian Lindner wrote:
> Hello,
> can classes which only consists of static functions emit signals? (Qt 3.1)
> Thx,
> Florian
> 
> --
> List archive and information: http://lists.trolltech.com/qt-interest/
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part


Message 3 in thread

Am Freitag, 9. Mai 2003 19:04 schrieb Craig W. Wright:
> I would suspect not, but a singleton can.
>
> -Craig.
>
> On Fri, 2003-05-09 at 09:17, Florian Lindner wrote:
> > Hello,
> > can classes which only consists of static functions emit signals? (Qt
> > 3.1) Thx,

Hey,
would you explain what a singleton is?
Thx,
Florian


Message 4 in thread


A singleton is a class that can only be instantiated one time. A header
file may look like this:

class MySingleton : public QWidget
{
      Q_OBJECT;
   public:
      static MySingleton* instance();
      //- Return the only instance of this class.

      ~MySingleton();
      //- Destructor.

   signals:
      void didIt();
      //- Singnal to say we "didIt"

   private:
      MySingleton();
      //- A private constructor.

      MySingleton(MySingleton const&);
      void operator=(MySingleton const&);
      //- Declare these away.

      static MySingleton* instancePtr_;
};

While the source file may look like this.

MySingleton* MySingleton::instancePtr_ = NULL;


MySingleton* MySingleton::instance()
{
   if ( NULL == instancePtr_ )
   { 
      instancePtr_ = new MySingleton();
   }
   return instancePtr_;
}

MySingleton::MySingleton() :
	QWidget(NULL, "mysingleton")
{
    // do whatever has to be done.
}

MySingleton::~MySingleton()
{
   // deallocate memory
   instancePtr_ = NULL;
}


I don't know if this will compile... I just wrote it in the mail editor.
It's close though.

You can see that the only way to get an instance is by calling
instance() and if you delete it, instancePtr will become NULL, which
will give you a new instance next time you call instance().

You can make the destructor private and create a friend class that will
destroy the singleton instance upon exit time. I'll leave that as an
exercise for you. :)

There are alot of ways to implement singleton. One of which is a slick
way using templates, but I don't know the details. Even if I did I'd
hesitate using it because template support on some compilers is not up
to standard. Template support in some compilers has been very poor in my
opinion.

-Craig.




On Fri, 2003-05-09 at 11:33, Florian Lindner wrote:
> Am Freitag, 9. Mai 2003 19:04 schrieb Craig W. Wright:
> > I would suspect not, but a singleton can.
> >
> > -Craig.
> >
> > On Fri, 2003-05-09 at 09:17, Florian Lindner wrote:
> > > Hello,
> > > can classes which only consists of static functions emit signals? (Qt
> > > 3.1) Thx,
> 
> Hey,
> would you explain what a singleton is?
> Thx,
> Florian
> 
> --
> List archive and information: http://lists.trolltech.com/qt-interest/
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part


Message 5 in thread

Another solution for you might be the QSignal class -- check the QT 
documentation on it.

-John

On Friday 09 May 2003 11:17, Florian Lindner wrote:
> Hello,
> can classes which only consists of static functions emit signals? (Qt 3.1)
> Thx,
> Florian
>
> --
> List archive and information: http://lists.trolltech.com/qt-interest/


Message 6 in thread

> can classes which only consists of static functions emit signals? (Qt 3.1)

I guess yes, as long as you inherit from QObject ;-)

Cheers, Kuba Ober