PDA

View Full Version : how to know which is the class that sends the signal in qt ?



sliverTwist
23rd April 2013, 12:41
My question is about how to know which is the class that sends the signal because what i want to do is specified to the class that has sent the signal
is it possible to do that ?
this is a prototype of what i intend to do


if (signal comes from class 1)
{do specified actions }
else if (comes from class 2)
{do something else }

Thanks

Santosh Reddy
23rd April 2013, 12:48
To answer yor question yes, use QObject::sender() and then cast it in to whatever class you want.

But basing the logic on the sending object is not considered as a good design, so try not to base your logic on the sender.

May be you could use QSignalMapper instead and base your logic on the formal slot parameter.

sliverTwist
23rd April 2013, 12:55
i know the method QSignalMapper but don't konw how to use it with this code

connect( ui->welcome, SIGNAL(changeStackedWidgetIndex(int)),
ui->stackedWidget, SLOT(setCurrentIndex(int)) );
connect( ui->credentials, SIGNAL(changeStackedWidgetIndex(int)),
ui->stackedWidget, SLOT(setCurrentIndex(int)) );
connect( ui->config, SIGNAL(changeStackedWidgetIndex(int)),
ui->stackedWidget, SLOT(setCurrentIndex(int)) );
connect( ui->syncwindow, SIGNAL(changeStackedWidgetIndex(int)),
ui->stackedWidget, SLOT(setCurrentIndex(int)) );
connect( ui->loaduser, SIGNAL(changeStackedWidgetIndex(int)),
ui->stackedWidget, SLOT(setCurrentIndex(int)) );

wysota
23rd April 2013, 13:21
Why would you want to use it with this code?

Santosh Reddy
23rd April 2013, 13:22
QSignalMapper * signalMapper = QSignalMapper(this);

connect(ui->welcome, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
signalMapper->setMapping(ui->welcome, 0);

connect(ui->credentials, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
signalMapper->setMapping(ui->credentials, 1);

connect(ui->config, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
signalMapper->setMapping(ui->config, 2);

connect(ui->syncwindow, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
signalMapper->setMapping(ui->syncwindow, 3);

connect(ui->loaduser, SIGNAL(changeStackedWidgetIndex()), signalMapper, SLOT(map()) );
signalMapper->setMapping(ui->loaduser, 4);

connect(signalMapper, SIGNAL(mapped(int)), this, SIGNAL(setCurrentIndex(int)));