也许我在思考它,但需要一些迭代和qtimer的帮助。我有以下QTimer代码和函数(我试图尽可能地简化它,抱歉,如果某些语法错误):
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(whateverfunction()));
timer -> start();
void MainWindow::whateverfunction()
{
checksomething();
if (checksomething() == 1)
{
dosomething1(); //function I want to run ONLY the first time checksomething() = 1
dosomething2(); //function I want to run the second,third,fourth,etc. time checksomething() = 1
}
else
{
donothing(); //if this function is run the count resets, meaning dosomething1() should be run again if checksomething() == 1 again-- but only the first time.
}
}
我怎样才能完成上述目标?我尝试引入一个控制变量,但每次通过QTimer运行此函数时都会重置。谢谢!
答案 0 :(得分:0)
我建议使用std::call_once
打电话给dosomething1()
std::once_flag flag;
void MainWindow::whateverfunction()
{
checksomething();
if (checksomething() == 1)
{
std::call_once(flag, [this](){
dosomething1();
};
dosomething2();
}
}