Thanks to this thread, I discovered QSignalMapper, and found this nice example in Qt's doc:
{
for (int i = 0; i < texts.size(); ++i) {
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}
connect(signalMapper,
SIGNAL(mapped
(QString)),
setLayout(gridLayout);
}
ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
: QWidget(parent)
{
signalMapper = new QSignalMapper(this);
QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}
connect(signalMapper, SIGNAL(mapped(QString)),
this, SIGNAL(clicked(QString)));
setLayout(gridLayout);
}
To copy to clipboard, switch view to plain text mode
My question is: can I use the above approach with custom context menus, thus?
class MyCustomMenu
: QMenu { /* do as above except map (QAction*, actionText) through signalMapper */
};
class MyCustomMenu : QMenu {
/* do as above except map (QAction*, actionText) through signalMapper */
};
To copy to clipboard, switch view to plain text mode
Would this work? If not this, what do most people do when you have a long list (say, more than 5-8) of context menu actions?
Thanks for your insightful comments.
Added after 1 40 minutes:
As it happens, my Google search before my posting was not useful. It's funny how juxtaposing a few words in Google search fetches a different set of hits ... this link clearly illustrates what I want to do, that too with the function pointer idiom, which is better, except if you have overloading. Even with overloading it is not at all bad.
Still I would like the opinion of Qt experts on the choice of idioms. Thanks.
Bookmarks