Important: Please read the Qt Code of Conduct -

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

  • 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{
             thread = new Worker(this);
        Worker *thread;
       int vriable; // this the vriable that I want to access from child thread
    class Worker : public QThread {
        Worker(QOject *parent = 0) : QThread(parent){}
        void run() {
              // I want to access vriable here

  • Lifetime Qt Champion

    @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.

Log in to reply