【发布时间】:2020-11-18 15:33:59
【问题描述】:
我有一个带有 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和slot:
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() 槽。
这里有什么我遗漏的吗?
【问题讨论】:
-
你能检查
connect(newAct, &QAction::triggered, this, &MainWindow::newGame);的返回值是真还是假。如果它为假,那么您的连接无效。
标签: c++ qt signals-slots qaction