c – Qt 5.1.0中是否仍支持QCoreApplication的aboutToQuit()信号?

该程序在打开.txt文件时启动.退出后,我希望它关闭.txt文件.以下是代码段:

MYRELAYSERVER:

void MyRelayServer::exitHandler()
{
    qDebug() << mFileName << " closed!" << mTcpPort;
    if (mDataLog)
        mTextStream << mFileName << " closed!" << mTcpPort;
    mFile.close();

}

主要:

#include <QCoreApplication>
#include "myrelayserver.h"
#include <QDebug>
#include <QObject>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    MyRelayServer server(9999);
    QObject::connect(&a, SIGNAL(aboutToQuit()), &server, SLOT(exitHandler()));
    return a.exec();
}

但是,它不执行exitHandler.

它也不会显示来自QCoreApplication a的信号的任何智能感知.

最佳答案 有同样的问题.

不知道原因,但改变一个指针固定它.

#include <QCoreApplication>
#include "myrelayserver.h"
#include <QDebug>
#include <QObject>


int main(int argc, char *argv[])
{
    QCoreApplication *a = new QCoreApplication (argc, argv);
    MyRelayServer server(9999);
    QObject::connect(a, SIGNAL(aboutToQuit()), &server, SLOT(exitHandler()));
    return a->exec();
}

它至少在自动填充中可用(在Qt Creator中).

点赞