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();
}
}
最后在这里调用了 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()));
reader->start();
}
请帮忙
Hi
i am implementing a simple threaded GUI application in QT 4.6.2. I am using QThread without subclassing it. I have made a call to the usleep() function in my start() function this however results in freezing of the GUI. How do i get around this. Below is the code
#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
Below is the start() function
void ecgreader::start()
{
int i= system("ls>output.txt");
SLEEP(10000);
if(i==0)
{
emit finished();
}
}
finally the call to start is made here
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();
}
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有两个问题:首先您创建了线程,但从未启动它。其次,您直接在阅读器上调用 start() 而不是发出信号。
我认为你的意思是调用 thread->start() 而不是 reader->start():
You have two problems: first you created the thread, but you never started it. Second you are directly calling start() on your reader instead of emiting a signal.
I think what you meant was to call thread->start() instead of reader->start():