【发布时间】:2012-01-06 13:31:47
【问题描述】:
这是一个最小的测试用例,我尝试使用 QThread 启动本地域服务器,因此 UI 不应该卡住。但是当它开始时,我看到了来自 qDebug() 的Listening 输出,但是从表单编辑器添加的小部件完全消失了,一切都变慢了(例如调整窗口大小),如果我删除thread.start(),UI 会显示出来并且运行良好.
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect (&thread , SIGNAL(started()) , SLOT(setupServer()));
thread.start();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setupServer()
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
// create socket .. and create socket file ..
// bind ...
// listen ..
qDebug() << "Listening ..";
while((connection_fd = ::accept(socket_fd,
(struct sockaddr *) &address,
&address_length)) > -1)
{
qDebug() << "Got an connection.";
::close (connection_fd);
}
// close socket and remove the socket file
}
【问题讨论】:
标签: sockets unix networking qt4 qthread