My problem is that when I click on an item in a QMenuBar, the corresponding slot gets called twice. I'm using Qt 4.8.1. I'm not using Qt Designer nor the "auto-connection" feature. Here is my code snippet :
#include <iostream>
#include <QWidget>
#include <QMenuBar>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0) : QWidget(parent)
{
QMenuBar *menu = new QMenuBar(this);
menu->addAction("Click here");
menu->addAction("Or here");
connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(handleAction(QAction*)));
}
public slots:
void handleAction(QAction *action)
{
std::cout << "Triggered" << std::endl;
}
};
And the main function :
#include "main.h"
#include <QApplication>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MyWidget w;
w.show();
return app.exec();
}
If you compile this (with the MOC file), you'll see that clicking on "Click here" will print "Triggered" once, and "Or here" twice. I don't understand why.
What am I doing wrong ?
QMenu
objects and adding them to theQMenuBar
. I suspect it's just sending the entire list of actions to your slot. – Bellbird