当前位置: 代码迷 >> QT开发 >> 诸位,看看为何信号调不到相应的槽函数
  详细解决方案

诸位,看看为何信号调不到相应的槽函数

热度:58   发布时间:2016-04-25 03:19:30.0
各位,看看为何信号调不到相应的槽函数
情况是这样的。。
代码段1:

void RevDataManager::JoinInThread(void)
{
    m_pCommuniThread = new CommThread;   //通信子线程
    this->moveToThread(m_pCommuniThread); //负责通信处理的Object添加到子线程中
    if(!m_pCommuniThread->isRunning())
    {
        m_pCommuniThread->start();
    }
}

代码段2:

void RevDataManager::TestFun()
{
    QStringList strRevData = QStringList()<<"美女.mp4 0:3:54"<<"旅游.mp4 0:4:12"<<\
                                            "靓仔.mp4 0:4:0"<<"孩子.mp4 0:3:56"<<\
                                            "夕阳.mp4 0:5:38";  //收到了这些数据
    m_pADController->UpdateADData(strRevData); // 调用QObject子类方法
}

代码3:

void ADController::UpdateADData(QStringList strADData)
{
    qDebug()<<"send UpdateADList signal start..."<<__FILE__;
    emit signalUpdateADList(strADData);   //向QmainWindow发送信号
    qDebug()<<"send UpdateADList signal end..."<<__FILE__;
    return;
}

代码4:

void MainPanel::InitConnections(void)
{
//QMainWindow中绑定的信号和槽,槽函数也在该类中。
    if(m_pADController) {
        qDebug()<<"444444444444444444444444444";
        qRegisterMetaType< vector<ADListData_t> >("vector<ADListData_t>");
        QObject::connect(m_pADController, SIGNAL(signalUpdateADList(const vector<ADListData_t>)),
                        this, SLOT(slotUpdateADListSignal(const vector<ADListData_t>)),Qt::QueuedConnection);
        qDebug()<<"555555555555555555555555555";
    }
    return;
}

结果:
代码3,4中LOG打出来了。证明信号槽建立,并且信号已经发送,但是槽函数slotUpdateADListSignal中的log没有打出来,不知道为什么。
------解决方案--------------------
代码太少,看不出来各个类之间的关系,都贴出来看看。
另外,可以尝试在通信子线程启动成功后再moveToThread,还有,把你的 ADController也纳入到子线程中去。
------解决方案--------------------
qRegisterMetaType< vector<ADListData_t> >("vector<ADListData_t>");放在发送信号的类中,并且别忘了声明
------解决方案--------------------
typedef  ADDATA  vector<ADListData_t>;
然后用ADDATA代替vector<ADListData_t>就行了
------解决方案--------------------
MainPanel应该是在主线程里面,也就是槽(slotUpdateADListSignal)也在主线程里面.不知道这样理解对不对.
如果需要让slotUpdateADListSignal在线程m_pCommuniThread里面执行,可能连接的时候需要Qt::DirectConnection参数来支持,这个操作,具体没有测试过,我想和真相大体也相差不远


------解决方案--------------------
...可能是我想我了,你确定人的信号参数没有写错.我看你抛出的信号是QStringList,
连接的时候会的vector.还有那个信号连接里面的const不用写.
  相关解决方案