0

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);
}
Sileno Brito
  • 449
  • 1
  • 13
  • 31
  • 3
    The easiest to do this is to use Slots and Signals (which make for some sort of interface after all). Then Qt will do the thread-crossing. You should call moveToThread on the SipLine – Amfasis Aug 29 '18 at 13:26
  • @Amfasis I really believe what this is easy, but i studying the Qt and C++ does exactly seven days, and i do not have idea of how this is make. But you helped me, now i have a direction to try resolve. – Sileno Brito Aug 29 '18 at 13:34
  • 3
    The Qt. doc shows how to add a signal to a class derived from `QObject` here: [Signals & Slots - A Small Example](http://doc.qt.io/qt-5/signalsandslots.html#a-small-example). When signal and signal slot belong to objects associated to different threads, the connection becomes automatically a queued connection. If in doubt (or using a lambda as slot), you can explicitly append a [`Qt::QueuedConnection`](http://doc.qt.io/qt-5/qt.html#ConnectionType-enum) to the arguments of [`QObject::connect()`](http://doc.qt.io/qt-5/qobject.html#connect). – Scheff's Cat Aug 29 '18 at 13:53
  • 3
    Another part of Qt doc. concerning this: [Signals and Slots Across Threads](http://doc.qt.io/qt-5/threads-qobject.html#signals-and-slots-across-threads). – Scheff's Cat Aug 29 '18 at 13:55

1 Answers1

1

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);
}
Sileno Brito
  • 449
  • 1
  • 13
  • 31