Is there a way to differentiate between which QAction's triggered() signal called a given slot? I have an application that creates its menus dynamically and I don't know at compile time how many there are going to be (hence the dynamic nature of the problem). I know that in Java I can determine the actionCommand or menu item identifier that called a given handler. Is there a mechanism in place to do this in Qt? ... and if so, where in the online help will it tell me?
Qt Labs Forum » General discussions
Multiple QAction signals to a single slot ...
(2 posts)-
Posted: 1 month #
-
Figured it out. Turned out to be much simpler than I expected. You simply need to subclass QAction and provide a more detailed signal/slot message than you would normally have (with connecting to triggered() in QAction):
class QA2 : public QAction { Q_OBJECT private: QString command; public: // implement all of QAction's ctors... QA2(QObject* parent) : QAction() { connectMe(); } . . . void connectMe() { connect(this, SIGNAL(triggered()), this, SLOT(triggered())); } QString getCommand() { return this->command; } signals: void triggered(QA2* qa2); private slots: void triggered() { emit(triggered(this)); } };You then can connect the menu action to whatever signal/slot you like:
QMenu* menu = new QMenu("&Menu"); QA2* barfu = new QA2(tr("&Barfu"), menu); barfu->setCommand(tr("print 'goodbye'")); barfu->setShortcut(tr("Ctrl+B")); connect(barfu, SIGNAL(triggered(QA2*)), this, SLOT(handleMenuAction(QA2*))); menu->addAction(edit_Barfu);Your class simply implements some form of handleMenuAction(QA2*) that then can have more information about which menu item was called.
Simple!
P.S. This forum SERIOUSLY needs a "Preview before post" button
Posted: 1 month #
Reply
You must log in to post.