qthread 中的睡眠调用会阻塞 UI 线程

发布于 2024-11-09 03:16:22 字数 1077 浏览 0 评论 0原文

你好 我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-11-16 03:16:22

您有两个问题:首先您创建了线程,但从未启动它。其次,您直接在阅读器上调用 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();
}

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():

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文