我试图每隔这么多毫秒保持一个运行时间戳。这最终将附加到温度读数上。现在,时间戳仅在我的程序的其余部分运行后显示,我需要它实时填充。我使用QTimer的更新代码是
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT());
//delay set to space out time readings, can be adjusted
timer->start(750);
//Gets the time
QTime time = QTime::currentTime();
//Converts to string with chosen format
QString sTime = time.toString("hh:mm:ss:ms");
//displays current time in text edit box
ui->tempTimeNoHeatMode->append(sTime);
//Pre heat turns on for selected # of seconds
digitalWrite(2,1);
delay(preHeat * 1000);
digitalWrite(2,0);
//Set loop to run while flow time is not 0
while(flowTime > 0)
{
displayCurrentTime();
//set zero pin to be high while flowtime is more than 0
digitalWrite(0,1);
flowTime--;
// set second pin LED to flash according to dutyCycle
digitalWrite(2,1);
delay(onTime);
digitalWrite(2,0);
delay(offTime);
}
//turn zero pin low after flow time reaches 0
digitalWrite(0,0);
}
代码控制两个LED代表我项目后期的加热器和水流。我正在使用QtCreator 4
。
答案 0 :(得分:0)
UI未更新的原因是您永远不会将控制权交给事件循环,因此在您的代码执行完之后,主线程才有机会更新UI。
这里有两种可能的解决方案: