Question

Hy Qt master..

I wanna make my label (pixmap) on off on off on and soon, how can i do that??

i have try use this code :

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"));

that is not work? how can i solved that? Thanks All


this is the problem :

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;

}

when I=4, Qtimer run normally but when I=5 Qtimer still active.

Was it helpful?

Solution

first add a boolean member variable like bool blink;, Create a QTimer and connect it's timeout() signal to a slot function like below:

// 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;
}

edit: if you want to control your timer, you should declare it in the header of the class first

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

and when you want to create it:

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

for stopping it:

  timer->stop();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top