0

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.

Majapahit
  • 31
  • 5

1 Answers1

2

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();
sithereal
  • 1,656
  • 9
  • 16
  • @Majapahit if this answer solved your question (sure it does), then please, be kind, and accept the answer. – felixgaal Jun 05 '12 at 06:54
  • @FèlixGalindoAllué yes it solved but there is something miss i still confuse to stop that Qtimer. i have write on the top. – Majapahit Jun 05 '12 at 12:41
  • You must stop the timer probably when I==5. Follow the edited instructions from @sithereal. Add `timer->stop()` in the `I==5` branch. Anyway, I don't see the "big picture" here with that `I`... is it a counter? Do you increment it in the slot function? – felixgaal Jun 05 '12 at 18:10
  • @FèlixGalindoAllué no it isnot counter but i use serial to make I==5, iam sure that serial is doing normally. when I==5 the picture is change but the timer is still run. – Majapahit Jun 06 '12 at 01:34
  • I don't understand. Please explain better. Add a MWE based on your program that we can analyze. – felixgaal Jun 06 '12 at 05:01