带插槽和信号的QThread似乎没有创建新线程

时间:2015-11-21 16:48:35

标签: c++ multithreading qt

我遇到了Qt线程的一些问题,允许线程部分更新程序的GUI。看起来这是Qt已知的“问题”,所以我找到了多个教程,但我不明白为什么我的例子不起作用。

我从QThread继承如下:

class CaptureThread: public QThread {
  Q_OBJECT

public:
  CaptureThread(const QObject *handler, QPushButton *start) {
    CaptureThread::connect(start, SIGNAL(clicked()), this, SLOT(capture_loop()));
    CaptureThread::connect(this, SIGNAL(sendPacket(Packet*)),
                           this, SLOT(receivePacket(Packet*)));
  }

signals:
  void sendPacket(Packet*);

public slots:
  void capture_loop() {
    Packet *packet;

    while (my_condition) {
      packet = Somewhere::getPacket();
      //getPacket is define somewhere and is working fine                                

      emit(sendPacket(packet));
      std::cout << "Sending packet!" << std::endl;
    }
  }
};

这是CaptureHandler:

class CaptureHandler: public QWidget {
  Q_OBJECT

public:
  CaptureHandler() {
    start = new QPushButton("Capture", this);

    thread = new CaptureThread(this, start);
    thread->start();
  }

public slots:
  void  receivePacket(Packet *packet) {
    std::cout << "Packet received!" << std::endl;
    /*                                                                                   
      Here playing with layout etc...                                                    
     */
  }

private:
  QPushButton   *start;
  CaptureThread *thread;
};

我认为信号和插槽都可以,因为它显示在终端上

Sending packet!
Packet received!
Sending packet!
Packet received!
Sending packet!
Packet received!

但是在receivePacket插槽中,我正在尝试修改我的GUI,但它不起作用。 GUI只是冻结,我所能做的就是终端上的CTRL + C. 所以我认为我的capture_loop目前是一个无限循环,它阻止了程序,这意味着我的线程还没有启动。

但是我调用了thread-&gt; start()。 我甚至尝试通过调用this-&gt; start()来启动CaptureThread构造函数中的线程,但结果是一样的。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您使用QThread错误。通过创建线程,它将不会在线程上执行。你需要在QThread::run函数内部(通过覆盖它)来完成它,因为那是唯一一个将在新线程上运行的函数。请注意,一旦从此函数返回,线程将退出。

如果你想在QThread::run函数中使用自己的循环(而不是使用Qts默认事件循环),线程将无法在运行函数内接收信号!

以下是有关如何使用QThread的示例:

class CaptureThread: public QThread {
  Q_OBJECT

public:
  CaptureThread(const QObject *handler, QPushButton *start) {
     //calling "start" will automatically run the `run` function on the new thread
     CaptureThread::connect(start, SIGNAL(clicked()), this, SLOT(start()));
    //use queued connection, this way the slot will be executed on the handlers thread
    CaptureThread::connect(this, SIGNAL(sendPacket(Packet*)),
                           handler, SLOT(receivePacket(Packet*)), Qt::QueuedConnection);
  }

signals:
  void sendPacket(Packet*);

protected:
  void run() {
    Packet *packet;

    while (my_condition) {
      packet = Somewhere::getPacket();
      //getPacket is define somewhere and is working fine                                

      emit sendPacket(packet) ;//emit is not a function
      qDebug() << "Sending packet!";//you can use qDebug
    }
  }
};