Rudius guerrerat 2024-03-15 17:42 采纳率: 50%
浏览 21
已结题

qt无法调用connect函数,如何解决?


 bool ok = connect(
        m_vedio, &VedioFFmpeg::frameChanged, this, [](QSharedPointer<YUV420Frame> frame)
        { qDebug() << "Received frameChanged signal with frame: " << frame.data(); });

//信号发射
bool VedioFFmpeg::ToRGB(const AVFrame *aframe, uint8_t *m_pixels[], int *m_ptich, int outwidth, int outheight)
{
    vedio_frame_mutex.lock();
    if (!Vavformtctx)
    {
        vedio_frame_mutex.unlock();
        return false;
    }
    Vcodecctx =
        Vavformtctx->streams[this->VedioStream]->codec;
    Vswsctx = sws_getCachedContext(Vswsctx, aframe->width, aframe->height, (enum AVPixelFormat)aframe->format, outwidth, outheight,
                                   AV_PIX_FMT_RGBA, // 输出像素格式
                                   SWS_BILINEAR,    // 输出像素算法
                                   NULL, NULL, NULL);
    if (!Vswsctx)
    {
        vedio_frame_mutex.unlock();
        printf("sws_getCachedContext failed\n");
        return false;
    }
    // uint8_t *dst_data[AV_NUM_DATA_POINTERS] = {0};
    // dst_data[0] = (uint8_t *)buffer;
    // int linesize[AV_NUM_DATA_POINTERS] = {0};
    // linesize[0] = outwidth * 4;
    int h = sws_scale(Vswsctx, aframe->data, aframe->linesize, 0, aframe->height, m_pixels, m_ptich);
    // if (h > 0)
    // {
    // printf("decode of success: %d\n)", h);
    QSharedPointer<YUV420Frame> frame = QSharedPointer<YUV420Frame>::create(m_pixels[0], aframe->width, aframe->height);
    
    emit frameChanged(frame);
    // }
    vedio_frame_mutex.unlock();
    return true;
}

无法触发槽函数执行

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-15 17:42
    关注

    引自免费微信小程序:皆我百晓生

    在Qt中,当无法触发槽函数执行时,可能的原因有很多。针对您给出的代码片段,以下是一些排查和解决connect函数失效问题的步骤:

    1. 信号与槽类型匹配: 确保VedioFFmpeg::frameChanged(QSharedPointer<YUV420Frame>)信号和lambda表达式类型的槽之间的类型是兼容的。从提供的代码来看,信号和槽似乎是匹配的。

    2. 对象生命周期: 确保在连接信号槽时,m_vedio对象已经正确实例化且没有提前析构。如果在m_vedio对象销毁后才发射信号,则槽函数不会被调用。

    3. 线程安全: Qt的信号槽机制默认不是线程安全的。若ToRGB函数在非GUI线程中执行并发射信号,需要确保使用Qt::QueuedConnectionQt::AutoConnection(当信号和槽不在同一线程时自动转为队列连接)来跨线程传递信号。修改连接方式如下:

    bool ok = connect(
        m_vedio, &VedioFFmpeg::frameChanged,
        this, [](QSharedPointer<YUV420Frame> frame) {
            qDebug() << "Received frameChanged signal with frame: " << frame.data();
        },
        Qt::QueuedConnection // 或者 Qt::AutoConnection
    );
    
    1. 检查信号是否已发射: 检查ToRGB函数内部逻辑,确保emit frameChanged(frame);这行代码确实被执行到了,并且frame变量有效。

    2. 编译器/构建系统配置: 确保项目正确启用了Qt的元对象系统(Meta-Object System),即在包含头文件时使用了QObject并且在实现类中调用了Q_OBJECT宏。

    3. 动态注册信号槽: 如果你的类是在运行时生成的,那么你可能需要动态地注册信号和槽,但这通常不适用于原始代码片段中的情况。

    4. 编译选项: 确认编译时启用了-fPIC(对于共享库)和-std=c++11(至少,根据实际Qt版本可能需要更新的C++标准版本),以及链接了所有必要的Qt库。

    综合以上建议,请逐一检查您的代码和配置。如果问题仍然存在,请提供更多上下文信息以便更精确的定位问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月31日
  • 已采纳回答 3月23日
  • 创建了问题 3月15日

悬赏问题

  • ¥20 python爬虫遇到空文本取不出来
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入