QAction 不调用连接的插槽
QAction doesn't call the connected slot
我有一个带有 QMainWindow 的 Qt 项目,它有以下操作和插槽:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
private:
Ui::MainWindowClass ui;
//..... other code
QMenu* fileMenu;
QAction* newAct; //The concerned QAction*
public slots:
void newGame();//The concerned slot
//..... other code
};
我已经在 MainWindow 的构造函数中初始化并连接了 QAction 和插槽:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
//...... other code
newAct = new QAction(tr("&New Game"), this);
newAct->setShortcut(QKeySequence::New);
connect(newAct, &QAction::triggered, this, &MainWindow::newGame);
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
//..... other code
}
当我 运行 应用程序时,QAction newAct 显示在菜单栏中,但是单击它时没有任何反应。当在代码的另一部分调用时,插槽工作正常,所以我知道插槽工作正常。出于某种原因,我怀疑被触发的 QAction 没有调用 NewGame() 插槽。
这里有什么我遗漏的吗?
带有 QAction
、Shortcut
和 Connect
的代码看起来不错,所以我怀疑主窗口中的插槽 newGame
我确实在我的系统上尝试过像
这样的 lambda
connect(newAct, &QAction::triggered, []()
{
qDebug()<< "Hello Action";
});
我可以在单击和使用快捷键 ctrl+N
时看到 HelloAction
我有一个带有 QMainWindow 的 Qt 项目,它有以下操作和插槽:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = Q_NULLPTR);
private:
Ui::MainWindowClass ui;
//..... other code
QMenu* fileMenu;
QAction* newAct; //The concerned QAction*
public slots:
void newGame();//The concerned slot
//..... other code
};
我已经在 MainWindow 的构造函数中初始化并连接了 QAction 和插槽:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
//...... other code
newAct = new QAction(tr("&New Game"), this);
newAct->setShortcut(QKeySequence::New);
connect(newAct, &QAction::triggered, this, &MainWindow::newGame);
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
//..... other code
}
当我 运行 应用程序时,QAction newAct 显示在菜单栏中,但是单击它时没有任何反应。当在代码的另一部分调用时,插槽工作正常,所以我知道插槽工作正常。出于某种原因,我怀疑被触发的 QAction 没有调用 NewGame() 插槽。
这里有什么我遗漏的吗?
带有 QAction
、Shortcut
和 Connect
的代码看起来不错,所以我怀疑主窗口中的插槽 newGame
我确实在我的系统上尝试过像
connect(newAct, &QAction::triggered, []()
{
qDebug()<< "Hello Action";
});
我可以在单击和使用快捷键 ctrl+N
时看到 HelloAction