Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to pass data from readyRead() to QRunnable class protected function.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 470 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marco Flad
    wrote on last edited by
    #1

    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 ,

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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);
      
      
      1 Reply Last reply
      2
      • M Offline
        M Offline
        Marco Flad
        wrote on last edited by
        #3

        thanks Bonnie ,

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved