In my game I need a stopwatch to measure and show the elapsed time.
For this purpose I made a simple widget:
    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);
}
But when I run my game the CPU usage is at about 13% on a 2,5Ghz i5. I know this is not problematic but it sure is ridiculous for a stupid clock.
Am I doing it completely wrong or is this common practice ?!
Many thanks in advance.
Start(1) sets the timer to trigger every millisecond
You then want to format a string and print it on the screen 16times faster than the screen is probably updating anyway
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论