0

To log the user in I have a method in the following classclass ProbeConnection : public QObject, QRunnable.

The method to login is called run as is expected for the QRunnable class.

void ProbeConnection::run(QString username, QString password, QString ipAddress, bool fts, bool tts, bool pcap, bool events, bool os_meas)

The method needs to run in a different thread as it is blocking the gui when there is no response (the method is used for logging in). I tried to get it to run with QtConcurrent by running:

QFuture<void> future = QtConcurrent::run(this->run(username, password, ipAddress, fts, tts,  pcap,  events,  os_meas));

But the message i get is:

no matching function for call to 'run'

How can I start the run method when it takes parameters?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169

1 Answers1

0

When Calling QtConncurrent::run with a member function you need correct QtConncurrent::run syntax as answered in this thread

As highlighted in Multi-threading Technologies in Qt ; QRunnable would be used when you want to reuse existing threads in a QThreadPool , On the other hand QtConcurrent would usually be used for its high level API, and returns a QFuture object to retrieve results from threads.

To use QRunnable::run , reimplemet it in subclass and use QThreadPool::start() to put the QRunnable in the QThreadPool's run queue ... NOT by calling QtConcurrent::run

In your code, both ProbeConnection::run and QFuture<void> are void .. and I see no reason for using QtConcurrent .. or use it directly without a QRunnable.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • > "I see no reason for using QtConcurrent". To easily run a function in another thread seems like a good reason to me. – GrecKo Aug 30 '19 at 07:59
  • @GrecKo, `QRunnable::run` by itself is designed to run in `QThreadpool` as described, not within a `QtConcurrent` ; moreover, whats the point when the function is `void` , how would `QFuture QtConcurrent::run` be different? – Mohammad Kanan Aug 30 '19 at 10:48
  • Simpler to use. `QtConcurrent::run(foo, &Foo::function, parameter)` vs having to inherit from `QRunnable`, overriding the `run` function and starting that in a `QThreadPool`. – GrecKo Aug 30 '19 at 11:10
  • @GrecKo, that's right, it could be simpler, yet other _statics_ are also simple. don't know why OP opted for `QRunnable::run` , the essence of my answer is how to implement each and not mix them. https://doc.qt.io/qt-5/threads-technologies.html#choosing-an-appropriate-approach – Mohammad Kanan Aug 30 '19 at 13:06