【问题标题】:QTime QTimer timeout() driven Stopwatch has high CPU usageQTime QTimer timeout() 驱动的秒表 CPU 占用率高
【发布时间】:2011-01-05 01:23:02
【问题描述】:

在我的游戏中,我需要一个秒表来测量和显示经过的时间。

为此我做了一个简单的小部件:

    ZuulStopwatchWidget::ZuulStopwatchWidget(QWidget *parent) :
    QWidget(parent)
{
        num = new QLCDNumber(this); // create the display
        num->setDigitCount(9);

        time = new QTime();
        time->setHMS(0,0,0,0); // set the time

        timer = new QTimer(this);

        connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
        i=0;
        QString text = time->toString("hh:mm:ss");
        num->display(text);
        //num->setStyleSheet("* { background-color:rgb(199,147,88);color:rgb(255,255,255); padding: 7px}}");
        num->setSegmentStyle(QLCDNumber::Flat); //filled flat outline
        //setStyleSheet("* { background-color:rgb(236,219,187)}}");

        layout = new QVBoxLayout(this);
        layout->addWidget(num);
        setMinimumHeight(70);
}

ZuulStopwatchWidget::~ZuulStopwatchWidget()
{
    // No need to delete any object that has a parent which is properly deleted.

}
void ZuulStopwatchWidget::resetTime()
{
    time->setHMS(0,0,0);
    QString text = time->toString("hh:mm:ss");
    num->display(text);
    i=0;
    stopTime();
}

void ZuulStopwatchWidget::startTime()
{
    //flag=0;
    timer->start(1);
}

void ZuulStopwatchWidget::stopTime()
{
    timer->stop();
}


void ZuulStopwatchWidget::showTime()
{
    QTime newtime;
        //if(flag==1)
                //i=i-1;
    i=i+1;
    newtime=time->addMSecs(i);
    QString text = newtime.toString("mm:ss:zzz");
    num->display(text);
}

但是当我运行我的游戏时,CPU 使用率在 2.5Ghz i5 上约为 13%。我知道这没有问题,但对于一个愚蠢的时钟来说确实很荒谬。

我这样做是完全错误的还是这是常见的做法?!

非常感谢。

【问题讨论】:

  • 您知道哪个函数会导致 CPU 时间激增吗?你能分析你的代码等吗?
  • 我绝对确定这个秒表小部件会导致问题。分析仅产生方法 ml_set_interrupts_enabled (马赫内核)占主导地位。我猜这些是计时器发送的超时信号。这个时钟的精度为一毫秒,但每秒仍然有 1000 个中断,这不是 CPU 密集型的吗? (我 15 岁的卡西欧手表可以用最小的电池做一年)

标签: c++ qt timer cpu-usage


【解决方案1】:

Start(1) 设置定时器每毫秒触发一次

然后你想格式化一个字符串并在屏幕上打印它比屏幕更新速度快 16 倍

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 2018-02-07
    • 2013-08-04
    • 1970-01-01
    • 2015-10-29
    • 2016-01-23
    • 2016-03-22
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多