2011-06-03 46 views
-1

这里是一段代码:的QThread不退出

class ServerThread : public QThread { 
    Q_OBJECT 
public: 
    ServerThread(); 
    void executeCommand(const char *command); 
private: 
    Server server; 
}; 

void Server::executeCommand(const char *command) { 
    QString qCommand = command; 
    if (qCommand == "close") { 
     closeServer(); 
     emit serverClosed(); 
    } else if (true) { 

    } 
} 

ServerThread::ServerThread() { 
    connect(&server, SIGNAL(serverClosed()), this, SLOT(quit())); 
} 

void ServerThread::executeCommand(const char *command) { 
    server.executeCommand(command); 
} 

信号serverClosed()被emmited但ServerThread似乎不退出()。为什么?

+0

我假设,既然你说ServerThread没有'quit()',你在调用'start()'的地方? – HostileFork 2011-06-03 19:12:45

+0

是的。我读过它是因为 server.start(); std :: string命令; do std :: getline(std :: cin,command); server.executeCommand(command.c_str()); } while(true); 但我怎样才能创建一个输入循环而不锁定主线程? – OneMoreVladimir 2011-06-03 19:17:52

+0

另外:你的意图是executeCommand在ServerThread的线程中运行?如果你只是把它变成一个普通的成员函数,它就会在调用者的线程中运行......你需要它在一个插槽中。 – HostileFork 2011-06-03 19:18:13

回答

0

请阅读以下文章了解QThread的工作原理:Threads, Events and QObjects。正如Colin评论过的,您至少需要start()和moveToThread()。

+0

moveToThread是“错误的”... http:// labs.qt.nokia.com/2010/06/17/youre-doing-it-wrong/ – 2011-06-04 12:25:43

+0

否:moveToThread(this)是错误的。 – hmuelner 2011-06-05 06:35:47