简体   繁体   English

如何使用Qt和C ++的“接口类”通信两个线程

[英]How communicate two thread, using a “Interface Class” with Qt and C++

I am using the Qt and i am trying call a method from main thread (navigate to other tab) when a event ocorr in other thread (pjsip library receive a call). 我正在使用Qt,并且尝试在其他线程中的事件ocorr(pjsip库收到调用)时从主线程(导航至其他选项卡)调用方法。

I make a class to use how interface your name is "SipHandlerController". 我制作了一个类,使用您的名字叫“ SipHandlerController”的接口。 I extend this class in my main class (mainwindow from Qt in my code has the name Start). 我在主类中扩展了该类(在我的代码中,来自Qt的mainwindow具有名称Start)。

My class is used at thread from pjsip to "receive a call". 我的类在pjsip的线程中用于“接收呼叫”。 The class from pjsip to handler this event has too a property from type "SipHandlerController" this property receive the instance of my main class (this is running in other thread to to manipulate all ui). 从pjsip到该事件的处理程序的类也具有“ SipHandlerController”类型的属性,该属性接收我的主类的实例(该类在其他线程中运行以操纵所有ui)。 When the event to "receive a call" is called by pjsip thread, it really call the method from my main thread (the method to implement the SipHandlerController), but a received a error and the application broken: 当pjsip线程调用“接收呼叫”事件时,它实际上是从我的主线程调用该方法(实现SipHandlerController的方法),但收到一个错误,应用程序损坏了:

ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 0x0x26545658. Receiver 'pageDialer' (of type 'QWidget') was created in thread 0x0x2071ab68", file kernel\\qcoreapplication.cpp, line 576 QCoreApplication :: sendEvent中的ASSERT失败:“无法将事件发送到其他线程拥有的对象。当前线程0x0x26545658。在线程0x0x2071ab68中创建了接收方'pageDialer'(类型为'QWidget')”,文件kernel \\ qcoreapplication.cpp,行576

My interface 我的介面

#ifndef SIP_HANDLER_CONTROLLER_H
#define SIP_HANDLER_CONTROLLER_H

#include <pjsua2.hpp>

using namespace pj;

class SipHandlerController
{
public:
    virtual void onSipIncomingRinging(Call * call) = 0;
};

#endif // SIP_HANDLER_CONTROLLER_H

My implementation in class from pjsip to listen the event 我在课堂上的实现从pjsip监听事件

void SipLine::onIncomingCall(OnIncomingCallParam &iprm){
    SipCall *call = new SipCall(*this, iprm.callId);
    call->setHandler(this->handler);

    if(this->handler != nullptr){
        // Here is call the method from main thread
        this->handler->onSipIncomingRinging(call);
    }
}

My the header from main class (implementing the class "SipHandlerController", this class is my main thread). 我来自主类的头(实现类“ SipHandlerController”,此类是我的主线程)。

class Start : public QMainWindow, public SearchHandler, public FooterHandler, public SipHandlerController, public PhonebookHandler
{
    Q_OBJECT

public:
    explicit Start(QWidget *parent = nullptr);
    ~Start();
    virtual void onSipIncomingRinging(Call * call);
}

The source from my main implement the SipHandlerController. 我主要实现的源代码是SipHandlerController。 (nav is a QStackedWidget), the method onSipIncomingRinging is called but the application is broken in this moment. (nav是QStackedWidget),调用了onSipIncomingRinging方法,但此时应用程序已损坏。

void  Start::onSipIncomingRinging(Call * call){
    this->call = call;
    ui->nav->setCurrentIndex(NavigationTabs::RINGING);
}

Was solved using the signals e emit: 使用信号e发射得到解决:

Added the signals 添加了信号

class Start : public QMainWindow, public SearchHandler, public FooterHandler, public SipHandlerController, public PhonebookHandler
{
    Q_OBJECT
   public:
        explicit Start(QWidget *parent = nullptr);
        ~Start();
        virtual void onSipIncomingRinging(Call * call);
    signals:
        void onSipIncomingRingingEvent(Call * call);
}

The implementation was changed to emit a signal 实现更改为发出信号

void Start::onSipIncomingRinging(Call * call){
    emit onSipIncomingRingingEvent(call);
}

The slot change the front-end 插槽更改前端

void Start::on_Start_onSipOutgoingRingingEvent(Call * call)
{
    this->call = call;
    ui->nav->setCurrentIndex(NavigationTabs::CONTACT);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM