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. Multithreading help... If thats what i need?
QtWS25 Last Chance

Multithreading help... If thats what i need?

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

    I need to run a file compressor from GUI but i also need to show the user that something is happening while the file is compressing.I have a dial that is supposed to start spinning when the compression starts and stop spinning when the compression is complete.The method to spin the dial is on a timer ,i made the timer start ,then called the compression method ,the problem is ,once the compression starts the dial stops spinning.After the compression the dial spins again ,i need the dial to spin DURING compression,please help.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      LinusA
      wrote on last edited by
      #2

      In your case, it should be enough to call
      @
      qApp->processEvents();
      @
      occasionally during compression. If you "own" the compression code, you can insert it somewhere in an inner loop -- but make sure it's not called every single iteration, otherwise performance goes down too much.

      If you've got a counter somewhere, that runs up the number of bytes or whatever, and loop index is i, you could for example do this:
      @
      if (i % 1024 == 0) {
      qApp->processEvents();
      }
      @
      This would only execute every 1024th time.

      If you compress the file using a single "external" call however, like qCompress, I'd suggest you use QtConcurrent. Look at http://doc.qt.nokia.com/latest/qtconcurrentrun.html . I'd suggest you do something like:
      @
      QFuture<QByteArray> future = QtConcurrent::run(qCompress, myByteArrayDatat);
      future.waitForFinished();
      QByteArray uncompressed = future.result();
      @
      Then your event loop keeps running during compression...

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        Several of these techniques are explained in my document here http://developer.qt.nokia.com/wiki/Threads_Events_QObjects .

        Btw:
        [quote author="LinusA" date="1312149978"]I'd suggest you do something like:
        @
        QFuture<QByteArray> future = QtConcurrent::run(qCompress, myByteArrayDatat);
        future.waitForFinished();
        QByteArray uncompressed = future.result();
        @
        Then your event loop keeps running during compression...
        [/quote]

        Read that again. waitFor - event loop keeps running. Do you see it?

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • L Offline
          L Offline
          LinusA
          wrote on last edited by
          #4

          [quote author="peppe" date="1312153167"]
          Read that again. waitFor - event loop keeps running. Do you see it?[/quote]
          Oups, silly mistake, thanks for pointing out. Of course this would NOT run the event loop. Better use a "QFutureWatcher":http://doc.qt.nokia.com/latest/qfuturewatcher.html . Great wiki page, peppe!

          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