6

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 ?

pintoch
  • 2,293
  • 1
  • 18
  • 26
  • Sorry i did not compile your code and am not a qt wizard. But i looked over the example from nokia's qt pages ( http://qt-project.org/doc/qt-4.8/mainwindows-menus.html ) and it seems to me that they connect the triggered signal to some slot in a different way: while you connect SIGNAL(triggered(QAction*)) they connect SIGNAL(triggered()). Can't tell if it makes a difference but this is a comment and not an answer :-) – Matthias May 06 '12 at 20:16
  • @Matthias: they connect the actions to the slots directly in those examples, pintoch is connection QMenuBar's signal, which is supposed to work as he intends it to. (Creating & connecting the actions directly would be a workaround, but not nice - he/she would need more slots or a signal mapper...) – Mat May 06 '12 at 20:24
  • It's unclear what you're *trying* to do here, but I suspect it's something different that you think - This won't actually even produce a menubar on OSX. You should be creating `QMenu` objects and adding them to the `QMenuBar`. I suspect it's just sending the entire list of actions to your slot. – Brian Roach May 06 '12 at 20:25
  • @BrianRoach: it's not. The same action is triggered twice on the second menu (on Linux/Qt 4.7 anyway). Same sender too. – Mat May 06 '12 at 20:42
  • Related to [pyQt: radioButton.isChecked() is executed twice](https://stackoverflow.com/questions/36808257/pyqt-radiobutton-ischecked-is-executed-twice) – Evandro Coan May 19 '20 at 20:25

2 Answers2

5

Use Qt::UniqueConnection to solve:

connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(handleAction(QAction*)), Qt::UniqueConnection);

http://doc.qt.io/qt-4.8/qt.html#ConnectionType-enum

jesterjunk
  • 2,342
  • 22
  • 18
rlnunes
  • 281
  • 3
  • 10
2

I get the same incorrect result as you on Windows 7 x64 using Qt 4.8.1. This certainly seems like a bug.

There was a bug reported and fixed for what appears to be the same behavior on Mac OS X. Although it has been closed, there is a single comment on it that they observed this problem on Windows 7.

I think filing a new bug report would be a good idea.

jesterjunk
  • 2,342
  • 22
  • 18
Arnold Spence
  • 21,942
  • 7
  • 74
  • 67