Changing Classes to Using Thread



  • Hello My Friends
    I am newbie in QT.
    I was developed a software with QT. It can wipe files and folders.
    overwriting files is time consuming operation and whenever big IO operation starts the application going to hang until IO finished.
    I think if the application is multi thread , the problem gone. I read qthread tutorials. it uses differ structure from what i write. for example pass qparent to defualt constructor and have a function with name "run()" that thread start from that.
    my question is if i want to change my codes that existed classes or function become multi thread what i need to do clearly .and is it force to rename all of my desired function be "run()" and with no argument.
    thank you all.


  • Lifetime Qt Champion

    Hi,

    Depending on what you have QtConcurrent might be more interesting.


  • Qt Champions 2016

    Hello,
    The accepted way of doing low-level multithreading in Qt is to subclass QObject, add your data processing functions as slots and connect them to the appropriate signals. Now, to have the slots be executed in your worker thread, your QObject subclass instance must live in that same worker thread. To do that, you call moveToThread() from the place where the object was created and pass your QThread object as a parameter. Be aware, that your QObject can not have a parent QObject when you move it (this is purposeful, not a bug). Be aware though that any function call that is not invoked through the Qt's event loop (meaning that is not a slot) either should be done from single thread or the function must be thread-safe. Here is a quick overview from the Qt's documentation http://doc.qt.io/qt-5/threads-technologies.html

    Kind regards.


  • Lifetime Qt Champion

    @kshegunov it depends on your needs, the worker object paradigm is not always the best or usable solution. The documentation of QThread explains both options and in which case you should use them.



  • thank you @SGaist and @kshegunov
    I will using QtConcurrent . I don't implement QObject in my classes.
    I think i have to change all of my class. now i'm working on a sample project to understand QtConcurrent . I will update this topic if i have success or fail.
    thank you again.


  • Qt Champions 2016

    @SGaist
    Yes, of course it depends on your needs, that's why I specifically wrote low-level mutithreading and attached a link with an overview of all available options, not for QThread. QtConcurent, although I personally prefer the low-level approach, has its uses and probably in many applications is the simpler and more robust way to go. As you said - depends what you need.

    @mjzarrin
    You are very welcome, and good luck!



  • Thank you very much.
    I used QtConcurrent, at first i changed all of my class to implement QObject and then just i need a line of code like bellow

    QFuture<void> test = QtConcurrent::run(&this->ObjectOfClass,&ClassName::Method,MethodParams);
    

    thank you again.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.