简体   繁体   English

QAction 不调用连接的插槽

[英]QAction doesn't call the connected slot

I have a Qt project with a QMainWindow, which has the following actions and slots:我有一个带有 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
};

I have initialized and connected the QAction and slot in the MainWindow's constructor:我已经在 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
}

When I run the application, the QAction newAct shows up in the menu bar, but when it is clicked, nothing happens.当我运行应用程序时,QAction newAct 出现在菜单栏中,但是当我单击它时,什么也没有发生。 The slot work fine, when it is invoked in another part of the code, so I know that the slot works fine.插槽工作正常,当它在代码的另一部分调用时,所以我知道插槽工作正常。 For some reason, I suspect that QAction being triggered isn't calling the NewGame() slot.出于某种原因,我怀疑被触发的 QAction 没有调用 NewGame() 插槽。

Is there anything I'm missing in here?我在这里有什么遗漏吗?

your code with the QAction , Shortcut and Connect looks fine so am suspecting of the slot newGame in the mainWindow I did try on my system with a lambda like您带有QActionShortcutConnect的代码看起来不错,所以我怀疑 mainWindow 中的插槽newGame我确实在我的系统上尝试过使用 lambda 之类的

connect(newAct, &QAction::triggered, []()
{
    qDebug()<< "Hello Action";
});

and I can see the HelloAction when clicking and when using the shortcut ctrl+N我可以在单击和使用快捷键 ctrl+N 时看到HelloAction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM