c++ - Qt - 使用由按钮控制的线程的简单示例

标签 c++ qt qthread

我一直在尝试让这个简单的示例使用由按钮激活的线程来工作。它基于以下问题的解决方案:

How to implement frequent start/stop of a thread (QThread)

上面的示例解决方案和我下面的代码之间的主要区别是:

  1. 我使用了 QWidget 而不是 MainWindow
  2. 为了清楚起见,我更改了信号的名称
  3. 我的代码包含调试信息
  4. 我尝试消除 worker 创建的信号,因为它们似乎没有做任何事情

开始/停止信号似乎没有触发它们相应的插槽,但我没有足够的经验来解决原因。

此外,我不确定信号的用途:

SignalToObj_mainThreadGUI()

那只是可以使用而不能使用的东西吗?

一段时间以来,我一直在尝试让这段代码正常工作,因此非常感谢任何帮助。

main.cpp

#include "threadtest.h"
#include <QApplication>

int main(int argc, char *argv[]) 
{
    QApplication a(argc, argv);
    ThreadTest w;
    w.show();

    return a.exec(); 
}

线程测试.h

#include <QWidget>
#include <QThread>
#include "worker.h"     

namespace Ui 
{
    class ThreadTest;
}

class ThreadTest : public QWidget
{
    Q_OBJECT

public:
    explicit ThreadTest(QWidget *parent = 0);
    ~ThreadTest();

signals:
    void startWorkSignal();
    void stopWorkSignal();


private slots:
    void on_startButton_clicked();
    void on_stopButton_clicked();

private:
    Ui::ThreadTest *ui;
    worker *myWorker;
    QThread *WorkerThread;
};

线程测试.cpp

#include "threadtest.h"
#include "ui_threadtest.h"

ThreadTest::ThreadTest(QWidget *parent) :
QWidget(parent),
ui(new Ui::ThreadTest) 
{
    ui->setupUi(this);

    myWorker = new worker;
    WorkerThread = new QThread;
    myWorker->moveToThread(WorkerThread);

    connect(this,
            SIGNAL(startWorkSignal()),
            myWorker,
            SLOT(StartWork())
            );

    connect(this,
            SIGNAL(stopWorkSignal()),
            myWorker,
            SLOT(StopWork())
            );

    //Debug
    this->dumpObjectInfo();
    myWorker->dumpObjectInfo(); 
}

ThreadTest::~ThreadTest() 
{
    delete ui; 
}

void ThreadTest::on_startButton_clicked() 
{
    qDebug() << "startwork signal emmitted";
    emit startWorkSignal(); 
}

void ThreadTest::on_stopButton_clicked() 
{
    qDebug() << "stopwork signal emmitted";
    emit stopWorkSignal(); 
}

worker.h

#include <QObject>
#include <QDebug>

class worker : public QObject {
    Q_OBJECT 
public:
    explicit worker(QObject *parent = 0);
    ~worker();

signals:
    void SignalToObj_mainThreadGUI();
    //void running();
    //void stopped();

public slots:
    void StopWork();
    void StartWork();

private slots:
    void do_Work();

private:
    volatile bool running, stopped; 
};

worker.cpp

#include "worker.h"

worker::worker(QObject *parent) : QObject(parent), stopped(false),
running(false) 
{
    qDebug() << "running: " << running;
    qDebug() << "stopped: " << stopped; 

}


worker::~worker() {}

void worker::do_Work() 
{
    qDebug() << "inside do Work";
    emit SignalToObj_mainThreadGUI();

    if (!running || stopped) return;

    // actual work here
    /*
    for (int i = 0; i < 100; i++)
    {
        qDebug() << "count: " + i;
    }
    */
    QMetaObject::invokeMethod(this, "do_Work", Qt::QueuedConnection); 
}

void worker::StopWork() 
{
    qDebug() << "inside StopWork";
    stopped = true;
    running = false;

    //emit stopped(); 
}

void worker::StartWork() 
{
    qDebug() << "inside StartWork";
    stopped = false;
    running = true;

    //emit running();
    do_Work(); 
}

最佳答案

你应该写

WorkerThread->start();

或者您可以使用 ThreadTest 对象的线程来代替 WorkerThread(在这种情况下不需要 WorkerThread):

 myWorker->moveToThread(thread()); // this->thread

关于c++ - Qt - 使用由按钮控制的线程的简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281361/

相关文章:

Python-PyQt : restarting a thread which is finished

python - drop发生在application外部时,如何获取drop target?

c++ - 解释字符串文字

c++ - OpenCV光 vector 计算

c# - 如何使用 C# 语言通过 TCP 发送十六进制代码

c++ - 新运算符(以及 malloc)无法分配 ~ 450 MB 内存

c++ - 子类化 QCompleter,我的子类中的虚拟插槽未被调用

python - PyQt5 : Destroyed while thread is still running

c++ - Qt - 检查互联网连接并创建线程

c++ - 使用 OpenCV 将 YUV 转换为有损压缩