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 access the parent thread's vriable from child QThread
Forum Updated to NodeBB v4.3 + New Features

How to access the parent thread's vriable from child QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 622 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
    Mohammadreza99A
    wrote on last edited by
    #1

    I have a class MyClass wich starts a QThread class named Worker. I have a vriable in MyClass that I want to access from child thread. Is this possible and if yes, how?

    class MyClass{
    public:
        MyClass(){
             thread = new Worker(this);
            thread->start();
       }
    private:
        Worker *thread;
       int vriable; // this the vriable that I want to access from child thread
    };
    
    class Worker : public QThread {
        Q_OBJECT
    public:
        Worker(QOject *parent = 0) : QThread(parent){}
        void run() {
              // I want to access vriable here
       }
    };
    
    jsulmJ 1 Reply Last reply
    0
    • M Mohammadreza99A

      I have a class MyClass wich starts a QThread class named Worker. I have a vriable in MyClass that I want to access from child thread. Is this possible and if yes, how?

      class MyClass{
      public:
          MyClass(){
               thread = new Worker(this);
              thread->start();
         }
      private:
          Worker *thread;
         int vriable; // this the vriable that I want to access from child thread
      };
      
      class Worker : public QThread {
          Q_OBJECT
      public:
          Worker(QOject *parent = 0) : QThread(parent){}
          void run() {
                // I want to access vriable here
         }
      };
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mohammadreza99A Why do you want to access a private variable from another class?
      Don't do that, use getter/setter.
      When using threads you would need to synchronise the access to avoid race conditions.
      Better would be to use signals/slots to exchange data between MyClass and Worker.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

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