简体   繁体   中英

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).

I make a class to use how interface your name is "SipHandlerController". I extend this class in my main class (mainwindow from Qt in my code has the name Start).

My class is used at thread from pjsip to "receive a call". 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). 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:

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

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

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).

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. (nav is a QStackedWidget), the method onSipIncomingRinging is called but the application is broken in this moment.

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

Was solved using the signals e emit:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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