Running a function in another thread in QT

In Qt, application code Class Ahas one member method, for example method1(). I want to call this method in another member function method2()and run mehtod1()in another thread. But what I found from the qt documentation is below.

  • We inherit the new class MyThread(suppose) of QThread.
  • Override the function method run()with the required code.
  • Create an object MyThreadin Class A, and then call the launch function wherever you want.

But the above seems a bit complicated. Is there any mechanism in Qt so that I can immediately create a new one QThread(without inheritance) in mine method1()and start method2()with this thread, and then return to method1()after completion of execution?

Please let me know if I do not understand in my question.

+5
source share
2 answers

Yes, there is a way how you want.

This article will help you understand why this is not the right way to inherit from QThread: http://blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong/

In this article, you will learn how to use QThread really simple: http://blog.qt.digia.com/blog/2006/12/04/threading-without-the-headache/

+10
source

You can use QObject slots and signals or event support in conjunction with streams.

, QObject, /, , QObject. QObject:: moveToThread.

QCoreApplication:: postEvent , .

QObjects Qt ( " QObjects" ).

, "" .

+3

All Articles