关闭pixmap的qt延迟

时间:2012-06-05 04:14:27

标签: qt pixmap

Hy Qt master ..

我想打开关闭的标签(pixmap)很快,我怎么能这样做?

我尝试使用此代码:

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仍然有效时。

1 个答案:

答案 0 :(得分: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();