如何将 QAction 从 QMenu 传递到 Qt 插槽

How to pass a QAction to a Qt slot from a QMenu

本文关键字:Qt 插槽 QMenu QAction      更新时间:2023-10-16

我是Qt的新手,我有问题如何将QAction作为参数传递,就像下面的代码:

connect(fileToolBarAct, SIGNAL(toggled(bool)), this, SLOT(ToggleBar(fileToolBarAct));

这是我的老虎机功能:

void MainWindow::ToggleBar(QAction& what)
{
    what.isCheckable();
}
QObject::connect不是

这样工作的。不能将对象传递给宏SIGNALSLOT宏。 SIGNALSLOT宏应采用函数签名。此外,the signature of a signal must match the signature of the receiving slotQt文档中所述。

发现您缺乏对信号和插槽机制的理解,我建议您阅读Qt信号和插槽文档以获取更多信息。阅读Qt Signals and Slots文档将为您清除所有内容。

onnect(fileToolBarAct, SIGNAL(toggled(bool)), this, SLOT(ToggleBar(bool));

void MainWindow::ToggleBar(bool checked)
{
    QAction* action = qobject_cast<QOAction*>(sender());
    if (action) 
         action->setChecked(checked);
}