I want to execute my slot with parameter when dynamically created QAction clicked, but I can't add my variables when creating QAction in QMenu, and default triggered()
slot can't pass it.
To be more clear, I want to archieve something like this:
connect(someAction, SIGNAL( triggered(MyClass*) ), this, SLOT( execute(MyClass*) );
How I can get this? I tried to create custom QAction, but I don't know how to add it to QMenu - there is no function like addAction(QAction)
.
You can store your parameter in the action itself as a QVariant
using QAction::setData()
function. For example:
QVariant v = qVariantFromValue((void *) yourClassObjPointer);
action->setData(v);
In the slot you will have to extract the pointer like:
void execute()
{
QAction *act = qobject_cast<QAction *>(sender());
QVariant v = act->data();
YourClass yourPointer = (YourClass *) v.value<void *>();
}
Gather your dynamic QAction
's in one QActionGroup
using QAction::setActionGroup()
Use QAction::setData()
to store the custom data in each QAction
.
connect
QActionData
's signal triggered(QAction*)
to some slot
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With