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. Signals and Slots with QMenu and QMessageBox
QtWS25 Last Chance

Signals and Slots with QMenu and QMessageBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwidgetqmenuqmenubarqmessageboxsignals & slots
7 Posts 4 Posters 1.8k 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.
  • D Offline
    D Offline
    DenizCaglar
    wrote on 5 Jun 2023, 18:23 last edited by
    #1

    I've recently started off with Qt and I'm in the process of writing a Qt Widgets app.

    I first wanted to make a menu bar with a hamburger menu so I wrote my own component for it which is basically just an instance of QMenu with a Hamburger Menu Icon.

    For some reason I haven't been able to get signals and slots working with this setup. I wanted to make a QMessageBox appear when the QMenu item gets clicked on or when its triggered() signal is emitted.

    Here's my code for the HamburgerMenu class
    hamburgermenu.h

    #ifndef HAMBURGERMENU_H
    #define HAMBURGERMENU_H
    
    #include <QMenu>
    #include <QMainWindow>
    
    class HamburgerMenu : public QMenu
    {
        Q_OBJECT
    public:
        HamburgerMenu();
        void setupMenu(QMenuBar *parent);
    };
    
    Q_DECLARE_METATYPE(HamburgerMenu);
    
    #endif // HAMBURGERMENU_H
    

    and hamburgermenu.cpp

    #include "hamburgermenu.h"
    #include <QMenuBar>
    
    HamburgerMenu::HamburgerMenu()
    {
        //setting up QMenu object with QIcon resource
        this->setIcon(QIcon(":/icons/hamburger_menu_icon.svg"));
        return;
    }
    
    void HamburgerMenu::setupMenu(QMenuBar *parent){
    
        // adding QMenu to parent QMenuBar
        parent->addMenu(this);
        return;
    }
    

    and here's what I do in my QMainWindow constructor to set this stuff up.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        , hamburger_menu(new HamburgerMenu)
        , alert_box(new QMessageBox)
    {
        ui->setupUi(this);
        hamburger_menu->setupMenu(this->menuBar());
    
        // setting up an example message box to tests signals/slots
        alert_box->setText("You just clicked the burger menu! :)");
    
        // register HamburgerMenu as metatype?
        qRegisterMetaType<HamburgerMenu>();
    
        // connecting the menu signal to the message box slot
        bool ret = connect(hamburger_menu,&HamburgerMenu::triggered,this,&MainWindow::hamburger_clicked);
    
        if (!ret)  // connect returned false, must have failed
            qDebug("Connect failed on hamburger menu signal");
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
        delete hamburger_menu;
        delete alert_box;
    }
    
    
    void MainWindow::hamburger_clicked(QAction *action) {
        alert_box->exec();
        qDebug("The hamburger clicked slot was called!");
    }
    
    

    I would appreciate any help in figuring out what's wrong. Thank you in advance

    J 1 Reply Last reply 5 Jun 2023, 18:43
    0
    • D DenizCaglar
      5 Jun 2023, 18:23

      I've recently started off with Qt and I'm in the process of writing a Qt Widgets app.

      I first wanted to make a menu bar with a hamburger menu so I wrote my own component for it which is basically just an instance of QMenu with a Hamburger Menu Icon.

      For some reason I haven't been able to get signals and slots working with this setup. I wanted to make a QMessageBox appear when the QMenu item gets clicked on or when its triggered() signal is emitted.

      Here's my code for the HamburgerMenu class
      hamburgermenu.h

      #ifndef HAMBURGERMENU_H
      #define HAMBURGERMENU_H
      
      #include <QMenu>
      #include <QMainWindow>
      
      class HamburgerMenu : public QMenu
      {
          Q_OBJECT
      public:
          HamburgerMenu();
          void setupMenu(QMenuBar *parent);
      };
      
      Q_DECLARE_METATYPE(HamburgerMenu);
      
      #endif // HAMBURGERMENU_H
      

      and hamburgermenu.cpp

      #include "hamburgermenu.h"
      #include <QMenuBar>
      
      HamburgerMenu::HamburgerMenu()
      {
          //setting up QMenu object with QIcon resource
          this->setIcon(QIcon(":/icons/hamburger_menu_icon.svg"));
          return;
      }
      
      void HamburgerMenu::setupMenu(QMenuBar *parent){
      
          // adding QMenu to parent QMenuBar
          parent->addMenu(this);
          return;
      }
      

      and here's what I do in my QMainWindow constructor to set this stuff up.

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
          , hamburger_menu(new HamburgerMenu)
          , alert_box(new QMessageBox)
      {
          ui->setupUi(this);
          hamburger_menu->setupMenu(this->menuBar());
      
          // setting up an example message box to tests signals/slots
          alert_box->setText("You just clicked the burger menu! :)");
      
          // register HamburgerMenu as metatype?
          qRegisterMetaType<HamburgerMenu>();
      
          // connecting the menu signal to the message box slot
          bool ret = connect(hamburger_menu,&HamburgerMenu::triggered,this,&MainWindow::hamburger_clicked);
      
          if (!ret)  // connect returned false, must have failed
              qDebug("Connect failed on hamburger menu signal");
      
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
          delete hamburger_menu;
          delete alert_box;
      }
      
      
      void MainWindow::hamburger_clicked(QAction *action) {
          alert_box->exec();
          qDebug("The hamburger clicked slot was called!");
      }
      
      

      I would appreciate any help in figuring out what's wrong. Thank you in advance

      J Offline
      J Offline
      JonB
      wrote on 5 Jun 2023, 18:43 last edited by JonB 6 May 2023, 18:45
      #2

      @DenizCaglar
      The signal/slot is (correctly) for a QAction getting triggered. But you have not added any.QActions, those are the menu items a user clicks on. See e.g. https://doc.qt.io/qt-6/qmenu.html#actions and https://doc.qt.io/qt-6/qaction.html#qaction-in-widget-applications or there must be Qt menu examples around.

      D 1 Reply Last reply 5 Jun 2023, 18:59
      0
      • J JonB
        5 Jun 2023, 18:43

        @DenizCaglar
        The signal/slot is (correctly) for a QAction getting triggered. But you have not added any.QActions, those are the menu items a user clicks on. See e.g. https://doc.qt.io/qt-6/qmenu.html#actions and https://doc.qt.io/qt-6/qaction.html#qaction-in-widget-applications or there must be Qt menu examples around.

        D Offline
        D Offline
        DenizCaglar
        wrote on 5 Jun 2023, 18:59 last edited by
        #3

        @JonB Do I'm slightly confused on the difference between QAction, QMenu.

        I thought that QMenu was the items that we placed on the QMenuBar. They also have the triggered() signal. Why can't I just connect a slot to that?

        And if I were to add a QAction, would it create a submenu item under my QMenu or is it going to be analogous to using an anchor tag in html where it won't be visible but make the component clickable?

        M 1 Reply Last reply 5 Jun 2023, 19:12
        0
        • D DenizCaglar
          5 Jun 2023, 18:59

          @JonB Do I'm slightly confused on the difference between QAction, QMenu.

          I thought that QMenu was the items that we placed on the QMenuBar. They also have the triggered() signal. Why can't I just connect a slot to that?

          And if I were to add a QAction, would it create a submenu item under my QMenu or is it going to be analogous to using an anchor tag in html where it won't be visible but make the component clickable?

          M Offline
          M Offline
          mpergand
          wrote on 5 Jun 2023, 19:12 last edited by
          #4

          @DenizCaglar
          You should use a QToolButton for that as i'm doing here:

          D 1 Reply Last reply 5 Jun 2023, 19:17
          0
          • M mpergand
            5 Jun 2023, 19:12

            @DenizCaglar
            You should use a QToolButton for that as i'm doing here:

            D Offline
            D Offline
            DenizCaglar
            wrote on 5 Jun 2023, 19:17 last edited by DenizCaglar 6 May 2023, 19:23
            #5

            @mpergand Just looked into QToolButton and QToolBar, that seems to fit better to my usecase I will try using that. Also I can't really see your example of QToolButton. Did you mean to send a link or some code?

            M 1 Reply Last reply 5 Jun 2023, 20:51
            0
            • D DenizCaglar
              5 Jun 2023, 19:17

              @mpergand Just looked into QToolButton and QToolBar, that seems to fit better to my usecase I will try using that. Also I can't really see your example of QToolButton. Did you mean to send a link or some code?

              M Offline
              M Offline
              mpergand
              wrote on 5 Jun 2023, 20:51 last edited by mpergand 6 May 2023, 21:01
              #6

              @DenizCaglar said in Signals and Slots with QMenu and QMessageBox:

              Also I can't really see your example of QToolButton. Did you mean to send a link or some code?

              First you create a QMenu:

              // menu options
                  QMenu* menu=new QMenu(this);
                  QAction *action;
                  action=menu->addAction(tr("About..."));
                  action->setData(ToolMenu_About);
                  action->setCheckable(false);
                  menu->addSeparator();
              
                  action=menu->addAction(tr("Preferences..."));
                  action->setData(ToolMenu_Prefs);
                  action->setCheckable(false);
                  //QShortcut* shortcut= new QShortcut(QKeySequence(QKeySequence::Preferences),this);
                  //connect(shortcut,SIGNAL(activated()),this,SLOT(applicationShortcut()));
                  action->setMenuRole(QAction::PreferencesRole);
                  menu->addSeparator();
              ....
              

              then connect all the actions:

              connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(optionMenuAction(QAction*)));
              

              it's old code i'm using old connect syntax, use the new one.

              then add the menu to the tool button:

              toolButton->setMenu(menu);
              
              1 Reply Last reply
              0
              • S Offline
                S Offline
                SimonSchroeder
                wrote on 7 Jun 2023, 07:11 last edited by SimonSchroeder 6 Jul 2023, 07:11
                #7

                Well, Qt has both a QMenu and a QMenuItem. Only the QMenuItem is supposed to trigger actions. The QMenu itself only has QMenuItems and submenus. So, its action is already defined.

                Usually, the QMenuItems have QActions associated with it. For the QMenu::triggered signal the documentation states:

                This signal is emitted when an action in this menu is triggered.

                To me this reads that you have several entries inside the QMenu and one of these entries triggers an action. This is also why this signal contains the QAction as a parameter. My understanding would mean that clicking on the QMenu itself does not emit a triggered signal, but only clicking on a QMenuItem does.

                1 Reply Last reply
                0

                1/7

                5 Jun 2023, 18:23

                • Login

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