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. useage of QfutureWatcher in lambda function
Forum Updated to NodeBB v4.3 + New Features

useage of QfutureWatcher in lambda function

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 660 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
    Mintogo
    wrote on last edited by
    #1

    Hi

    I create qt connection like below

    QObject::connect(&my_class, &my_class::signal,
    		[=](bool result, QString string) 
    		{
                    // do something...
    
    		QFutureWatcher<QByteArray>* futureWatcher = new QFutureWatcher<QByteArray>();
    
    		QObject::connect(futureWatcher, &QFutureWatcher<QByteArray>::finished,
    			&my_class, &my_class::slot);
    
                   _watcher_list.push_back(futureWatcher);
    
    		QFuture<QByteArray> future = QtConcurrent::run(static_function);
    
    		futureWatcher->setFuture(future);
    		}
    
                    // do something...
    	);
    

    After finish work static_function, finished signal not emitted.

    Did I use it wrong?

    JonBJ KroMignonK 2 Replies Last reply
    0
    • M Mintogo

      Hi

      I create qt connection like below

      QObject::connect(&my_class, &my_class::signal,
      		[=](bool result, QString string) 
      		{
                      // do something...
      
      		QFutureWatcher<QByteArray>* futureWatcher = new QFutureWatcher<QByteArray>();
      
      		QObject::connect(futureWatcher, &QFutureWatcher<QByteArray>::finished,
      			&my_class, &my_class::slot);
      
                     _watcher_list.push_back(futureWatcher);
      
      		QFuture<QByteArray> future = QtConcurrent::run(static_function);
      
      		futureWatcher->setFuture(future);
      		}
      
                      // do something...
      	);
      

      After finish work static_function, finished signal not emitted.

      Did I use it wrong?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Mintogo said in useage of QfutureWatcher in lambda function:

        QFuture<QByteArray> future = QtConcurrent::run(static_function);
      
        futureWatcher->setFuture(future);
        }
      

      I'm not an expert (never done this stuff), but future is a local variable in the lambda body which goes out of scope and is destroyed immediately after the setFuture(future) statement, which doesn't sound right to me...?

      P.S.
      Just found effectively the same question with the same answer as I have given at https://stackoverflow.com/a/43458370/489865.

      M 1 Reply Last reply
      2
      • JonBJ JonB

        @Mintogo said in useage of QfutureWatcher in lambda function:

          QFuture<QByteArray> future = QtConcurrent::run(static_function);
        
          futureWatcher->setFuture(future);
          }
        

        I'm not an expert (never done this stuff), but future is a local variable in the lambda body which goes out of scope and is destroyed immediately after the setFuture(future) statement, which doesn't sound right to me...?

        P.S.
        Just found effectively the same question with the same answer as I have given at https://stackoverflow.com/a/43458370/489865.

        M Offline
        M Offline
        Mintogo
        wrote on last edited by
        #3

        @JonB Thank you, I will take a look :)

        1 Reply Last reply
        0
        • M Mintogo

          Hi

          I create qt connection like below

          QObject::connect(&my_class, &my_class::signal,
          		[=](bool result, QString string) 
          		{
                          // do something...
          
          		QFutureWatcher<QByteArray>* futureWatcher = new QFutureWatcher<QByteArray>();
          
          		QObject::connect(futureWatcher, &QFutureWatcher<QByteArray>::finished,
          			&my_class, &my_class::slot);
          
                         _watcher_list.push_back(futureWatcher);
          
          		QFuture<QByteArray> future = QtConcurrent::run(static_function);
          
          		futureWatcher->setFuture(future);
          		}
          
                          // do something...
          	);
          

          After finish work static_function, finished signal not emitted.

          Did I use it wrong?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @Mintogo said in useage of QfutureWatcher in lambda function:

          After finish work static_function, finished signal not emitted.
          Did I use it wrong?

          What are you doing during "do something"?
          I hope there is no forever loop there. This would lock the event loop and signal could not be done.

          By the way, I would change to code to this:

          QObject::connect(&my_class, &MyClassName::signal,
             // capture "my_class" per reference and NOT as copy!!
              [=, &my_class](bool result, QString string) {
                  // do something...
          
                  QFutureWatcher<QByteArray>* futureWatcher = new QFutureWatcher<QByteArray>();
          
                  QObject::connect(futureWatcher, &QFutureWatcher<QByteArray>::finished,
                      &my_class, 
                      // capture "my_class" per reference and NOT as copy!!
                      [&my_class, futureWatcher]() {
                          my_class->slot(futureWatcher->result());
                          futureWatcher->deleteLater(); // free memory
                      }
                  );
          
                  futureWatcher->setFuture(QtConcurrent::run(static_function));
              }
          
              // do something...
          );
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          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