I think you don't understand the concept of a "Singleton".
Basically, a singleton is an object that can only have a single instance. That instance can be used exactly like a global class with a static method in most cases.
So, you implement your Global class without a static function for doing the global stuff (it is just an ordinary member function, which must be called through an instance pointer). Every time you want to call that method, you get a pointer to the global instance of your class (using the getInstance() static method, and call it. Since that pointer is a genuine pointer to an instance, the doStuff() method can emit signals, get connected to slots, whatever.
// Global.h
{
Q_OBJECT
public:
static Global * getInstance();
public slots:
void doStuff();
signals:
void somethingChanged();
private:
virtual ~Global() = 0; // singleton can't be deleted by a caller of getInstance()
};
// Global.cpp
#include "Global.h"
static Global * theInstance = 0;
Global * Global::getInstance()
{
if ( theInstance == 0 )
theInstance = new Global();
return theInstance;
}
Global
:: Global( QObject * pParent
){
// Initialize whatever else needs to be done
}
void Global::doStuff();
{
// do what you need to do
emit somethingChanged();
}
// someOtherCode.cpp
#include "Global.h"
SomeOtherCode
::SomeOtherCode( QObject * pParent
){
connect( Global::getInstance(), SIGNAL( somethingChanged() ), this, SLOT( whoaGlobalStuffHappened() ) );
// Two ways to kill the same bird:
connect( somePushButton, SIGNAL( clicked() ), Global::getInstance(), SLOT( doStuff() ) );
connect( someOtherButton, SIGNAL( clicked() ), this, SLOT( doTheGlobalThing() ) );
}
void SomeOtherCode::doTheGlobalThing()
{
Global::getInstance()->doStuff();
}
// Global.h
class Global : public QObject
{
Q_OBJECT
public:
static Global * getInstance();
public slots:
void doStuff();
signals:
void somethingChanged();
private:
Global( QObject * pParent = 0 );
virtual ~Global() = 0; // singleton can't be deleted by a caller of getInstance()
};
// Global.cpp
#include "Global.h"
static Global * theInstance = 0;
Global * Global::getInstance()
{
if ( theInstance == 0 )
theInstance = new Global();
return theInstance;
}
Global:: Global( QObject * pParent )
: QObject( pParent )
{
// Initialize whatever else needs to be done
}
void Global::doStuff();
{
// do what you need to do
emit somethingChanged();
}
// someOtherCode.cpp
#include "Global.h"
SomeOtherCode::SomeOtherCode( QObject * pParent )
: QObject( pParent )
{
connect( Global::getInstance(), SIGNAL( somethingChanged() ), this, SLOT( whoaGlobalStuffHappened() ) );
// Two ways to kill the same bird:
connect( somePushButton, SIGNAL( clicked() ), Global::getInstance(), SLOT( doStuff() ) );
connect( someOtherButton, SIGNAL( clicked() ), this, SLOT( doTheGlobalThing() ) );
}
void SomeOtherCode::doTheGlobalThing()
{
Global::getInstance()->doStuff();
}
To copy to clipboard, switch view to plain text mode
All the same functionality as your Global class with the static method, except you get to use all the Qt signal / slot functionality. If "doStuff" needs to change some global properties, make them private member variables of the Global class and provide access functions for the app to get at their values after they have changed:
bool someConditionalValue = Global::getInstance()->conditionalValue();
bool someConditionalValue = Global::getInstance()->conditionalValue();
To copy to clipboard, switch view to plain text mode
Bookmarks