Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QTimer setInterval integer value

QTimer setInterval integer value

Scheduled Pinned Locked Moved Solved C++ Gurus
7 Posts 5 Posters 2.0k 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.
  • K-StrK Offline
    K-StrK Offline
    K-Str
    wrote on last edited by VRonin
    #1

    Hello,
    in the past I got good answers to my questions and so I would like to ask you again:
    I think this is only a little problem.
    The qt version is: Qt Creator 4.5.0 based Qt 5.10.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
    The cross compiler is for a Raspberry.

    I would like to start a interval timer.
    The value is read as std:basic_string from a file.
    Then I convert this string to a integer value.
    But when I execute the Program it stops.
    Her a part of the code (Timeout_ms is defined as a global variable:

        string Timeout_in;
        ifstream TimeoutInputFile;
        TimeoutInputFile.open("timeoutfile", std::ifstream::in);
        getline(TimeoutInputFile, Timeout_in);
        Timeout_ms = atoi(Timeout_in.c_str());
    //    Timeout_ms=2000;
        TimeoutInputFile.close();
        StepperMotorTimer = new QTimer(this);
        connect(StepperMotorTimer, SIGNAL(timeout()),this, SLOT(StepperTimeout()));
        printf("Timeout tim = %d ms\n",Timeout_ms);
        StepperMotorTimer->setInterval(Timeout_ms);
        StepperMotorTimer->start();
    

    When I assign the value Timeout_ms (her commented out) then it works.
    Please could you give me a hint whats wrong here?
    Thanks a lot!

    VRoninV aha_1980A jsulmJ 3 Replies Last reply
    0
    • K-StrK K-Str

      Hello,
      in the past I got good answers to my questions and so I would like to ask you again:
      I think this is only a little problem.
      The qt version is: Qt Creator 4.5.0 based Qt 5.10.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
      The cross compiler is for a Raspberry.

      I would like to start a interval timer.
      The value is read as std:basic_string from a file.
      Then I convert this string to a integer value.
      But when I execute the Program it stops.
      Her a part of the code (Timeout_ms is defined as a global variable:

          string Timeout_in;
          ifstream TimeoutInputFile;
          TimeoutInputFile.open("timeoutfile", std::ifstream::in);
          getline(TimeoutInputFile, Timeout_in);
          Timeout_ms = atoi(Timeout_in.c_str());
      //    Timeout_ms=2000;
          TimeoutInputFile.close();
          StepperMotorTimer = new QTimer(this);
          connect(StepperMotorTimer, SIGNAL(timeout()),this, SLOT(StepperTimeout()));
          printf("Timeout tim = %d ms\n",Timeout_ms);
          StepperMotorTimer->setInterval(Timeout_ms);
          StepperMotorTimer->start();
      

      When I assign the value Timeout_ms (her commented out) then it works.
      Please could you give me a hint whats wrong here?
      Thanks a lot!

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Can you try:

      • replacing atoi with stringstream: std::stringstream(Timeout_in) >> Timeout_ms;
      • output the value of Timeout_ms (qDebug() << Timeout_ms;)

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • K-StrK K-Str

        Hello,
        in the past I got good answers to my questions and so I would like to ask you again:
        I think this is only a little problem.
        The qt version is: Qt Creator 4.5.0 based Qt 5.10.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
        The cross compiler is for a Raspberry.

        I would like to start a interval timer.
        The value is read as std:basic_string from a file.
        Then I convert this string to a integer value.
        But when I execute the Program it stops.
        Her a part of the code (Timeout_ms is defined as a global variable:

            string Timeout_in;
            ifstream TimeoutInputFile;
            TimeoutInputFile.open("timeoutfile", std::ifstream::in);
            getline(TimeoutInputFile, Timeout_in);
            Timeout_ms = atoi(Timeout_in.c_str());
        //    Timeout_ms=2000;
            TimeoutInputFile.close();
            StepperMotorTimer = new QTimer(this);
            connect(StepperMotorTimer, SIGNAL(timeout()),this, SLOT(StepperTimeout()));
            printf("Timeout tim = %d ms\n",Timeout_ms);
            StepperMotorTimer->setInterval(Timeout_ms);
            StepperMotorTimer->start();
        

        When I assign the value Timeout_ms (her commented out) then it works.
        Please could you give me a hint whats wrong here?
        Thanks a lot!

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by aha_1980
        #3

        Hi @K-Str

        Based on your description, your bug must be here:

        string Timeout_in;
        ifstream TimeoutInputFile;
        TimeoutInputFile.open("timeoutfile", std::ifstream::in);
        getline(TimeoutInputFile, Timeout_in);
        Timeout_ms = atoi(Timeout_in.c_str());
        

        What could happen?

        • The file may not exist or not exist at the place you expect it
        • The file may contain empty lines before the one specifying the timeout
        • The file may contain one line, but it's not a valid integer

        If you can, fire up a debugger, otherwise add debug outputs. If functions provide return values (I guess open and getline do), check them.

        I have not verified that your code is good at all (I don't use STL much), but I'd rewrite it in Qt (not tested!):

        QFile file("timeoutfile");
        if (!file.open(QIODevice::ReadOnly))
          // error!
        
        QByteArray contents = file.readLine();
        bool ok = false;
        int timeout = contents.toInt(&ok);
        if (!ok)
          // error!
        
        // you can use timeout here
        

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        5
        • K-StrK K-Str

          Hello,
          in the past I got good answers to my questions and so I would like to ask you again:
          I think this is only a little problem.
          The qt version is: Qt Creator 4.5.0 based Qt 5.10.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)
          The cross compiler is for a Raspberry.

          I would like to start a interval timer.
          The value is read as std:basic_string from a file.
          Then I convert this string to a integer value.
          But when I execute the Program it stops.
          Her a part of the code (Timeout_ms is defined as a global variable:

              string Timeout_in;
              ifstream TimeoutInputFile;
              TimeoutInputFile.open("timeoutfile", std::ifstream::in);
              getline(TimeoutInputFile, Timeout_in);
              Timeout_ms = atoi(Timeout_in.c_str());
          //    Timeout_ms=2000;
              TimeoutInputFile.close();
              StepperMotorTimer = new QTimer(this);
              connect(StepperMotorTimer, SIGNAL(timeout()),this, SLOT(StepperTimeout()));
              printf("Timeout tim = %d ms\n",Timeout_ms);
              StepperMotorTimer->setInterval(Timeout_ms);
              StepperMotorTimer->start();
          

          When I assign the value Timeout_ms (her commented out) then it works.
          Please could you give me a hint whats wrong here?
          Thanks a lot!

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

          @K-Str said in QTimer setInterval integer value:

          Qt Creator 4.5.0 based Qt 5.10.0 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)

          Just as a note: this is not necessarily Qt version you're using. This say which QtCreator version you're using and which Qt version was used to build this QtCreator. To know which Qt version you're really using go to the Kit you're using and check there.

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

          1 Reply Last reply
          4
          • K-StrK Offline
            K-StrK Offline
            K-Str
            wrote on last edited by
            #5

            thank you for your tips.
            Hello aha_1980,
            I'm sorry but it does not work. It is still th same problem.
            Hello jsulm;
            I think this is Qt version 5.6.4 for Embedded Linux.

            JonBJ aha_1980A 2 Replies Last reply
            0
            • K-StrK K-Str

              thank you for your tips.
              Hello aha_1980,
              I'm sorry but it does not work. It is still th same problem.
              Hello jsulm;
              I think this is Qt version 5.6.4 for Embedded Linux.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @K-Str
              I understand you to be saying:

                  Timeout_ms = atoi(Timeout_in.c_str());
              

              crashes/stops/fails/doesn't behave right, while

                  Timeout_ms=2000;
              

              behaves perfectly.

              So why don't you debug out whatever is going on in your atoi(Timeout_in.c_str()), a bit at a time?

              Or are you saying something else?

              If you're having problems with atoi and c_str you could use Qt functions which work on QStrings...

              1 Reply Last reply
              4
              • K-StrK K-Str

                thank you for your tips.
                Hello aha_1980,
                I'm sorry but it does not work. It is still th same problem.
                Hello jsulm;
                I think this is Qt version 5.6.4 for Embedded Linux.

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                hi @K-Str,

                i gave you three points to check, have done this?

                you will need to debug this issue yourself, we cannot do it for you.

                regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                2

                • Login

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