c++ - QThread 永远不会启动

标签 c++ multithreading qt qthread

我正在尝试创建一个将尝试连接到串行端口的线程。在 Debug模式下,从 Serial 内部发出的信号都不会被触发。 connectToPort() 也从未被输入。

我是不是漏掉了什么?

我习惯于子类化 QThread,这是我第一次尝试使用 moveToThread() 方法。

代码如下:

蓝牙.cpp:

void Bluetooth::deviceSelected()
{
    QStringList comPort;

    comPort = bluetoothSelectedDevice->split(":");

    qDebug() << comPort.at(0);

    //Create COM port object to be used with QSerialPort by providing COM port in text.
    QSerialPortInfo temp(comPort.at(0));

    if(temp.isBusy())
    {
        qDebug() << "COM port is already open!";
        //TODO: Notify user that COM port is already open and that he should close it and try again.
        return;
    }

    //Create serial port object.
    serialPort = new QSerialPort(temp);

    //Instantiate a serial class object.
    serial = new Serial(serialPort);

    //Create a thread to be used with the serial object.
    serialWorkerThread = new QThread();

    //Move serial object to serial worker thread.
    serial->moveToThread(serialWorkerThread);

    QObject::connect(serialWorkerThread, SIGNAL(started()), serial, SLOT(connectToPort()));
    QObject::connect(serialWorkerThread, SIGNAL(finished()), serialWorkerThread, SLOT(deleteLater()));
    QObject::connect(serial, SIGNAL(connected()), this, SLOT(on_connected()));
    QObject::connect(serial, SIGNAL(couldNotConnect()), this, SLOT(on_couldNotConnect()));
    QObject::connect(serial, SIGNAL(portSettingsFailed()), this, SLOT(on_portSettingsFailed()));
}

序列号.h:

#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QSerialPort>

class Serial : public QObject
{
    Q_OBJECT
public:
    explicit Serial(QSerialPort *serialPort);

signals:
    void connected();
    void couldNotConnect();
    void portSettingsFailed();

public slots:
    void connectToPort();

private:
    QSerialPort *serialPort;

};

#endif // SERIAL_H

序列号.cpp:

#include "serial.h"
#include <QSerialPort>
#include <QDebug>

Serial::Serial(QSerialPort *serialPort)
{
    this->serialPort = serialPort;
}

void Serial::connectToPort()
{
    //Try to connect 5 times.
    for(int i = 0; i < 5; i++)
    {
        if(!serialPort->open(QIODevice::ReadWrite))
        {
            qDebug() << "Failed to open port";
            if(i == 4)
            {
                emit couldNotConnect();
                return;
            }
        }
        else
        {
            break;
        }
    }

    //Set port settings.
    if(serialPort->setBaudRate(QSerialPort::Baud9600) &&
       serialPort->setDataBits(QSerialPort::Data8) &&
       serialPort->setParity(QSerialPort::NoParity) &&
       serialPort->setStopBits(QSerialPort::OneStop) &&
       serialPort->setFlowControl(QSerialPort::NoFlowControl) != true)
    {
        qDebug() << "Failed to configure port";
        emit portSettingsFailed();
    }

    emit connected();
}

最佳答案

您是否粘贴了所有代码?您是否正在验证执行是否确实到达了您的 deviceSelected 方法的末尾?看起来你从来没有开始过你的话题。

即在你的连接添加之后:

serialWorkerThread->start();

关于c++ - QThread 永远不会启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21806671/

相关文章:

multithreading - 来自 ThreadPool 的 QNetworkAccessManager

android - 如何在 Qt (QML) 应用程序中使用适用于 Android 的 Tensorflow?

c++ - 试图为成员(member)回拨

c++ - 在此 C++ 代码段中,复制构造函数被调用了多少次?

c - Telnet session ,fork 与 thread

java - 将实际命令作为参数传递 Java

c++ - 在 QThread::run 中间调用一个槽

c++ - 每种哈希方法的不同值

c++ - 具有类似 QLineEdit 背景的 Qt Widget

c++ - 查找歌曲的长度