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. [Solved]Transfer parameter between QThread
QtWS25 Last Chance

[Solved]Transfer parameter between QThread

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 6.0k 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.
  • I Offline
    I Offline
    Ivan1120
    wrote on last edited by
    #1

    Hello everyone:
    I had a problem.I wrote my code like following, how do I transfer parameter into this thread, when it start?? Thanks in advance.
    @
    Qthread mthread;
    mthread.start();
    @

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

      Hi,

      You have to setup your thread before starting it. If you need to change things at run time, you need setters.

      But before that, are you sure you need a QThread derivative ? Wouldn't a worker object be better ?

      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
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hmm, the QThread object itself is only a wrapper for what SGalst points out. You need a worker object in the thread to be run. Or you need to derive from QThread (but this is discouraged). So have a look at this post:
        "Thread":http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
        Then to pass data from one thread to another use signal/slots or mutex protected variables with setter/getter functions.

        Greetz, Jeroen

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

          There's also "this one":http://woboq.com/blog/qthread-you-were-not-doing-so-wrong.html

          This really depends on the use case. Most of the time the worker object is the right solution, but not always.

          Also, depending on the job, QtConcurrent suits even better.

          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
          0
          • I Offline
            I Offline
            Ivan1120
            wrote on last edited by
            #5

            Thanks all your suggestions, Actually, I wrote a program is GUI, and whenever user clicked the button, something would be done and displayed to the QTextEdit of GUI. But If I don't use thread, the GUI would be locked because the thread of GUI is busy.It couldn't respond to user.So should I put work item into worker, then use singnal & slot pass information to GUI that could display to user??

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

              Yes but be careful when you start using multithreading.

              Please read the examples and the documentation from Qt's various threading classes (and if you're nor familiar with the thread subject, a good book about them is really a good idea). And only then start designing your application using threads.

              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
              0
              • I Offline
                I Offline
                Ivan1120
                wrote on last edited by
                #7

                Thanks very much for your help : )

                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