首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qtimer迭代次数

Qtimer迭代次数
EN

Stack Overflow用户
提问于 2017-01-09 01:11:25
回答 1查看 207关注 0票数 0

也许我想得太多了,但是在迭代和qtimer方面需要一些帮助。我有以下QTimer代码和函数(我已经尽可能地简化它,如果某些语法错误,很抱歉):

代码语言:javascript
复制
    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运行此函数时,它都会重置。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-01-09 01:13:42

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

代码语言:javascript
复制
std::once_flag flag;

void MainWindow::whateverfunction()
{
checksomething();
if (checksomething() == 1)
{
    std::call_once(flag, [this](){
        dosomething1();
    };
    dosomething2();
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41535402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档