使用 Qt 进行简单的多线程处理:我这样做对吗?

问题描述 投票:0回答:2

我是 StackOverflow 的新手,想知道我这样做是否正确:

我正在编写一个简单的 Qt 应用程序来测试多线程(我也是全新的)。我创建了一个包含小部件的 MainWindow 和一个 MyThread 类,该类是 QThread 的子类并重写 run() 方法。

该应用程序仅显示两个按钮“启动计数器”和“停止计数器”以及一个文本字段。当按下“启动计数器”时,将创建一个工作线程并在后台运行,在 while 循环中不断递增计数器,并向主线程(GUI 所在的位置)发送更新值的信号。当按下“停止计数器”时,会向主线程发送一个停止 while 循环的信号,并且计数器将停止,直到再次按下“启动计数器”为止。

这工作得很好......但这是最好的方法吗?我对此很陌生,读到很多人说“不要子类化 QThread”,其他人说“子类化 QThread”,这有点令人困惑。如果这不是实现此类事情的最佳方法(在带有“开始”和“停止”按钮的后台线程中运行计算密集型循环),那么什么是?如果我做错了,我该如何做对呢?我不想学错方法。

谢谢!这是代码:

MyThread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QMutex>

class MyThread : public QThread
{
   Q_OBJECT

public slots:
    void stopRunning();

protected:
   virtual void run();

signals:
   void signalValueUpdated(QString);

private:
    bool isRunning;

};

MyThread.cpp

#include "MyThread.h"
#include <QString>

void MyThread::run()
{
    qDebug("Thread id inside run %d",(int)QThread::currentThreadId());

    static int value=0; //If this is not static, then it is reset to 0 every time this function is called.
    isRunning = 1;
    while(isRunning == 1)
    {
        QString string = QString("value: %1").arg(value++);
        sleep(1/1000); //If this isn't here, the counter increments way too fast and dies, or something; the app freezes, anyway.

        emit signalValueUpdated(string);       
    }            
}

void MyThread::stopRunning()
{
    isRunning = 0;
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QApplication>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLineEdit>
#include "MyThread.h"

class MainWindow : public QWidget
{
  Q_OBJECT

  public:
    MainWindow(QWidget *parent = 0);

  private:
    //Widgets
    QHBoxLayout * boxLayout;
    QPushButton * startButton;
    QPushButton * stopButton;
    QLineEdit * lineEdit;

    MyThread thread;
};

#endif

主窗口.cpp

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    boxLayout = new QHBoxLayout(this);
    startButton = new QPushButton("Start Counter", this);
    stopButton = new QPushButton("Stop Counter", this);
    lineEdit = new QLineEdit(this);

    boxLayout->addWidget(startButton);
    boxLayout->addWidget(stopButton); 
    boxLayout->addWidget(lineEdit);

    qDebug("Thread id %d",(int)QThread::currentThreadId());

    //When the start button is pressed, invoke the start() method in the counter thread
    QObject::connect(startButton,SIGNAL(clicked()),&thread,SLOT(start()), Qt::QueuedConnection);

    //When the stop button is pressed, invoke the stop() method in the counter thread
    QObject::connect(stopButton,SIGNAL(clicked()),&thread,SLOT(stopRunning()), Qt::QueuedConnection);

    //When the counter thread emits a signal saying its value has been updated, reflect that change in the lineEdit field.
    QObject::connect(&thread,SIGNAL(signalValueUpdated(const QString&)),lineEdit,SLOT(setText(const QString&)), Qt::QueuedConnection);
}
c++ multithreading qt4 qthread
2个回答
5
投票

大多数时候,QThread 子类化是在 Qt 中进行线程处理的错误方法。我建议您阅读一篇关于线程、事件循环和其他的文章,它可以让您了解如何更好地在 Qt 中使用线程。但不要听任何人争论使用 QThread 的正确方法只有一种。有两种方法,虽然通常不需要子类化,但有时它可能很有用。您只需要使用非子类化方式,直到您确实需要子类化。在您的特定情况下,您不需要子类化。


2
投票
sleep(1/1000);

替换为

msleep(100);
一切都会好起来的:)
    

© www.soinside.com 2019 - 2024. All rights reserved.