qthread中的睡眠调用阻止了UI线程

| 你好 我正在QT 4.6.2中实现一个简单的线程GUI应用程序。我正在使用QThread而不对其进行子类化。我在start()函数中调用了usleep()函数,但这导致GUI冻结。我该如何解决。下面是代码
#ifndef ECGREADER_H
#define ECGREADER_H
#include<QObject>
class ecgreader : public QObject
{
    Q_OBJECT

public:
    ecgreader(QObject *parent=0);
    ~ecgreader();
public Q_SLOTS:
    void start();
Q_SIGNALS:
    void finished();
};
#endif // ECGREADER_H
下面是start()函数
void ecgreader::start()
{
   int i= system(\"ls>output.txt\");
   SLEEP(10000);
   if(i==0)
   {
       emit finished();
   }
}
终于在这里打起电话了
void Application::onbtnclicked()
{
    QThread* thread=new QThread;
    ecgreader* reader=new ecgreader;
    reader->moveToThread(thread);
    connect(thread,SIGNAL(started()),reader,SLOT(start()));
    connect(reader,SIGNAL(finished()),thread,SLOT(quit()));
    connect(reader,SIGNAL(finished()),reader,SLOT(deleteLater()));
    connect(reader,SIGNAL(finished()),thread,SLOT(deleteLater()));
    reader->start();
}
请帮忙     
已邀请:
您有两个问题:首先,您创建了线程,但从未启动它。其次,您直接在阅读器上调用start()而不发出信号。 我认为您的意思是调用thread-> start()而不是reader-> start():
void Application::onbtnclicked()
{
    QThread* thread=new QThread;
    ecgreader* reader=new ecgreader;
    reader->moveToThread(thread);
    connect(thread,SIGNAL(started()),reader,SLOT(start()));
    connect(reader,SIGNAL(finished()),thread,SLOT(quit()));
    connect(reader,SIGNAL(finished()),reader,SLOT(deleteLater()));
    connect(reader,SIGNAL(finished()),thread,SLOT(deleteLater()));
    thread->start();
}
    

要回复问题请先登录注册