触发时无法将 QAction 链接到函数 (Qt 5)

Unable to link QAction to a function when triggered (Qt 5)

本文关键字:函数 Qt 链接 QAction      更新时间:2023-10-16

我正在尝试将上下文菜单添加到系统托盘(可通过单击系统托盘图标激活(我已经成功添加了菜单和一个带有文本"退出"的操作,但是我不知道如何将操作"触发"功能链接到另一个功能/更改触发功能或其他任何可以工作的东西。我只是想在单击操作时激活某些特定行为。此操作按钮在我单击它时不执行任何操作。我尝试使用此构造函数将其链接到成员函数:QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = ...)这是我代码中最重要的部分:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mSystemTrayIcon = new QSystemTrayIcon(this);
mSystemTrayIcon->setIcon(QIcon(":/iris_logo.png"));
mSystemTrayIcon->setVisible(true);
systemTrayMenu = new QMenu("Context menu");
systemTrayMenu->setToolTipsVisible(true);
// I get the error: no matching member function for call to 'addAction'
systemTrayMenu->addAction("Open", this, on_actionQuit_triggered())); 
// I dont get an error, however this only creates a menu button, not its corresponding function that must be called.
systemTrayMenu->addAction("Exit"); 
mSystemTrayIcon->setContextMenu(systemTrayMenu);
}

addAction返回指向QAction对象的指针,获取此指针并使用connect在信号triggeredon_actionQuit_triggered插槽之间建立连接:

QAction* openAction = systemTrayMenu->addAction("Open"); 
connect (openAction, SIGNAL(triggered()) , this, SLOT(on_actionQuit_triggered()));