繁体   English   中英

Qtimer迭代

[英]Qtimer iterations

也许我想得太多,但是在迭代和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运行此函数时,都会重置。 谢谢!

我建议用std :: call_once包装对dosomething1()的调用

std::once_flag flag;

void MainWindow::whateverfunction()
{
checksomething();
if (checksomething() == 1)
{
    std::call_once(flag, [this](){
        dosomething1();
    };
    dosomething2();
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM