Important: Please read the Qt Code of Conduct -

Signal & Slots problem on menus

  • Hi, I've a problem with Signals & Slots in menus.
    I don't understand why it don't work.

    Thi is the header file:

    #ifndef SER_H
    #define SER_H
    #include <QMenu>
    #include <QAction>
    #include <QToolBar>
    #include <QString>
    #include <QMainWindow>
    class Ser : public QMainWindow
        Ser(QWidget *parent = nullptr);
        const QString     APPNAME       = "Serial Block";
        const QString     APPVERSION    = "0.1b";
        QMenu       *m_AboutMenu;
        QAction     *a_AboutSerBlock;
        QAction     *a_AboutQt;
        QMenu       *m_ExitMenu;
    private slots:
        void    on_Quit            (void);
        void    on_AboutSerBlock    (void);
        void    on_AboutQt         (void);
    #endif // SER_H

    and this is the code file:

    #include <QApplication>
    #include <QMenuBar>
    #include <QScreen>
    #include <QRect>
    #include <QGuiApplication>
    #include <QMessageBox>
    #include <QFileInfo>
    #include <QDebug>
    #include "ser.h"
    Ser::Ser(QWidget *parent) : QMainWindow(parent)
        QMenuBar *pMenuBar = new QMenuBar(this);
        m_AboutMenu = new QMenu("About", this);
        a_AboutSerBlock = new QAction("Ser Block", this);
        a_AboutQt = new QAction("Qt", this);
        connect(a_AboutSerBlock, SIGNAL(triggered()), this, SLOT(on_AboutSerBlock()));
        connect(a_AboutQt, SIGNAL(triggered()), this, SLOT(on_AboutQt()));
        m_ExitMenu = new QMenu("Exit", this);
        connect(m_ExitMenu, SIGNAL(triggered()), this, SLOT(on_Quit()));
    void Ser::on_AboutSerBlock(void)
        qDebug() << "About SerBlock";
    void Ser::on_AboutQt(void)
    void Ser::on_Quit(void)

    Only AboutQt works.
    Can you help me?

  • Lifetime Qt Champion

    @Stefanoxjx said in Signal & Slots problem on menus:

    connect(m_ExitMenu, SIGNAL(triggered()), this, SLOT(on_Quit()));

    This line should actually trigger a warning at runtime as there is no such signal in QMenu.
    Correct signal is: - Hint: it has a parameter.

  • Lifetime Qt Champion

    To avoid such errors please us the new signal slot syntax:

  • Hi, thanks for your help.
    Now I corrected the problem and works fine.
    But, I would like to ask you why in this code, only signals aboutToHide aboutToShow works?

        m_Exit = new QMenu("Exit", this);
        //connect(m_Exit, SIGNAL(aboutToShow()), this, SLOT(on_Quit())); //Work
        connect(m_Exit, SIGNAL(aboutToHide()), this, SLOT(on_Quit()));     //Work
       //connect(m_Exit, SIGNAL(hovered(QAction *)), this, SLOT(on_Quit(QAction *))); //Doesn't work
       //connect(m_Exit, SIGNAL(triggered(QAction *)), this, SLOT(on_Quit(QAction *))); //Doesn't work

    While with hovered and triggered doesn't works.
    With hovered and triggered the prototype of slot is with parameters: void on_Quit(QAction *action);
    and method is the same void Ser::on_Quit(QAction *action);
    For this specific case, is aboutToShow the correct signal?

  • Lifetime Qt Champion

    Again: please use the new signal/slot syntax to see if the signal and slot match during build time.

  • Many thanks for help.
    Now I understand :)

Log in to reply