QSocketNotifier:只能与以 QThread 错误开头的线程一起使用
QSocketNotifier: Can only be used with threads started with QThread error
我正在尝试使用QLocalServer作为IPC解决方案。qt 的版本是 4.6
这是我的主要.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:只能与以QThread错误开头的线程一起使用。
我尝试将整个东西包装在 QThread 中,但我仍然收到同样的错误。
谁能解释一下我做错了什么,或者为什么甚至涉及一个线程?
错误消息具有误导性。你需要一个Qt事件循环才能使用QSocketNotifier。在您的应用程序中执行此操作的适当方法是创建一个 QApplication(或者如果您不需要任何图形内容,则创建 QCoreApplication)。您的主要内容应如下所示:
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QServer test();
app.exec();
return 0;
}
QCoreApplication::exec() 启动事件循环(替换while (true) {}
循环)。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 在C++中使用cURL和多线程
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在cuda线程之间共享大量常量数据
- 如何将元素添加到数组的线程安全函数?
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 异常属于C++中的线程还是进程
- C++中的线程安全删除
- C++使用params创建线程函数会导致转换错误
- 类与私有变量的其他类之间的线程安全性
- CoInitialize()在单独的线程上崩溃而不返回
- c++中的线程池
- 线程之间的布尔停止信号
- 为什么std::async使用同一个线程运行函数
- 用于矢量处理的多个线程
- C++为线程工作动态地分割例程
- QSocketNotifier:只能与以 QThread 错误开头的线程一起使用