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. I added a progress bar in the GUI window, I want to create a thread, in this thread can control the position of the progress bar GUI window properti

I added a progress bar in the GUI window, I want to create a thread, in this thread can control the position of the progress bar GUI window properti

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

    I added a progress bar in the GUI window, I want to create a thread, in this thread can control the position of the progress bar GUI window properties? through the MainWindow *AA defined in the thread pointer can control?

    Qt中国群 :218967017、218967042
    python中国群:133067664

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      In GUI window create a thread and a progress bar then connect your thread object with a "setValue slot":http://qt-project.org/doc/qt-5/qprogressbar.html#value-propof of QProgressBar in GUI window.

      In the thread emit a signal to update QProgressBar.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Soraltan
        wrote on last edited by
        #3

        I guess this should work. A bit more info on the background:

        There are queued and non-queued signal-slot connections in Qt. Furthermore each QObject has a QThread it belongs to. When sender and receiver of a signal-slot connection belong to different QThreads the connection has to be queued to ensure thread-safty.

        When calling "QObject::connect":http://qt-project.org/doc/qt-4.8/qobject.html#connect-3 a connection where sender and receiver belong to different QThreads is established as a queued connection by default (see "Qt:ConnectionType":http://qt-project.org/doc/qt-4.8/qt.html#ConnectionType-enum). I never tried to establish a connection where one of the parts (sender/receiver) is actually a QThread, but I guess a QThread should "belong to itself", shouldn't it?

        Hope it helps.

        Soraltan

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Soraltan
          wrote on last edited by
          #4

          Ok, maybe I was wrong. Maybe a QThread belongs to the main thread and not to the thread it represents. I didn't find section in the docu really stating that, but "Qt Doc":http://qt-project.org/doc/qt-4.8/threads-qobject.html states:

          bq. The child of a QObject must always be created in the thread where the parent was created. This implies, among other things, that you should never pass the QThread object (this) as the parent of an object created in the thread (since the QThread object itself was created in another thread).

          So QThread would require you to make this connection queued manually (as the default in this case would be a non-queued connection).

          @
          connect(pointer_to_thread,
          SIGNAL(signal_for_new_progress(int)),
          pointer_to_bar,
          SLOT(setValue(int)),
          Qt::QueuedConnection)
          @

          Anyhow, the better way would probably be to have a QObject (not QThread) which emits the signal_for_new_progress (as I called it). You could "move":http://qt-project.org/doc/qt-4.8/qobject.html#moveToThread this to the QThread and make a normal connect (which would then default to a queued connection).

          The general idea for threading in Qt is to not overwrite the run method (as you would usually do). Creating a QThread and calling start() starts a brand new Qt Event Loop within that thread. You then move QObjects which do your work to the QThread and trigger their actions via (Queued) signal-slot connections. See at "Threads and QObjects":http://qt-project.org/doc/qt-4.8/threads-qobject.html for more info.

          I personally really like this approach. Threading is always kind of error prone (except maybe for experienced thread-users) and threading-errors are hard to debug. The Qt threading approach avoids a bunch of typical threading pitfalls.

          Best

          Soraltan

          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