Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to pass data from readyRead() to QRunnable class protected function.



  • data Variable from myclint class to mytask class run() function than inheritance QRunnable class

    myclint.cpp

    void MyClient::readyRead()
    {
        QByteArray data = socket->readAll();
        qDebug() << data ;
    
        //Time Consumer
        MyTask *mytask = new MyTask();
        mytask->setAutoDelete(true);
        connect(mytask,SIGNAL(Result(int)),SLOT(TaskResult(int)), Qt::QueuedConnection);
        QThreadPool::globalInstance()->start(mytask);
    }
    

    mytask.cpp

    void MyTask::run()
    {
        //time consumer
    
        qDebug() << "Task Start";
    
        int iNumber = 0;
        for(int i = 0; i < 1000; i++)
        {
            iNumber += i;
        }
    
        qDebug() << "Task Done";
    
        emit Result(iNumber);
    }
    

    thanks ,



  • Define a member variable in MyTask class;
    Then pass the data using constructor or define a public function to set it.

    class MyTask : public QRunnable {
    public:
      MyTask(const QByteArray& data);
      void run();
    private:
      QByteArray m_data;
    };
    
    MyTask::MyTask(const QByteArray& data) : m_data(data){}
    
    void MyTask::run()
    {
      //do something with m_data
    }
    
    MyTask *mytask = new MyTask(data);
    
    


  • Define a member variable in MyTask class;
    Then pass the data using constructor or define a public function to set it.

    class MyTask : public QRunnable {
    public:
      MyTask(const QByteArray& data);
      void run();
    private:
      QByteArray m_data;
    };
    
    MyTask::MyTask(const QByteArray& data) : m_data(data){}
    
    void MyTask::run()
    {
      //do something with m_data
    }
    
    MyTask *mytask = new MyTask(data);
    
    


  • thanks Bonnie ,


Log in to reply