Qt 主窗口菜单信号

发布于 2024-12-17 22:15:12 字数 1455 浏览 3 评论 0原文

我有处理 QMainWindow 的“Core”对象。
Core.h 代码

class Core : public QObject
{
    Q_OBJECT
public:
    explicit Core(QObject *parent = 0);
    ~Core();
    void appInit();
    int getAuth();

public slots:
    void appExit();

private slots:
    void appMenuTriggered(QAction *action);

private:
    void preInit();
    MainWindow *mwnd;
};

Core.cpp 代码

Core::Core(QObject *parent) : QObject(parent)
{
    qDebug() << "Core::Constructor called";
    preInit();
}

Core::~Core()
{
    delete mwnd;
    qDebug() << "Core::Destructor called";
}

int Core::getAuth()
{
    LoginDialog *login = new LoginDialog();
    int r = login->exec();
    delete login;
    return r;
}

void Core::appExit() // connected to qapplication aboutToQuit
{
    qDebug() << "Core::appExit called";
}

void Core::preInit()  // called after getAuth im main.cpp
{
    qDebug() << "Core::preInit called";
}

void Core::appMenuTriggered( QAction *action )
{
    qDebug() << "action triggered";
}

void Core::appInit()
{
    mwnd = new MainWindow();
    mwnd->show();
    qDebug() << "Core::appInit called";
}

我正在尝试将 mainwindow 菜单栏信号连接到核心插槽,如下所示:

connect(mwnd->menuBar(), SIGNAL(triggered()), this, SLOT(appMenuTriggered()));

但它不起作用。我是 C++ 和 Qt 新手。这个要怎么连接呢? 或者也许有更好的方法来处理主窗口对其他程序部分的操作。

UPD 问题解决了。忘记包含 QMenuBar

I've "Core" object that handles QMainWindow.
Core.h code

class Core : public QObject
{
    Q_OBJECT
public:
    explicit Core(QObject *parent = 0);
    ~Core();
    void appInit();
    int getAuth();

public slots:
    void appExit();

private slots:
    void appMenuTriggered(QAction *action);

private:
    void preInit();
    MainWindow *mwnd;
};

Core.cpp code

Core::Core(QObject *parent) : QObject(parent)
{
    qDebug() << "Core::Constructor called";
    preInit();
}

Core::~Core()
{
    delete mwnd;
    qDebug() << "Core::Destructor called";
}

int Core::getAuth()
{
    LoginDialog *login = new LoginDialog();
    int r = login->exec();
    delete login;
    return r;
}

void Core::appExit() // connected to qapplication aboutToQuit
{
    qDebug() << "Core::appExit called";
}

void Core::preInit()  // called after getAuth im main.cpp
{
    qDebug() << "Core::preInit called";
}

void Core::appMenuTriggered( QAction *action )
{
    qDebug() << "action triggered";
}

void Core::appInit()
{
    mwnd = new MainWindow();
    mwnd->show();
    qDebug() << "Core::appInit called";
}

I'm trying to connect mainwindow menubar signal to core slot like this:

connect(mwnd->menuBar(), SIGNAL(triggered()), this, SLOT(appMenuTriggered()));

But it doesn't work. Im new to c++ and Qt. How to connect this?
Or maybe there is better way to handle mainwindow actions to other programm parts.

UPD
Problem solved. Forget to include QMenuBar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2024-12-24 22:15:12

您必须在 SIGNAL 和 SLOT 参数中给出完整的函数规范(但没有参数名称)。像这样:

connect(mwnd->menuBar(),
        SIGNAL(triggered(QAction*)),
        this,
        SLOT(appMenuTriggered(QAction*)));

如果您在 Qt Creator 中调试此类代码,当 connect 函数找不到信号或插槽时,它会将诊断错误消息写入“应用程序输出”窗格。我建议您在解决问题之前先找到这些错误消息,以便知道将来要查找的位置。信号和槽很容易出错!

You have to give the full function spec in the SIGNAL and SLOT parameters (but without the argument names). Like this:

connect(mwnd->menuBar(),
        SIGNAL(triggered(QAction*)),
        this,
        SLOT(appMenuTriggered(QAction*)));

If you debug such code in Qt Creator, the connect function will write diagnostic error messages to the Application Output pane when it doesn't find a signal or a slot. I suggest that you find these error messages before you fix your problem, so that you know where to look in future. It's very easy to get signals and slots wrong!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文