卑鄙的张老二 2023-10-25 10:32 采纳率: 0%
浏览 2

connect函数使用了QueuedConnection处理模式,在主线程闲置的时候为什么没有执行槽函数


#include "Radar.h"
#include "ui_Radar.h"

Radar::Radar(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Radar)
{
    ui->setupUi(this);

    try {
         m_radar = new Radar_RW();
         m_radar->moveToThread(new QThread());

    } catch (std::bad_alloc &e) {
        qDebug()<<"omg!!";
    }
       QObject::connect(m_radar,&Radar_RW::dataReceived,this,&Radar::ondataReceived);
       m_radar->start();
       try {
           QObject::connect(ui->Come_Tiltration,SIGNAL(clicked()),m_radar,SLOT(ComeTiltration_setting()),Qt::QueuedConnection);
       } catch (std::exception &e) {
           qDebug()<<"Oh,shit!";
       }


   Dialog_Init();

}

Radar::~Radar()
{
    m_radar->quit();
    delete ui;
}



#include "Radar_RW.h"

Radar_RW::Radar_RW(QObject *parent):
    QThread(parent),
    m_ReceiveThreadFlag(true)
{
    m_serialPort_Infos = QSerialPortInfo::availablePorts();
    qDebug() << "Available serial ports:";
    for (const QSerialPortInfo &serialPortInfo : m_serialPort_Infos) {
        qDebug() <<serialPortInfo.portName();
    }
    // 打开串口
    m_serialPort.setPortName(m_serialPort_Infos.first().portName()); // 设置串口名
    m_serialPort.setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);  // 设置波特率
    m_serialPort.setDataBits(QSerialPort::Data8);  // 设置数据位
    m_serialPort.setParity(QSerialPort::NoParity);  // 设置校验位
    m_serialPort.setStopBits(QSerialPort::OneStop);  // 设置停止位
    if (!m_serialPort.open(QIODevice::ReadWrite)) {
        qDebug() << "Failed to open serial port!"<<m_serialPort.errorString();

    }
    else{
        qDebug()<<"success!";
        }

}

void Radar_RW::run()
{
    while (m_ReceiveThreadFlag){       
        try {
            // 等待接收数据
             m_serialPort.waitForReadyRead(1000);
            // 读取数据
            QByteArray responseData = m_serialPort.readAll();
            m_data=responseData.toHex();
            if(!m_data.isEmpty())
            {
            qDebug()<<m_data;
            }
            emit dataReceived(m_data);
        } catch (std::exception &e) {
           qDebug()<<"sorry!";
        }


    }
}

void Radar_RW::Receive()
{
    // 等待接收数据
    QString Data;
    QByteArray responseData;
    while (true) {
        if (m_serialPort.waitForReadyRead(1000)) {
                responseData = m_serialPort.readAll();
                 QString hexString = QString(responseData.toHex());
                 qDebug()<<hexString;
                if (hexString.startsWith("fa323030fb")) {
                    QString subString = hexString.mid(0, 10);
                    Data.append(subString);
                    }
                if (Data == "fa323030fb") {
                     qDebug() << "SET_Success";
                    break;
                    }
            }
    }

    Data.truncate(0);
}

void Radar_RW::Enter_setting()
{
    m_ReceiveThreadFlag=false;
    // 发送控制指令
    QByteArray sendData;
    sendData.append(0xFA);
    sendData.append(0x31);
    sendData.append(0x30);
    sendData.append(0x30);
    sendData.append(0xFB);
    qint64 bytesWritten = m_serialPort.write(sendData);
    if (bytesWritten == -1) {
        qDebug() << "Failed to send control command!";
        m_serialPort.close();
    }
}

void Radar_RW::Exit_setting()
{
    // 发送控制指令
    QByteArray sendData;
    sendData.append(0xFA);
    sendData.append(0x32);
    sendData.append(0x30);
    sendData.append(0x30);
    sendData.append(0xFB);
    qint64 bytesWritten = m_serialPort.write(sendData);
    if (bytesWritten == -1) {
        qDebug() << "Failed to send control command!";
        m_serialPort.close();
    }
     m_ReceiveThreadFlag=true;
}

void Radar_RW::ComeTiltration_setting()
{
    Enter_setting();
    // 发送控制指令1  触头
    QByteArray sendData1;
    sendData1.append(0xFA);
    sendData1.append(0x33);
    sendData1.append(0x31);
    sendData1.append(0x30);
    sendData1.append(0xFB);
    qint64 bytesWritten1 = m_serialPort.write(sendData1);
    if (bytesWritten1 == -1) {
        qDebug() << "Failed to send control command!";
        m_serialPort.close();
    }
    // 发送控制指令2  来向
    QByteArray sendData;
    sendData.append(0xFA);
    sendData.append(0x37);
    sendData.append(0x31);
    sendData.append(0x30);
    sendData.append(0xFB);
    qint64 bytesWritten2 = m_serialPort.write(sendData);
    if (bytesWritten2 == -1) {
        qDebug() << "Failed to send control command!";
        m_serialPort.close();
    }
    Receive();
    Exit_setting();
}

其中的m_radar是子线程,控件是线程的第二个connect函数使用了QueuedConnection处理模式,在主线程闲置的时候为什么没有执行槽函数
希望大家可以帮忙看一下

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-25 12:17
    关注

    【以下回答由 GPT 生成】

    首先,根据你提供的代码,我注意到在主线程中创建了一个Radar_RW对象,并将其移动到了一个新的线程中。然后,在主线程中通过QObject::connect函数使用QueuedConnection模式将Radar_RW对象的dataReceived信号与Radar对象的ondataReceived槽函数连接起来。

    根据你提供的代码,你在Radar_RW类的构造函数中并没有发出dataReceived信号,这可能是导致槽函数没有执行的原因之一。确保在Radar_RW类中定义并发出了dataReceived信号。以下是一个示例:

    // Radar_RW.h
    class Radar_RW : public QThread
    {
        Q_OBJECT
    public:
        explicit Radar_RW(QObject *parent = nullptr);
        // ...
    signals:
        void dataReceived(const QByteArray& data);
        // ...
    };
    
    // Radar_RW.cpp
    Radar_RW::Radar_RW(QObject *parent)
        : QThread(parent),
          m_ReceiveThreadFlag(true)
    {
        // ...
        connectToSomeSignal(); // 连接到某个信号上并发出dataReceived信号
    }
    
    // 假设有一个函数connectToSomeSignal,可以在这个函数中连接到Radar_RW对象的某个信号并发出dataReceived信号
    

    除此之外,你还需要确保以下几点:

    1. Radar_RW对象已经启动运行,可以通过调用start()方法来实现。例如,在Radar类的构造函数中调用m_radar->start()方法。

    2. ondataReceived槽函数的定义和实现是正确的,并能够处理接收到的数据。你需要确保槽函数的实现在接收到数据时能够被正确调用。

    如果你已经确保了以上几点,并且问题仍然存在,那么问题可能出在其他地方。例如,可能存在其他线程阻塞了主线程,导致主线程无法空闲并执行槽函数。在这种情况下,你需要检查其他线程的代码并确保没有锁住主线程。

    如果以上的解决方案都没有解决你的问题,并且你无法找到其他解决办法,那么可能问题的原因更加复杂,超出了本次问题解答的范围。在这种情况下,建议你详细检查和调试你的代码,或者向Qt社区或相关论坛寻求帮助。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 10月25日

悬赏问题

  • ¥18 help me! 希望大家来看看 吉~
  • ¥15 C++显示超限兔子集结
  • ¥15 sql server 2012的下载出错
  • ¥15 图像识别用户软件开发
  • ¥20 类原生rom lineageos
  • ¥15 有没有会做中专,云计算,卷子的,有偿一百块
  • ¥15 HC32串口DMA循环发送数据
  • ¥15 Uni-App实现飞书授权登陆
  • ¥50 Qt应用中如何通过代码打开开发者工具devtools
  • ¥20 mpp硬解码h264转为yuv