PDA

View Full Version : How to emit signal from static method?



kusumat
20th July 2011, 09:44
Hi I have tried to emit signal from static method in this way .




MainWidget* pThis;
MainWidget *MainWidget:: _instance = 0;

MainWidget::MainWidget(QWidget *parent):QWidget(parent)
{
pThis = this;
}
MainWidget* MainWidget::getInstance()
{
if(!_instance)
_instance = new MainWidget();
return _instance;
}
void MainWidget :: emitMySignal(){
qDebug()<<"emitMySignal";
pThis->emit mySignal ();
}
MainWidget::~MainWidget()
{

}


and the call to static function and signal slot connection is here


MainWidget::getInstance()->emitMySignal();
qDebug()<<"after emitCall back....";
QObject::connect(MainWidget::getInstance(),SIGNAL( mySignal()),this,SLOT(connectToSlot()));


But this is not happening for me .
Plz suggest

mcosta
20th July 2011, 09:48
you connected the slot after emitted the signal.

try with



QObject::connect(MainWidget::getInstance(),SIGNAL( mySignal()),this,SLOT(connectToSlot()));
MainWidget::getInstance()->emitMySignal();
qDebug()<<"after emitCall back....";


what is the static method??

Santosh Reddy
20th July 2011, 09:54
pThis = this; //pThis is global, don't do this, you never know what it contains unless, ctor as private

void MainWidget :: emitMySignal(){ //I assume this is non static function, if not make it non-static function
qDebug()<<"emitMySignal";
//pThis->emit mySignal ();
emit mySignal(); //this will work
}

high_flyer
20th July 2011, 10:17
You can't emit signal from static methods, because static methods are not part of an objects instance, they are global, in the scope of the class, which means, you don't have a "sender" object for them.
You could send an event though.