1

我正在使用 Qt5,我通过 moveToThread() 将 QObject 工作线程传递给 QThread 的实例来实现线程。我的实现看起来像这样..

工人.h

class worker : public QObject
{
    Q_OBJECT
public:
    explicit worker(QObject *parent = 0);
    bool IsWorkRunning();
    void MoveObjectToThread();

signal:
    void SignalToObj_mainThreadGUI();

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

private:
    void Sleep();
    QThread *workerthread;    
    volatile bool running,stopped;
};

工人.cpp

 worker::worker(QObject *parent) :
    QObject(parent),stopped(false),running(false)
{
}

void worker::do_Work()
{
    running = true;
    while(!stopped)
    {
       if(running)
       {
        emit SignalToObj_mainThreadGUI();
        workerthread->msleep(20);
       }
    }
}

void worker::StopWork()
{
    running = false;
}

void worker::StartWork()
{
    running = true;
}

bool worker::IsWorkRunning()
{
    return running;
}

void MoveObjectToThread()
{
  workerthread = new QThread;
  QObject::connect(workerthread,SIGNAL(started()),this,SLOT(do_Work()));

  this->moveToThread(workerthread);

  workerthread->start();
}

主窗口.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

signals:
    void Startwork_mainwindow();
    void Stopwork_mainwindow();

public slots:

private slots:
    void on_pushButton_push_to_start_clicked();

    void on_pushButton_push_to_stop_clicked();

private:

    Ui::MainWindow *ui;
    worker myWorker;
    bool work_started;

};

主窗口.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),work_started(false),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QObject::connect(this,SIGNAL(Startwork_mainwindow()),&myWorker,SLOT(StartWork()));
    QObject::connect(this,SIGNAL(Stopwork_mainwindow()),&myWorker,SLOT(StopWork()));
}

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

void MainWindow::on_pushButton_push_to_start_clicked()
{
    if(!work_started)
    {
      myWorker.MoveObjectToThread();
      work_started = true;
    }

    if(!myWorker.IsWorkRunning())
       emit this->Startwork_mainwindow();
}

void MainWindow::on_pushButton_push_to_stop_clicked()
{
  if(myWorker.IsWorkRunning())
       emit this->Stopwork_mainwindow();
}

不知道为什么以下两个信号/插槽对似乎不起作用

QObject::connect(this,SIGNAL(Startwork_mainwindow()),&myWorker,SLOT(StartWork()));
QObject::connect(this,SIGNAL(Stopwork_mainwindow()),&myWorker,SLOT(StopWork()));

因此,一旦do_Work()插槽被started()QThread 对象的信号触发,我就无法启动或停止线程。仅供参考,我的这篇文章是我之前在这里描述的文章的延续。任何见解都会有所帮助......谢谢

4

1 回答 1

2

在您的MainWindow类定义中,尝试更改worker myWorkerworker * myWorker. 另外,我会按照其他人的建议将线程移到worker课堂之外。构造MainWindow函数变成这样:

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
  , work_started(false)
  , ui(new Ui::MainWindow)
  , myWorker( new worker() )
{
  // NOTE:  myWorker is created without a parent on purpose.
  // Qt won't change the thread affinity of an obj with a parent

  ui->setupUi(this);

  connect( this, SIGNAL(Startwork_mainwindow()), myWorker, SLOT(StartWork()) );
  connect( this, SIGNAL(Stopwork_mainwindow()), myWorker, SLOT(StopWork()) );

  QThread * thread = new QThread();
  // delete the worker obj whenever this obj is destroyed
  connect( this, SIGNAL(destroyed()), myWorker, SLOT(deleteLater()) );
  // stop the thread whenever the worker is destroyed
  connect( myWorker, SIGNAL(destroyed()), thread, SLOT(quit()) );
  // clean up the thread
  connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater()) );
  myWorker->moveToThread( thread );
  thread->start();
}

当然,您不再需要该worker::MoveObjectToThread()方法。此外,worker::IsWorkRunning()MainWindow. 这个具体的例子你可能不会遇到任何麻烦,但是当事情变得更复杂时,它肯定会让你感到痛苦。相反,添加一个信号或类似的东西,并在课堂上workFinished()聆听它。MainWindow

信号将Startwork_mainwindow()开始工作。由于您没有在对 的调用中提供连接类型connect,因此 Qt 将QueuedConnection在您更改线程关联 ( moveToThread) 时使用。基本上,myWorker是在一个有自己的事件循环的线程中。Qt::QueuedConnection使用将事件发布到该事件循环来调用插槽,然后该事件循环将插槽方法排队。只要事件循环到达它,它就会运行。

于 2013-01-24T04:09:17.823 回答