0

嗨Qt大师..

我想让我的标签(像素图)关闭关闭关闭很快,我该​​怎么做?

我尝试使用此代码:

Sleeper::sleep(2);
    ui->label->setPixmap(QPixmap("C:/Users/EVAN/Pictures/New folder/85.png"));
    Sleeper::sleep(2);
    ui->label->setPixmap(QPixmap("C:/Users/EVAN/Pictures/New folder/87.png"));
    Sleeper::sleep(2);
    ui->label->setPixmap(QPixmap("C:/Users/EVAN/Pictures/New folder/85.png"));
    Sleeper::sleep(2);
    ui->label->setPixmap(QPixmap("C:/Users/EVAN/Pictures/New folder/87.png"));

那不是工作吗?我该如何解决?谢谢大家


这就是问题 :

if(I==4)
        {
            QTimer *timer1 = new QTimer(this);
            connect(timer1, SIGNAL(timeout()), this, SLOT(OnTimer()));
            timer1->start(1000);
            blink=true;
            port->write(send);
        }
else if(I==5)
        {
            ui->label->setPixmap(QPixmap("../../picture/green.png"));
            port->write(send);
        }

..................................................

void traffic1::OnTimer()
{

        ui->label->setPixmap(QPixmap(blink ? "../../picture/dark.png" : "../../picture/yellow.png"));
        blink = !blink;

}

当 I=4 时,Qtimer 正常运行,但当 I=5 时,Qtimer 仍然有效。

4

1 回答 1

2

首先添加一个布尔成员变量bool blink;,创建一个 QTimer 并将它的timeout()信号连接到一个槽函数,如下所示:

// constructor:
YourClass::YourClass()
{
       QTimer *timer = new QTimer(this);
       connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
       timer->start(1000);
       blink = false;
}

...........

void YourClass::OnTimer()
{
  ui->label->setPixmap(QPixmap(blink ? "C:/Users/EVAN/Pictures/New folder/85.png" : "C:/Users/EVAN/Pictures/New folder/87.png"));
  blink = !blink;
}

编辑:如果你想控制你的计时器,你应该首先在类的标题中声明它

class YourClass
{
  QTimer *timer;
   ...
};

当你想创建它时:

YourClass::YourClass()
{
       timer = new QTimer(this);
       connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
       timer->start(1000);
       blink = false;
}

停止它:

  timer->stop();
于 2012-06-05T04:18:50.620 回答