Custom context menu event not being caught for QToolButton
-
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.
-
@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.
-
@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!
-
-
@Terrence-Mitchem Why do you use a lambda here? It's not needed.
-
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 ofQtLauncher
. 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 ofthis
, the compiler will complain immediately. By writing it out, hopefully you see that the slot is from the classQtLauncher
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 aQPoint
. However, you are now handing it aQToolButton
. Try to connect directly if lambdas are not totally necessary.