You can use composition instead of inheritance (so the mainWindow or dialog instance "contains" a notifier object).
If you want to stick to inheritance, then the "Notifier" class does not have to be QObject-based:
// helper class used to maintain the connections
class NotifierHelper
: public QObject{ Q_OBJECT
signals:
void error(Errors::ErrorCodes errorCode);
void warning(Warnings::WarningCodes warningCode);
};
// "error reporting" base class, not based on QObject
class Notifier
public:
void error(Errors::ErrorCodes errorCode){
this->_sender.error(errorCode); // you can invoke signals directly, like method calls (in fact they are methods)
}
void addErrorReceiver
(QObject * receiver,
const char * signal_or_slot
){ QObject::connect(&_sender,
SIGNAL(error
(Errors
::ErrorCodes)), receiver, signal_or_slot
);
}
private:
NotifierHelper _sender;
};
class Compressor : public Notifier
{
Q_OBJECT
public:
void functionWithError(){
SomeReceiver receiver;
this->addErrorReceiver(&receiver, SLOT(receiveError(ErrorCodes)));
// now the 'receiver' object will get the error signal
this->error(Errors::SomeErrorCode);
}
}
// helper class used to maintain the connections
class NotifierHelper : public QObject{
Q_OBJECT
signals:
void error(Errors::ErrorCodes errorCode);
void warning(Warnings::WarningCodes warningCode);
};
// "error reporting" base class, not based on QObject
class Notifier
public:
void error(Errors::ErrorCodes errorCode){
this->_sender.error(errorCode); // you can invoke signals directly, like method calls (in fact they are methods)
}
void addErrorReceiver(QObject * receiver, const char * signal_or_slot){
QObject::connect(&_sender, SIGNAL(error(Errors::ErrorCodes)), receiver, signal_or_slot);
}
private:
NotifierHelper _sender;
};
class Compressor : public Notifier
{
Q_OBJECT
public:
void functionWithError(){
SomeReceiver receiver;
this->addErrorReceiver(&receiver, SLOT(receiveError(ErrorCodes)));
// now the 'receiver' object will get the error signal
this->error(Errors::SomeErrorCode);
}
}
To copy to clipboard, switch view to plain text mode
There exists quite a lot of object-oriented programming languages without the macro expansion feature. Just imagine how would you solve this problem in one of these languages.
Bookmarks