PDA

View Full Version : Context menu / connect with parameters



pietrom
18th February 2009, 17:49
I have a problem with context menus. I would like to create one in a dynamic way, i.e. passing a set of string variables:



void MyClass::contextMenu(std::vector<QAction*>& labelsAct)
{
for(size_t i = 0; i < labelsAct.size(); ++i)
{
QObject::connect(labelsAct[i], SIGNAL(mySignal(int)),
this, SLOT(doSomething(int)));
}
}


This is the message I get at runtime:


Object::connect: No such signal QAction::mySignal(int)

doSomething(int) is defined in the MyClass class.
mySignal(int) is declared in the MyClass class; why does it look for the signal in QAction?

If I remove the int parameters from those function (definitions), the connect works correctly. But I need those parameters.

Thank you...

spirit
18th February 2009, 18:20
QAction doesn't has such signal
try to do next


...
connect(this, SIGNAL(mySignal(int)), SLOT(doSomething(int)));
...

void MyClass::contextMenu(std::vector<QAction*>& labelsAct)
{
for(size_t i = 0; i < labelsAct.size(); ++i)
{
QAction *action = labelsAct[i];
action->setProperty("myValue", i);
QObject::connect(action, SIGNAL(triggered()),
this, SLOT(mySlot()));
}
}

void MyClass::mySlot()
{
QAction *action = qobject_cast<QAction *>(sender());
if (!action)
return;
int myValue = action->property("myValue").toInt();
emit mySignal(myValue);
}