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]QThread, the right way and sleep()
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QThread, the right way and sleep()

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

    Hi!

    This time when I was using threads I've done it "the right way" ( creating object, connecting it with signals with its thread and moving it to thread).

    But now I've a slight problem. I need QThread()::msleep(). Which I cannot access through signals and slots nor directly by calling msleep() because is protected.

    Any proposal how can I solve this? or should I inherit QThread and forget about "done right"?

    I was thinking of deriving from QThread an creating slot for delay, or deriving QThread and placing everything in thread as I already did once in another program.

    How is this usually done ( the correct way)?

    Regards,
    Jake


    Code is poetry

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cyao
      wrote on last edited by
      #2

      Here is how I did for Sleep:

      If your class is derived from QThread, you can add a static public method like this:
      @
      static void Sleep(unsigned long ms) { QThread::msleep(ms); }
      @
      Then you can call MyThread::Sleep(1000) to sleep one second. If your clss is not a subclass of QThread and you want it to have a Sleep method, you can add a QThread derived class embedded in your class which has a static method Sleep as above. Then you define a public static method Sleep in your class that deligates to Sleep method of embedded class:
      @
      void MyClass::Sleep( unsigned long ms)
      {
      MyClass::MySleepThread::Sleep(ms);
      }
      @
      Hope it works for you.

      [Edit: Added @ tags around code -- mlong]

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jake007
        wrote on last edited by
        #3

        I did as you did in your first solution.
        I derived QThread and reimplemented all functions for sleep (static), so that I can use them in my worker class.

        Thanks.

        Marked as solved.

        Regards,
        Jake


        Code is poetry

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Just for your interest, the sleep functions are public in Qt 5

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          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