Running two interval timers on QT

  • Hello to all,
    Thanks for the "helping brains" here in this forum.
    Now I have a problem using Qt-Creator to develop a program which uses two timers.
    Here the problem:
    I want to read data from a 868Mhz receiver.
    One timer is to read on bit from the device in a 200µs interval in a sequence of ca. 100 bit.
    The second one is to make a delay of 10 minutes for the next sequence.
    Now I found this program which would solve this problem. I make it with this command on the Raspberry: Compile with: gcc -Wall -o timer1 -lrt timer1.c
    But it cannot be used by Qt-Creator. [0_1565542414226_main.cpp](Uploading 100%) It cannot find the two functions timer_create and timer_settime

    #include <QCoreApplication>
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <signal.h>
    timer_t Timerid1;
    timer_t Timerid2;
    int count1 = 0;
    int count2 = 0;
    /* Timer erzeugen und starten
     * Timerid: die zurueckgegebene ID des Timers
     * sek: Wartezeit Sekundenanteil
     * msek: Wartezeit Millisekundenanteil
    void start_timer(timer_t *Timerid, int sek, int msek)
      struct itimerspec timer;
      timer.it_value.tv_sec = sek;
      timer.it_value.tv_nsec = msek * 1000000;
      timer.it_interval.tv_sec = sek;
      timer.it_interval.tv_nsec = msek * 1000000;
      timer_create (CLOCK_REALTIME, NULL, Timerid);
      timer_settime (*Timerid, 0, &timer, NULL);
      printf("Timer gestartet, ID: 0x%lx\n", (long) *Timerid);
    /* Anhalten eines durch Timerid identifizierten Timers
     * durch Setzen aller Zeiten auf 0
    void stop_timer(timer_t *Timerid)
      struct itimerspec timer;
      timer.it_value.tv_sec = 0;
      timer.it_value.tv_nsec = 0;
      timer.it_interval.tv_sec = 0;
      timer.it_interval.tv_nsec = 0;
      timer_settime (*Timerid, 0, &timer, NULL);
    /* Signalhandler für alle Timer
     * Unterscheidung der Timer anhand von tidp
    void timer_callback(int sig, siginfo_t *si, void *uc)
      timer_t *tidp;
      tidp = &si->si_value.sival_ptr;
      printf("Signal: %d, ID: %p ", sig, tidp);
      if (tidp == Timerid1)
        printf(", Count 1: %d\n",count1++);
      if (tidp == Timerid2)
        printf(", Count 2: %d\n",count2++);
    int main(int argc, char *argv[])
        struct sigaction sa;
        /* callback-Handler installieren */
        memset(&sa, 0, sizeof (sa));
        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = timer_callback;
        if (sigaction(SIGALRM, &sa, NULL) == -1)
        /* timer starten */
        /* Programm macht irgendwas */
        QCoreApplication a(argc, argv);
        while(count1 <= 5);
        /* Fertig, Timer stoppen */
        return a.exec();

    when I try to compile this program I get two errors:
    /home/kurt/Qt-Projecte/ZweiTimer/main.cpp:32: error: undefined reference to timer_create' /home/kurt/Qt-Projecte/ZweiTimer/main.cpp:33: error: undefined reference totimer_settime'
    I have not found any solution.
    If somebody has an idea whats wrong please help me.

  • Hello, here I'm again!
    I think this is a problem using the timer function.
    I tried to make it with QTimer it works but it can handle only milli seconds. But I need an interval of 200 micro seconds. The second interval is 15 minutes.
    So I still need a solution using POSIX timer. And here is my problem.
    My configuration is :
    UBUNTU 18 running as Hyper-v machine on Windows 10
    0_1565604212648_Screenshot from 2019-08-12 12-01-17.png

    It was installed using this Description:
    installation guide in german
    It seems there is something missing.
    Please could somebody give me a tip
    Thanks a lot in advance.

  • @k-str

    The "problem" is not with Qt creator, but the actual Qt libs you are using for your application.

    Qt creator is an IDE (Integrated Development Environment) which happens to be based on Qt libs as well. Creator 4.9.2 is using Qt 5.12.4, but you may use any other Qt libs version as installed on your machine. The version may be higher or lower or the exactly the same. However, those are strictly separated.

    Under projects on the left pane in creator you will find Build & run, whzere you can see the actual version used for compilation. Also lower you will find an icon with the project. When you click on this you see the actual version being used and if it is release or debug mode.

    Finally to your question. AFAIK QTimer is not meant for what you are trying to do. As you found out it has only granularity of 1 ms, but you cannot rely on this.
    Timer events might be blocked during other events running in the event loop. Timer events will be executed when they are encountered in the event loop.

  • Hi @koahnig ,
    thanks for your answer.
    I checked some on my configs.
    It seems I have on my raspberry Qt version 5.7.1
    on the UBUNTU toolchain I have Qt version 5.12.3
    This means I have to install QT-Creator and the toolchain completely new?

  • @k-str

    Not necessarily. It depends on your intentions. This might be perfectly fine. However, I tend to have same versions everywhere. IMHO otherwise one gets confused.

    When you use the Ubuntu install to do a cross-compile for RPi, it is recommended to have the same kit/toolchain for both. I do not know if there is a place to get RPi pre-compiled versions. You might have to cross-compile a newer Qt lib version for RPi or the other way around you need to downgrade on Ubuntu to Qt 5.7.1.

  • @koahnig
    Thanks for your answer.
    I think I install everything completely new.
    This means I have to install the following Items:

    • QT-Creator
    • qt-everywhere
    • a rpi image raspbian stretch which fits to qt-everywhere

    What are the actual versions of this items?

  • @k-str

    It depends how you have installed the components before. The easiest is IMHO the online installer found on e.g. the open-source version on the right.

    Potentially you have used in the past and you have already maintenance tool available. This is typically updating for the newest Qt creator anyway.

    When you have already a prebuilt rpi Qt lib version you can try to match this version by installing it on your linux desktop. Otherwise you can download the linux open source archive and build the rpi Qt lib version based on . There are also other tutorials available based on different Qt versions. I expect that a fairly new tutorial would work also with latest Qt lib code.

    As indicated above I suggest to install the same version on linux desktop and use it also for rpi. Typically it is a matter of taste what people prefer. However, using Qt 5.12.4 seem to be good choice because it is relatively new and has long-term support.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.