本文介绍了QTimer变得更快,每个开始/停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 QTimer 平滑地更改标签的大小:当我将鼠标悬停在按钮上时,它应该会慢慢增长,然后慢慢崩溃

I'm using a QTimer to smoothly change the size of a label: it should slowly grow when I hover the mouse over a button, and slowly collapse (decrease it's size until it dissapears) when the mouse leaves the button.

我的表单类中有两个计时器:

I have two timers in my form's class:

QTimer oTimer, cTimer;//oTimer for expanding, cTimer for collapsing

在我的表单的构造函数中,我设置计时器的值,并连接按钮的 mouseOver mouseOut 我的表单的位置:

In my form's constructor, I'm setting the timer's values and connecting the button's mouseOver and mouseOut signals to my form's slots:

oTimer.setInterval( 25 );
cTimer.setInterval( 25 );

connect( ui.Button,
         SIGNAL(mouseEntered(QString)),
         this,
         SLOT(expandHorizontally(QString)) );
connect( ui.Button,
         SIGNAL(mouseLeft(QString)),
         this,
         SLOT(compactHorizontally(QString)) );

现在,在这些插槽中,我将相应的定时器连接到将逐渐改变大小的插槽,然后我启动计时器:

Now, in these slots I connect the corresponding timer to a slot that will gradually change the size, and then I start the timer:

void cForm::expandHorizontally(const QString & str)
{
    ui.Text->setText( str );
    connect( &oTimer, SIGNAL(timeout()), this, SLOT(increaseHSize()) );
    cTimer.stop();//if the label is collapsing at the moment, stop it
    disconnect( &cTimer, SIGNAL(timeout()) );
    oTimer.start();//start expanding
}

void cForm::compactHorizontally(const QString &)
{
    connect( &cTimer, SIGNAL(timeout()), this, SLOT(decreaseHSize()) );
    oTimer.stop();
    disconnect( &oTimer, SIGNAL(timeout()) );
    cTimer.start();
}

之后,标签开始改变大小:

After that, the label starts changing it's size:

void cForm::increaseHSize()
{
    if( ui.Text->width() < 120 )
    {
        //increase the size a bit if it hasn't reached the bound yet
        ui.Text->setFixedWidth( ui.Text->width() + 10 );
    }
    else
    {
        ui.Text->setFixedWidth( 120 );//Set the desired size
        oTimer.stop();//stop the timer
        disconnect( &oTimer, SIGNAL(timeout()) );//disconnect the timer's signal
    }
}

void cForm::decreaseHSize()
{
    if( ui.Text->width() > 0 )
    {
        ui.Text->setFixedWidth( ui.Text->width() - 10 );
    }
    else
    {
        ui.Text->setFixedWidth( 0 );
        cTimer.stop();
        disconnect( &cTimer, SIGNAL(timeout()) );
    }
}

问题:Everything工作顺利,标签慢慢打开和关闭。然而,如果它这样做了几次,它开始改变大小更快更快每次(就像定时器的间隔越来越小,但显然不是)。最后,在几个开口/关闭后,只要开始立即将其大小增加到边界,当我将鼠标悬停在按钮上,并立即折叠到零大小

The problem: Everything works smoothly at first, the label slowly opens and closes. However, if it does so a couple of times, it starts changing the size faster and faster each time (just as if the timer's interval was getting smaller and smaller, but it obviously isn't). Eventually after a couple of openings/closes it just begins to immediately increase it's size up to the bound when I hover the mouse over the button, and immediately collapse to the zero size when the mouse goes away from the button.

推荐答案

这是什么原因?

我建议事件正在等待处理,并且排队事件的数量随时间增加。也许是因为一个事件在两个计时器事件之间或由于程序的其他部分没有完全处理。

I would suggest that events are waiting to be processed, and the number of queued events increase with time. Maybe because an event is not completely processed between two timer events or due to the other parts of the program.

为什么不使用一个计时器?你可以更进一步,通过使用插槽大小更改事件。其他槽只是为了改变什么类型的变化:

Why don't you use only one timer? You can go even further, by using only slot for size change events. The other slots are just there for changing what type of change:

void cForm::connectStuff(){
    connect( &oTimer, SIGNAL(timeout()), this, SLOT(changeSize()) );
    connect(
          ui.Button,
          SIGNAL(mouseEntered(QString)),
          this,
          SLOT(expandHorizontally())
    );
    connect(
          ui.Button,
          SIGNAL(mouseLeft(QString)),
          this,
          SLOT(compactHorizontally())
    );
}

void cForm::expandHorizontally(){
      shouldExpand = true;
}

void cForm::compactHorizontally(){
      shouldExpand = false;
}

void cForm::changeSize(){
     if(shouldExpand)
        increaseHSize();//call the function which expand
     else
        decreaseHSize();//call the function which compact
}

这篇关于QTimer变得更快,每个开始/停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:24