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 { Q_OBJECT public: Ser(QWidget *parent = nullptr); ~Ser(); private: 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); setMenuBar(pMenuBar); m_AboutMenu = new QMenu("About", this); a_AboutSerBlock = new QAction("Ser Block", this); a_AboutQt = new QAction("Qt", this); m_AboutMenu->addAction(a_AboutSerBlock); m_AboutMenu->addAction(a_AboutQt); this->menuBar()->addMenu(m_AboutMenu); connect(a_AboutSerBlock, SIGNAL(triggered()), this, SLOT(on_AboutSerBlock())); connect(a_AboutQt, SIGNAL(triggered()), this, SLOT(on_AboutQt())); m_ExitMenu = new QMenu("Exit", this); this->menuBar()->addMenu(m_ExitMenu); connect(m_ExitMenu, SIGNAL(triggered()), this, SLOT(on_Quit())); } Ser::~Ser() {} void Ser::on_AboutSerBlock(void) { qDebug() << "About SerBlock"; } void Ser::on_AboutQt(void) { QMessageBox::aboutQt(this); } void Ser::on_Quit(void) { QApplication::quit(); }
Only AboutQt works.
Can you help me?
Thanks. -
@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: https://doc.qt.io/qt-5/qmenu.html#triggered - Hint: it has a parameter. -
To avoid such errors please us the new signal slot syntax: https://doc.qt.io/qt-5/signalsandslots.html
-
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); this->menuBar()->addMenu(m_Exit); //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?
Thaks. -
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 :)