我想在不使用QCoreApplication
的异步信号/插槽的应用程序中运行QLocalServer
。据我所知,这应该使用QEventLoop
:
void MyThread::startSocketServer()
{
m_server = new QLocalServer();
m_server->listen("ExamplePipe");
/*
// This works:
m_server->waitForNewConnection(-1);
auto socket = m_server->nextPendingConnection();
socket->write("Test"); */
// This signal is never triggered
connect(m_server, &QLocalServer::newConnection, this, [this]()
{
auto socket = m_server->nextPendingConnection();
connect(socket, &QLocalSocket::readyRead, this, [this, socket]()
{
//socket->readAll();
//usleep(1000);
//socket->write("Test");
});
});
}
void MyThread::run()
{
QEventLoop eventLoop;
startSocketServer();
eventLoop.exec();
}
同步版本按预期工作。有没有一种方法可以使用QLocalServer
的异步方式?
编辑:这里有一个基本版本:
void MyThread::run()
{
QEventLoop eventLoop;
m_server = new QLocalServer();
m_server->listen("ExamplePipe");
connect(m_server, &QLocalServer::newConnection, &eventLoop, []()
{
// not triggered
});
eventLoop.exec();
}
问候,
我开始工作了:
void MyThread::run()
{
int argc = 0;
char *argv[] = {NULL};
m_qtApplication = new QCoreApplication(argc, argv);
m_server = new QLocalServer();
m_server->listen("ExamplePipe");
connect(m_server, &QLocalServer::newConnection, m_qtApplication, []()
{
// works
});
m_qtApplication->exec();
}