Solved &Action and &MayApp::Slot() : How to pass arguments
-
Hi to all Qt experts.
In my app I have this:
void MyApp::on_listWidget_customContextMenuRequested(const QPoint &pos) { listWidgetMenu->popup(ui->listWidget->viewport()->mapToGlobal(pos)); connect(action_SetColor, &QAction::triggered, this, &MyApp::slot_SetColor); }
List widget and action are defined at the program start. So far everything is fine.
But because I try to pass "pos" to a Slot which was trigged by an action without argument the compiler complains:.../Qt4/5.15.2/clang_64/lib/QtCore.framework/Headers/qobject.h:255: error: static_assert failed due to requirement 'bool((QtPrivate::CheckCompatibleArguments<QtPrivate::List<bool>, QtPrivate::List<QPoint> >::value))' "Signal and slot arguments are not compatible."
I have tried this approach (from another thread):
connect(action_SetColor, &QAction::triggered, this, // <- control object [this](QPoint& pos) {MyApp::slot_SetColor(pos);});
But I get the same error message.
-
Then let's simplify this: just connect your action's triggered signal after you create them.
There's no need for that convoluted logic you are trying to implement with the right click position. In the slot you can request the current selected item and apply your code on it.
-
@ademmler said in &Action and &MayApp::Slot() : How to pass arguments:
How can I pass "pos" or how can I change the trigger to match ...
Use a lambda
-
Hi Christian- like this? The same error.
connect(action_SetColor, &QAction::triggered, this, // <- control object [this](QPoint& pos) {SpectraProof::slot_SetColor(pos);});
-
Hi,
First some questions:
- Why are you re-connecting that action each time a contextual menu is created ?
- How is this position related to the slot ?
-
- Its linked to an list widget item.
- In another thread this was meant to be the only option ;-)
Ofcourese I can put it also to app start section ...
-
- but you are just reconnecting the exact same action again and again.
- It's not really clear.
Can you maybe describe what you want to achieve between the contextual menu and slot_SetColor ?
From pure guess, your menu seems to be some sort of color picker, if so, I fail to see what the position of the right click comes into play with that slot.
-
@SGaist Ofcourse I can:
In my main window I have a listWidget. This has a list of colors.
I want to do a right click on those items -> this opens a list of possible commands:At app start I define:
action_SetColor = new QAction(tr("Set color"), this); action_SetOpaque = new QAction(tr("Set opaque"), this); action_AddColor = new QAction(tr("Set opaque"), this); listWidgetMenu=new QMenu(this); listWidgetMenu->addAction(action_SetColor); listWidgetMenu->addAction(action_SetOpaque); listWidgetMenu->addAction(action_AddColor);
I have activated contextMenue in QtCReator for this listWwidget and I have set a slot:
void MyApp::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
//This let the listwidet with commands appear
listWidgetMenu->popup(ui->listWidget->viewport()->mapToGlobal(pos));And now I need to start the "action" chosen from this list passing the position ...
I need the position for the following action to know which list item has been clicked.
} -
can you tell me, what's wrong with my lamda - please?
-
Then let's simplify this: just connect your action's triggered signal after you create them.
There's no need for that convoluted logic you are trying to implement with the right click position. In the slot you can request the current selected item and apply your code on it.
-
@SGaist Sounds good - hence I was miss leaded ... I try this and let you know. thx in advance
-
@SGaist thx for simplifying my life ;-)
-
@ademmler said in &Action and &MayApp::Slot() : How to pass arguments:
can you tell me, what's wrong with my lamda - please?
Capturing parameters in lambdas does not work like you're doing:
connect(action_SetColor, &QAction::triggered, this, // <- control object [pos, this]() {SpectraProof::slot_SetColor(pos);});