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. Question about threads

Question about threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.6k 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
    Jeronimo
    wrote on last edited by Jeronimo
    #1

    What is better move the thread or create different threads?? For example in my case i have different windows and i want to start in each one one different thread because i want to do different things, in one show list of numbers in other show one counter with qlabel and in the other thing and all heridate of one called main window. What do you think and what recommend me in this case. Sorry again a lot and sorry if the question is a little stupid.

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

      Hi
      No questions are stupid here :)
      A window cannot be in another thread.
      All GUI stuff is in same main thread.
      So your calculation should in the other/new thread.

      For a counting label, a QTimer can be used.

      Threads are mostly used for something that takes a long time to finish
      or a helper function that need to run all the time. like reading from a device.

      So can you tell a bit more what these different windows will do and why you think they need a thread to do it?

      J 2 Replies Last reply
      1
      • mrjjM mrjj

        Hi
        No questions are stupid here :)
        A window cannot be in another thread.
        All GUI stuff is in same main thread.
        So your calculation should in the other/new thread.

        For a counting label, a QTimer can be used.

        Threads are mostly used for something that takes a long time to finish
        or a helper function that need to run all the time. like reading from a device.

        So can you tell a bit more what these different windows will do and why you think they need a thread to do it?

        J Offline
        J Offline
        Jeronimo
        wrote on last edited by
        #3

        @mrjj for example i will have one father and different son's but at the same time if i need to create that one son have different son i need to implement new thread. i more or less understand one thread it's like a helper function for example if one window count a lot like (10000000) then you can create one thread to avoid that the main window are busy solving this issue. So with threads you avoid problems like this and many others. But i am starting with threads because they are very useful in GUI windows. So my question is: I have one window and has different son's. But one son has different son's too. I need to use two threads? Is better move the thread or create different threads. More or less these are my questions now because i'm beginner. Sorry if i asked bad or something. thx

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          No questions are stupid here :)
          A window cannot be in another thread.
          All GUI stuff is in same main thread.
          So your calculation should in the other/new thread.

          For a counting label, a QTimer can be used.

          Threads are mostly used for something that takes a long time to finish
          or a helper function that need to run all the time. like reading from a device.

          So can you tell a bit more what these different windows will do and why you think they need a thread to do it?

          J Offline
          J Offline
          Jeronimo
          wrote on last edited by
          #4

          @mrjj sorry other question if i did one slot and this do slow main window or busy i can do one thread for this slots and can finish their activities sorry again and thx!!

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Threads is normally only needed if you do heavy calculations.

            If you just want to count , then using a qtimer is much easier and faster.

            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            public:
                ...
            
            private slots:
                void timer_image_function();
            
            };
            
            in mainwin::constructor
            connect(timer, SIGNAL(timeout()), this, SLOT(timer_image_function()));
            timer->start(1000);   // 1 sec
            
            
            void MainWindow::timer_image_function()
            {
              // count and set to some label
            }
            
            
            
            • But one son has different son's too. I need to use two threads?
              Depends on what the thread is doing.
              Again, the window will stay in the GUI thread so its not the window running in the thread.
              It will be some worker object.
              So if son need its own worker object then yes , it should create thread too.

            Before trying to use threads, you should read about it
            http://doc.qt.io/qt-5/thread-basics.html
            https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

            So lets sum up

            • GUI objects cannot be moved to a new thread. One must use a worker object and use signals
              to talk to any GUI.

            • QTimers are a good alternative to threads for light running concurrent jobs.

            • Threads are complex and doc must be read and understood or it will be hard to get working.

            • if i did one slot and this do slow main window or busy i can do one thread for this slots and can finish their activities
              No idea what you are saying here. :)
            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved