how to pass an argument through a signal
Added by Norman Hampel almost 2 years ago
Hi everyone,
I'm trying to pass an argument to the setColor() function in the following snippet:
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/black.png","black",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/grey.png","grey",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/red.png","red",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/orange.png","orange",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/yellow.png","yellow",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/pink.png","pink",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/purple.png","purple",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/blue.png","blue",this, &APP::setColor);
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/green.png","green",this, &APP::setColor);
I've searched the FAQ and this forum but haven't found a solution yet. SignalMapper would only work if I created a new MenuItem for every single color. I'm still hopeful there is a way to just pass a single argument to setColor() such as for example:
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/red.png","red",this, &APP::setColor("red"));
...
setColor(std::string color)
{
new WImage("resources/colorIcons/" + color + ".png", container);
}
A single function for every color is, of course, not practical but neither is SignalMapper with an array of MenuItems for every such menu (plus all these lines of code for mapping the signals) when the above snippet could somehow do it with the arguments added.
I'd much appreciate a suggestion and thanks in advance!
Norman
Replies
RE: how to pass an argument through a signal - Added by U. Artie Eoff almost 2 years ago
It looks like your using Ext::Menu. But, that's irrelevant. Anyhow, try using boost::bind like so:
#include <boost/bind.hpp>
...
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/black.png", "black");
colorItem_->activated().connect(boost::bind(&APP::setColor, boost::ref(*this), "black"));
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/grey.png", "grey");
colorItem_->activated().connect(boost::bind(&APP::setColor, boost::ref(*this), "grey"));
...
Note, also, that you will not be able to pass the boost::bind result as an argument to the Menu::addItem function as it has an incompatible signature. But passing to the activated().connect function should be no problem since it is a boost::signal already and inherently designed to be compatible.
Good Luck,
U.Artie
RE: how to pass an argument through a signal - Added by Wim Dumon almost 2 years ago
That's correct.
Additionally, I added boost::bind support to Ext::Menu, so that you can now write it in one line:
colorItem_ = chooseColorMenu->addItem("resources/colorIcons/black.png","black", boost::bind(&APP::setColor, this, "black"));
This will appear in the git version soon and in the next release.
W.
RE: how to pass an argument through a signal - Added by Norman Hampel almost 2 years ago
Cheers mate! I´ll give it a go tomorrow but it sure sounds promising. You´re right I´m using an Ext::Menu; it seemed the only/best way for a drop-down list with images in them. Of course, I´d much rather have a more advanced way of selecting a color (i.e. more advanced than merely presenting a small choice of preset colors) but that´ll do for now. Made a rookie very happy ;) thanks again.
RE: how to pass an argument through a signal - Added by Wim Dumon almost 2 years ago
I once integrated jscolor in Wt. That's not too much work for a nice color picker...