我正在尝试使用QT 4.6制作简单的GUI。我做了一个代表菜单栏的分离类:

MenuBar::MenuBar()
{
    aboutAct = new QAction(tr("&About QT"), this);
    aboutAct->setStatusTip(tr("Show the application's About box"));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));

    quitAct = new QAction(tr("&Quit"),this);
    quitAct->setStatusTip(tr("Exit to the program"));
    //connect(quitAct, SIGNAL(triggered()), &QApp, SLOT(quit()));

    menuFile = new QMenu("File");
    menuFile->addAction(quitAct);

    menuLinks = new QMenu("Links");

    menuAbout = new QMenu("Info");
    menuAbout->addAction(aboutAct);


    addMenu(menuFile);
    addMenu(menuLinks);
    addMenu(menuAbout);
}


我无法将quitAct的信号与主应用程序的退出插槽连接,可能是因为它在MenuBar类中不可见。

//connect(quitAct, SIGNAL(triggered()), &QApp, SLOT(quit()));


我该怎么做?

最佳答案

你有错字:)

在:connect(quitAct, SIGNAL(triggered()), &QApp, SLOT(quit()));

变量的名称是qApp,而不是QApp。顺便说一句,balpha说了一切。所以是:

connect(quitAct, SIGNAL(triggered()), qApp, SLOT(quit()));
要么
connect(quitAct, SIGNAL(triggered()), QApplication::instance(), SLOT(quit()));

09-27 19:12