简体   繁体   English

如何在Qt中使用线程

[英]How to use threads in qt

I am new to QT. 我是QT新手。 I need to use threads for some purpose. 我需要出于某些目的使用线程。 I searched a lot about threading in QT but all the articles and videos are using same example. 我搜索了很多有关QT中的线程的信息,但是所有文章和视频都使用相同的示例。 They are using dialog and putting a label with 2 buttons to print some data on label. 他们正在使用对话框,并使用2个按钮放置标签以在标签上打印一些数据。 I want to use threads with MainWindow. 我想在MainWindow中使用线程。 My application include reading a serial data and then displaying the related information on a label. 我的应用程序包括读取串行数据,然后在标签上显示相关信息。 That information contains a string and an audio file. 该信息包含一个字符串和一个音频文件。 String and audio file needs to be played at the same time. 字符串和音频文件需要同时播放。 I have a connected a signal for serial read like below: 我连接了一个串行读取的信号,如下所示:

connect(&Serial, SIGNAL(readyRead()), this, SLOT(SerialRead()));

QString MainWindow::SerialRead()
{
  word Words; //
  QString serialData = Serial.readAll();              //Reading Serial Data
  //Now here I want to start the two threads
  //Thread 1 to display string
  //Thread 2 to play audio
  return 0;

}

How can I achieve above task. 我如何实现以上任务。 Can anyone please refer me to some usefull links or articles. 任何人都可以请我参考一些有用的链接或文章。 Thanks 谢谢

While I very highly recommend that you use std::thread instead of QThread, it's your call. 虽然我非常建议您使用std::thread而不是QThread,但这是您的要求。 However, on the Qt docs page of QThread there's a very good example that exactly fits what you need. 但是,在QThreadQt docs页面上,有一个非常适合您需要的很好的示例。 Here it's: 这里是:

class Worker : public QObject
{
    Q_OBJECT

public slots:
    void doWork(const QString &parameter) {
        QString result;
        /* ... here is the expensive or blocking operation ... */
        emit resultReady(result);
    }

signals:
    void resultReady(const QString &result);
};

class Controller : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    Controller() {
        Worker *worker = new Worker;
        worker->moveToThread(&workerThread);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(this, &Controller::operate, worker, &Worker::doWork);
        connect(worker, &Worker::resultReady, this, &Controller::handleResults);
        workerThread.start();
    }
    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }
public slots:
    void handleResults(const QString &);
signals:
    void operate(const QString &);
};

Basically, in this example, the Controller is your MainWindow, and the constructor of Controller is your MainWindow::SerialRead() . 基本上,在此示例中, Controller是您的MainWindow,而Controller的构造函数是您的MainWindow::SerialRead() Be careful with memory and thread management though if you want to do that, because that Controller is made to destroy things when it exists, not when the thread is finished. 但是,如果要这样做,请注意内存和线程管理,因为该Controller是在存在时(而不是在线程完成时)破坏事物的。

So you either use that controller as is (simply instantiate it in your MainWindow::SerialRead() ), or change it to include parts of it in your MainWindow . 因此,您可以按原样使用该控制器(只需在MainWindow::SerialRead()实例化该控制器),或对其进行更改以使其包含在MainWindow

you may not need to use 2 threads to do such things. 您可能不需要使用2个线程来执行此类操作。 just emit a signal connected to setText(const QString&) and the other signal connected to the slot for playing audio. 只是发出一个连接到setText(const QString&)的信号,另一个发出连接到插槽以播放音频的信号。 what is the size of serial data? 串行数据的大小是多少?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM