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. simple timer in main or separate thread
Forum Update on Monday, May 27th 2025

simple timer in main or separate thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
timer thread
5 Posts 4 Posters 1.1k 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.
  • A Offline
    A Offline
    another_one
    wrote on last edited by
    #1

    Hello!
    Im a beginner in qt, wants to receipt data from uart and to do it i need perodically monitor the receive buffer. I cant do it in while(1) or from button event. I ve seen many examples but all of these doesnt compile and I m not figureout where is a problem yet. Can somebody give simple example?, please
    Thanks in advance

    jsulmJ Gojir4G 2 Replies Last reply
    0
    • A another_one

      Hello!
      Im a beginner in qt, wants to receipt data from uart and to do it i need perodically monitor the receive buffer. I cant do it in while(1) or from button event. I ve seen many examples but all of these doesnt compile and I m not figureout where is a problem yet. Can somebody give simple example?, please
      Thanks in advance

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @another_one Do you really think with the information you provided anybody will be able to help you?
      What exactly did you try? What was the error message? What example do you want to have? For timer examples you find already some in the documentation: http://doc.qt.io/qt-5/qtimer.html
      If something still doesn't work then please be more specific and show what you did and what errors you get.
      Regarding the title of your post: there is usually really no need to use a timer in a thread.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • A another_one

        Hello!
        Im a beginner in qt, wants to receipt data from uart and to do it i need perodically monitor the receive buffer. I cant do it in while(1) or from button event. I ve seen many examples but all of these doesnt compile and I m not figureout where is a problem yet. Can somebody give simple example?, please
        Thanks in advance

        Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by
        #3

        @another_one Hi,

        Don't use a QTimer, but either QSerialPort::readyRead() signal, which will notify you when data are available on the port.

        Qt already provide examples for QSerialPort (and for almost everything)
        I would suggest Terminal Example, Command Line Writer Async Example and Command Line Reader Async Example. Which seems to be close of what you want to achieve.
        Note that you can open these examples directly from QtCreator.

        1 Reply Last reply
        1
        • BuckwheatB Offline
          BuckwheatB Offline
          Buckwheat
          wrote on last edited by
          #4

          Hi @another_one , Welcome!

          Although @jsulm is correct about desiring more information, he could have been a little more tactful (I firmly believe what goes around comes around!) in conveying it. QSerialPort is your best bet as @Gojir4 explained. Use the dataReady to read the port.

          *** Be warned! SIGNALS and SLOTS work on the event queue in multiple threads. You can freeze out (or delay processing) if you do a lot of busy work in large routines.

          One technique I do use is to start a timer when I am waiting on a response from the serial port as a timeout. This is helpful for detecting loss of signal. Stop/reset the timer when data arrives depending on your use case.

          If you need to use the serial port in the background and not worry about interference with the user interface (either in responsiveness or port data processing) then run the serial port in a worker thread. The best thread option is the worker thread. See Maya's blog (https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/). I use this technique for both QSerialPort and QCanBUs.

          Dave Fileccia

          jsulmJ 1 Reply Last reply
          2
          • BuckwheatB Buckwheat

            Hi @another_one , Welcome!

            Although @jsulm is correct about desiring more information, he could have been a little more tactful (I firmly believe what goes around comes around!) in conveying it. QSerialPort is your best bet as @Gojir4 explained. Use the dataReady to read the port.

            *** Be warned! SIGNALS and SLOTS work on the event queue in multiple threads. You can freeze out (or delay processing) if you do a lot of busy work in large routines.

            One technique I do use is to start a timer when I am waiting on a response from the serial port as a timeout. This is helpful for detecting loss of signal. Stop/reset the timer when data arrives depending on your use case.

            If you need to use the serial port in the background and not worry about interference with the user interface (either in responsiveness or port data processing) then run the serial port in a worker thread. The best thread option is the worker thread. See Maya's blog (https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/). I use this technique for both QSerialPort and QCanBUs.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Buckwheat Thanks for the hint - I agree with you :-)
            People often ask questions which are hard to understand or do not provide enough information (like in this thread), if you see such questions again and again it is easy to loose patience, especially if you have a bad day :-)
            @another_one Sorry if I was rude! Try to be more precise if you ask a question, then it is easier to answer it :-)

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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