PDA

View Full Version : QSignalMapper question: SIGNAL 2 int's



vonCZ
4th July 2007, 13:28
Is it possible to use QSignalMapper to send 2 int's from a QAction? Here's my code:

QPushButton *pButton = new QPushButton;
QSignalMapper *mapper = new QSignalMapper(pButton2);
mapper->setMapping(Action1, 3);
connect(Action1, SIGNAL(triggered()), mapper, SLOT(map()));
connect(mapper, SIGNAL(mapped(int)), coinWidget, SLOT(cTexture(int)));

I'd like to send two or more int's to cTexture(), but apparently the mapped() signal can only take one parameter.

vonCZ
4th July 2007, 13:48
i thought of a way around it: my int values are between 0-999. If the two ints I want to send are 7 and 3, for example, i could send a string, 007003, and parse/convert to ints in my cTexture() function. I suspect there's a much better way, however.

wysota
4th July 2007, 15:49
I think it might prove faster to have a controller slot that checks which action was triggered and emits another signal with arguments that depend on the action. It's exactly the same what the signal mapper does, only that it's better fit to your application.

vonCZ
4th July 2007, 17:20
I think it might prove faster to have a controller slot that checks which action was triggered and emits another signal with arguments that depend on the action. It's exactly the same what the signal mapper does, only that it's better fit to your application.
gotcha, thanks. I just set it up and it works.


pB_terrain = new QPushButton(tr("&Terrain"));
pB_terrain->setCheckable(true);
pB_terrain>setChecked(true);
connect(pB_terrain, SIGNAL(clicked()), this, SLOT(terrain_sl()));
connect(this, SIGNAL(terrain_sig(int, int)), coinWidget, SLOT(terrain_sl(int, int)));
.
.
.

void Window::terrain_sl()
{
QPushButton *action = (QPushButton *)sender();
emit terrain_sig(7,3);
}

my question is: will I need a separate slot function like terrain_sl() for each of the QPushButtons (and QActions), or is there a way that I can evaluate which button/Action sent the signal, and call the appropriate emit function depending on that.

wysota
4th July 2007, 23:15
I think the code you pasted (especially line #12) already answers that question...

vonCZ
20th July 2007, 10:02
I think the code you pasted (especially line #12) already answers that question...

ok. I see sender() "returns a pointer to the object that sent the signal"... This is what I did (with QSignalMapper objects instead of QPushButtons...):


//
lyImgAcMapper[0] = new QSignalMapper(button1);
lyImgAcMapper[1] = new QSignalMapper(button2);
lyImgAcMapper[2] = new QSignalMapper(button3);

// i set their mapping/etc. then connect them to a custom SLOT

connect(lyImgAcMapper[0], SIGNAL(mapped(int)), this, SLOT(lyImgSL(int)));
connect(lyImgAcMapper[1], SIGNAL(mapped(int)), this, SLOT(lyImgSL(int)));
connect(lyImgAcMapper[2], SIGNAL(mapped(int)), this, SLOT(lyImgSL(int)));

// the lyImgSL(int) slot looks like this

void Window::lyImgSL(int value)
{
QSignalMapper *sigmap = qobject_cast<QSignalMapper *>(sender()); //from Rajesh's post on another thread
if(sigmap==lyImgAcMapper[0])
{
emit lyImgSG(0,value);
}
else if(sigmap==lyImgAcMapper[1])
{
emit lyImgSG(1,value);
}
else
{
emit lyImgSG(2,value);
}
}

// lyImgSG is then connected to another slot



...um, this is working for me so far, although the truth is I only have one lyImgAcMapper[] array at the moment. Does this seem proper/efficient?