【发布时间】:2017-11-16 04:15:55
【问题描述】:
我收到一个错误:
非静态成员函数的使用无效
基本上,每当我收到消息时,我都需要更新线程中的 UI:
if (pthread_create(&thread, NULL, MainWindow::thread_message, &sock))
{
perror("error in creating the thread");
// return EXIT_FAILURE;
}
void *MainWindow::thread_message(void *arg)
{
// UI update, whenever I receive a message from the server.
}
【问题讨论】:
-
Qt的基本规则,GUI不应该从不同的线程更新到主线程,检查以下链接:doc.qt.io/qt-4.8/…,也不建议使用C风格的线程, Qt 提供了更友好的方式来完成诸如 QRunnable、QThreadPool、QtConcurrent、QThread 之类的任务:doc.qt.io/qt-5/threads-technologies.html
-
这个错误是因为pthread_create需要静态方法而thread_message不需要。
标签: c++ qt pthreads posix non-static