Qt QTime does not work on exe!



  • Hello everyone!

    I'm using Windows Qt Creator 3.2.2 based on Qt 5.3.2 (MSVC 2010, 32bit).

    In my application I have to do some function in "x" seconds. During these "x" seconds I need to do some other functions every "y" milliseconds. Those "y" ms have to be "y", I gotta be very precised!

    I can wirte down some sample code of my app:

    mytimer.cpp
    @
    Mytimer::Mytimer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Mytimer)
    {
    ui->setupUi(this);

    }

    Mytimer::~Mytimer()
    {
    delete ui;
    }

    void Mytimer::on_start_btn_clicked()
    {
    ui->start_btn->setDisabled(true);

    t = new timerThread(this);
    t->text = ui->text;
    // total time in ms
    t->totTime = (ui->tottime->value())*1000;
    //interval time in ms
    t->interval = ui->interval->value();
    connect(t,SIGNAL(reset()),this,SLOT(reset()));
    t->start();
    

    }

    void Mytimer::reset(){
    ui->start_btn->setDisabled(false);
    }
    @

    timerthead.cpp
    @
    timerThread::timerThread(QObject *parent) :
    QThread(parent)
    {
    }

    void timerThread::run(){

    long time = 0;
    QTime start_time = QTime::currentTime();
    QTime measure = QTime::currentTime();
    long step = interval;
    
    while(time<totTime){
    
        time = start_time.msecsTo(QTime::currentTime());
    
        // SOME FUNCTIONS
    
        if(time>=interval){
            //get the ms passed since last time we have been here
            long msec = measure.msecsTo(QTime::currentTime());
            QString s = QString::number(msec) + " msec";
            text->setText(s);
            measure = QTime::currentTime();
            interval+=step;
            //SOME FUNCTIONS
    
        }
    
        // SOME FUNCTIONS
    }
    
    emit reset();
    

    }
    @

    I'm gonig to split my problem in 3 cases:

    "build and run" from Qt Creator --> WORKS (it is printed the exact same value as the one I set on the Interval field)

    run the "myapp.exe" file with Qt Creator opened --> WORKS (it is printed the exact same value as the one I set on the Interval field)

    run the "myapp.exe" file with Qt Creator closed --> DOES NOT WORK (it is printed 0 , 15 or 16 if I set a value lower than 15 ms in the Interval field otherwise the printed values are always around the set interval but never the same)

    Could you please help me understand why!?

    It seems like the app is able to use a precised timer only when I have Qt Creator opened.


Log in to reply
 

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