我正在尝试使用QLocalServer作为进程间通信解决方案。qt版本为4.6
这是我的main.cpp:
int main(int argc, const char*argv[]) {
QServer test();
while (true) {
}
}这是我的QServer类:
class QServer : public QObject
{
Q_OBJECT
public :
QServer ();
virtual ~QServer();
private :
QLocalServer* m_server;
QLocalSocket* m_connection;
private slots:
void socket_new_connection();
};
QServer::QServer()
{
m_server = new QLocalServer(this);
if (!m_server->listen("DLSERVER")) {
qDebug() << "Testing";
qDebug() << "Not able to start the server";
qDebug() << m_server->errorString();
qDebug() << "Server is " << m_server->isListening();
}
connect(m_server, SIGNAL(newConnection()),
this, SLOT(socket_new_connection()));
}
void
QServer::socket_new_connection()
{
m_connection = m_server->nextPendingConnection();
connect(clientConnection, SIGNAL(readyRead()),
this, SLOT(newData(clientConnection)));
}这一切都可以编译,但是在运行时,当我尝试连接newConnection()时,我得到了一个QSocketNotifier: Can be be used with QThread with QThread error。
我试着把整个东西包装在一个QThread中,但是我还是得到了同样的错误。
有没有人能解释我做错了什么,或者为什么会涉及到一个线程?
发布于 2012-12-21 01:19:04
该错误消息具有误导性。为了使用QSocketNotifier,你需要一个Qt事件循环。在您的应用程序中实现这一点的合适方法是创建一个QApplication (或者,如果您不想要任何图形内容,可以创建一个QCoreApplication)。你的main应该看起来像这样:
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QServer test();
app.exec();
return 0;
}exec()启动事件循环(替换您的while (true) {}循环)。
https://stackoverflow.com/questions/13888061
复制相似问题