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. Changing Classes to Using Thread
Forum Updated to NodeBB v4.3 + New Features

Changing Classes to Using Thread

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.7k Views 1 Watching
  • 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
    mjzarrin
    wrote on last edited by A Former User
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Depending on what you have QtConcurrent might be more interesting.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        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.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          kshegunovK 1 Reply Last reply
          0
          • M Offline
            M Offline
            mjzarrin
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS SGaist

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

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by kshegunov
              #6

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

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mjzarrin
                wrote on last edited by
                #7

                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.

                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