我有一个带有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()插槽。
这里有我遗漏的东西吗?
发布于 2020-07-29 06:25:27
您使用QAction、Shortcut和Connect的代码看起来很好,因此我怀疑在系统中使用lambda (如
connect(newAct, &QAction::triggered, []()
{
qDebug()<< "Hello Action";
});在单击和使用快捷方式HelloAction时,可以看到ctrl+N。
https://stackoverflow.com/questions/63146908
复制相似问题