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. Custom context menu event not being caught for QToolButton

Custom context menu event not being caught for QToolButton

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 372 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.
  • Terrence MitchemT Offline
    Terrence MitchemT Offline
    Terrence Mitchem
    wrote on last edited by Terrence Mitchem
    #1

    I'm working with VS2022 and the official Qt extension

    I'm obviously doing something wrong connecting signals and slots. I would expect a right-click on the button to wind up in ShowContextMenu().

    Header file:

    #pragma once
    
    #include <QtWidgets/QMainWindow>
    #include <QtWidgets/QMenu.h>
    #include "ui_QtLauncher.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class QtLauncherClass; };
    QT_END_NAMESPACE
    
    class QtLauncher : public QMainWindow
    {
        Q_OBJECT
    
    public:
        QtLauncher(QWidget *parent = nullptr);
        ~QtLauncher();
    
        void ConfigureUI();
        void ShowContextMenu(const QPoint&);
    
    private:
        Ui::QtLauncherClass *ui;
    };
    

    Source file:

    #include "stdafx.h"
    #include "QtLauncher.h"
    
    /*
    #include <string>
    
    using namespace std;
    */
    
    QtLauncher::QtLauncher(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::QtLauncherClass())
    {
        ui->setupUi(this);
    }
    
    QtLauncher::~QtLauncher()
    {
        delete ui;
    }
    
    void QtLauncher::ConfigureUI()
    {
        // Borderless window
        setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    
        // Use a background bitmap
        QPixmap bkgnd("Images/background-01.jpg");
        bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);
        QPalette palette;
        palette.setBrush(QPalette::Window, bkgnd);
        setPalette(palette);
    
        // Make the settings button transparent and place text
        ui->SettingsButton->setStyleSheet("QToolButton { background - color: transparent; border: none; }");
        ui->SettingsButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
        
        // Use custom context menu for settings button
        ui->SettingsButton->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), ui->SettingsButton, SLOT(ShowContextMenu(const QPoint&)));
    
        return;
    }
    
    
    void QtLauncher::ShowContextMenu(const QPoint& p)
    {
        QMenu myMenu(ui->SettingsButton);
        myMenu.addAction("Menu Item 1");
    
        myMenu.show();
    
        return;
    }
    

    I'm not sure where I'm messing up.

    Christian EhrlicherC 1 Reply Last reply
    0
    • Terrence MitchemT Terrence Mitchem

      I'm working with VS2022 and the official Qt extension

      I'm obviously doing something wrong connecting signals and slots. I would expect a right-click on the button to wind up in ShowContextMenu().

      Header file:

      #pragma once
      
      #include <QtWidgets/QMainWindow>
      #include <QtWidgets/QMenu.h>
      #include "ui_QtLauncher.h"
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class QtLauncherClass; };
      QT_END_NAMESPACE
      
      class QtLauncher : public QMainWindow
      {
          Q_OBJECT
      
      public:
          QtLauncher(QWidget *parent = nullptr);
          ~QtLauncher();
      
          void ConfigureUI();
          void ShowContextMenu(const QPoint&);
      
      private:
          Ui::QtLauncherClass *ui;
      };
      

      Source file:

      #include "stdafx.h"
      #include "QtLauncher.h"
      
      /*
      #include <string>
      
      using namespace std;
      */
      
      QtLauncher::QtLauncher(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::QtLauncherClass())
      {
          ui->setupUi(this);
      }
      
      QtLauncher::~QtLauncher()
      {
          delete ui;
      }
      
      void QtLauncher::ConfigureUI()
      {
          // Borderless window
          setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
      
          // Use a background bitmap
          QPixmap bkgnd("Images/background-01.jpg");
          bkgnd = bkgnd.scaled(size(), Qt::IgnoreAspectRatio);
          QPalette palette;
          palette.setBrush(QPalette::Window, bkgnd);
          setPalette(palette);
      
          // Make the settings button transparent and place text
          ui->SettingsButton->setStyleSheet("QToolButton { background - color: transparent; border: none; }");
          ui->SettingsButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
          
          // Use custom context menu for settings button
          ui->SettingsButton->setContextMenuPolicy(Qt::CustomContextMenu);
          connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), ui->SettingsButton, SLOT(ShowContextMenu(const QPoint&)));
      
          return;
      }
      
      
      void QtLauncher::ShowContextMenu(const QPoint& p)
      {
          QMenu myMenu(ui->SettingsButton);
          myMenu.addAction("Menu Item 1");
      
          myMenu.show();
      
          return;
      }
      

      I'm not sure where I'm messing up.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Terrence-Mitchem said in Custom context menu event not being caught for QToolButton:

      I'm not sure where I'm messing up.

      connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), ui->SettingsButton, SLOT(ShowContextMenu(const QPoint&)));

      Your connect (the receiver) is wrong. You should use the pmf-style connect syntax to catch such errors.

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

      Terrence MitchemT 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @Terrence-Mitchem said in Custom context menu event not being caught for QToolButton:

        I'm not sure where I'm messing up.

        connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), ui->SettingsButton, SLOT(ShowContextMenu(const QPoint&)));

        Your connect (the receiver) is wrong. You should use the pmf-style connect syntax to catch such errors.

        Terrence MitchemT Offline
        Terrence MitchemT Offline
        Terrence Mitchem
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in Custom context menu event not being caught for QToolButton:

        @Terrence-Mitchem said in Custom context menu event not being caught for QToolButton:

        I'm not sure where I'm messing up.

        connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), ui->SettingsButton, SLOT(ShowContextMenu(const QPoint&)));

        Your connect (the receiver) is wrong. You should use the pmf-style connect syntax to catch such errors.

        The functor approach seems to have worked seems to have worked:

        connect(ui->SettingsButton, &QPushButton::customContextMenuRequested, [=] { ShowContextMenu(ui->SettingsButton); });
        

        Thanks for the pointer, since it lead me to the functor thing!

        Christian EhrlicherC 1 Reply Last reply
        0
        • Terrence MitchemT Terrence Mitchem has marked this topic as solved on
        • Terrence MitchemT Terrence Mitchem

          @Christian-Ehrlicher said in Custom context menu event not being caught for QToolButton:

          @Terrence-Mitchem said in Custom context menu event not being caught for QToolButton:

          I'm not sure where I'm messing up.

          connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), ui->SettingsButton, SLOT(ShowContextMenu(const QPoint&)));

          Your connect (the receiver) is wrong. You should use the pmf-style connect syntax to catch such errors.

          The functor approach seems to have worked seems to have worked:

          connect(ui->SettingsButton, &QPushButton::customContextMenuRequested, [=] { ShowContextMenu(ui->SettingsButton); });
          

          Thanks for the pointer, since it lead me to the functor thing!

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Terrence-Mitchem Why do you use a lambda here? It's not needed.

          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
          1
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            Starting from the old syntax, you should have written

            connect(ui->SettingsButton, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(ShowContextMenu(const QPoint&)));
            

            Because ShowContextMenu is a member function of QtLauncher. Somewhere in the output (when running the application) you should have seen that this connect didn't work.

            With the new syntax this becomes

            connect(ui->SettingsButton, &QToolButton::customContextMenuRequested, this, &QtLauncher::ShowContextMenu);
            

            If you try to put in ui->SettingsButton instead of this, the compiler will complain immediately. By writing it out, hopefully you see that the slot is from the class QtLauncher and thus needs an object of this types.

            Unless you have other changes in your code, your current solution should not work. You showed that ShowContextMenu expects a QPoint. However, you are now handing it a QToolButton. Try to connect directly if lambdas are not totally necessary.

            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