0

Please can some-one suggest on this question..? I have to send data from my TX thread to mainwindow using class derived from QEvent.

I want my RX thread to throw an event whenever some data is received so hat i can display that data in the Mainwindow.

I start a main thread when button start is pressed. I save the pointer to my mainwindow inside the object of main thread. I will use this pointer to post events to the mainwindow object.

When first time i enter the Qthread function dowork_rx(). Event is thrown & i am able to catch it in customEvent() handler. But when dowork_rx() while loop starts when i throws the event it does not trigger the customEvent() function.

Please suggest how to resolve this problem.

My qevent class :---

//String event derived class
template <typename T> class StringEvent : public QEvent
{
   QString m_str;
public:
   explicit StringEvent(const QString val) : QEvent(staticType()), m_str(val)
   {
   }

   void setvalue(QString val)
   {
       m_str = val;
   }

   QString value() const
   {
       return m_str;
   }

   static QEvent::Type staticType()
   {
      static int type = QEvent::registerEventType();
      return static_cast<QEvent::Type>(type);

      /*
        static int type;
      if(type == 0)
      {
          type = QEvent::registerEventType();
      }

         return static_cast<QEvent::Type>(type);*/
   }

   static bool is(const QEvent * ev)
   {
       return ev->type() == staticType();
   }
};

class UpdateEvent : public StringEvent<UpdateEvent>
{
public:
    explicit UpdateEvent(QString val): StringEvent(val)
    {
       //qDebug() << "hello";

    }

};


class ClearEvent  : public StringEvent<ClearEvent>
{
public:
    explicit ClearEvent(QString val): StringEvent(val)
    {

    }
};

Dowork function of the thread :----

// Common slot for the rx - thread
void RxThreadObject::dowork_rx()
{
    int i =0;
    qDebug() << "\nrx start \n";

    myUpdateEvent_rx = new UpdateEvent("UpdateEventObject - dowork_rx");
    //myUpdateEvent_rx->setvalue("first");
    QCoreApplication::postEvent(m_pMainThreadObj->ptrmainwindow, myUpdateEvent_rx);
    qDebug() << "\nrx throw event - done \n";

    while(!m_bQuitRx)
    {

        SleepTimerDelay::sleep(2);

        if(i==0){
            //qDebug() << "\nrx throw event - done  11 \n";
            myUpdateEvent_rx = new UpdateEvent("first");
            myUpdateEvent_rx->setvalue("first");
            QCoreApplication::postEvent(m_pMainThreadObj->ptrmainwindow, myUpdateEvent_rx);
            i++;
        }else{
            //qDebug() << "\nrx throw event - done  22 \n";
            myUpdateEvent_rx = new UpdateEvent("second");
            myUpdateEvent_rx->setvalue("second");
            QCoreApplication::postEvent(m_pMainThreadObj->ptrmainwindow, myUpdateEvent_rx);
            i=0;
        }


    }

    qDebug() << "\nrx end \n";

}

Event handler :----

/*!
** Custom event handler
*/
void MainWindow::customEvent(QEvent *event)
{
    qDebug() << "oo customEvent";
   if (UpdateEvent::is(event)) {
       UpdateEvent *tempUpdateEvent = static_cast<UpdateEvent *>(event);
       qDebug() << tempUpdateEvent->value();

   }
   else if (ClearEvent::is(event)) {
       ClearEvent *tempClearEvent = static_cast<ClearEvent *>(event);
       qDebug() << tempClearEvent->value();

   }

}

/*!
** event filter handler
*/
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{

    qDebug() << "oo  eventFilter";
   if (UpdateEvent::is(event)) {
       UpdateEvent *tempUpdateEvent = static_cast<UpdateEvent *>(event);
       qDebug() << tempUpdateEvent->value();

   }
   else if (ClearEvent::is(event)) {
       ClearEvent *tempClearEvent = static_cast<ClearEvent *>(event);
       qDebug() << tempClearEvent->value();

   }

   return true;

}
Katoch
  • 2,709
  • 9
  • 51
  • 84

0 Answers0