Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Signal & Slots problem on menus
QtWS25 Last Chance

Signal & Slots problem on menus

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 777 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • S Stefanoxjx

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      7
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        To avoid such errors please us the new signal slot syntax: https://doc.qt.io/qt-5/signalsandslots.html

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        7
        • S Offline
          S Offline
          Stefanoxjx
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • S Offline
              S Offline
              Stefanoxjx
              wrote on last edited by
              #6

              Many thanks for help.
              Now I understand :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved