c++ - Qt : am I doing this right? 的简单多线程

标签 c++ multithreading qt4 qthread

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

我正在编写一个简单的 Qt 应用程序来测试多线程(这对我来说也是全新的)。我制作了一个包含小部件的 MainWindow 和一个继承 QThread 并覆盖 run() 方法的 MyThread 类。

该应用程序只显示两个按钮,“Start Counter”和“Stop Counter”,以及一个文本字段。当按下“启动计数器”时,将创建一个工作线程并在后台运行,在 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;
}

主窗口.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);
}

最佳答案

大多数情况下,QThread 子类化是在 Qt 中执行线程的错误方法。我建议你阅读 article about threads, event loops and other这可以让您了解如何以更好的方式在 Qt 中使用线程。但是不要听信任何人争论只有一种正确的使用 QThread 的方法。有两种方法,虽然通常不需要子类化,但有时它可能很有用。你只需要使用非子类化的方式,直到你真的需要子类化。在您的特定情况下,您不需要子类化。

关于c++ - Qt : am I doing this right? 的简单多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15216046/

相关文章:

visual-studio-2010 - Qt每500ms中断程序

QTreeView自定义各行的行高

c++ - 使用宏创建自定义 block 类型

c# - 将列表分成组

c++ - 什么时候必须用 C 语言链接库?

java - JAVA 中的网络爬虫。 java.out.lang.outofmemory 无法创建 native 线程

java - 同步块(synchronized block)后面

c++ - Qt 中的 c++ 与 ANSI c++ 不同吗?

c++ - CRTP vs 过载和最终

c++ - 无依赖性最快的 C++ 信号/槽库