QTimer setInterval integer value



  • 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!


  • Qt Champions 2018

    Can you try:

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

  • Qt Champions 2018

    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 Champions 2018

    @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.



  • 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.



  • @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...


  • Qt Champions 2018

    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


Log in to reply
 

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