Qtimer event Slot 1 minute before



  • How can I set a timer that run an event slot 1 minute before of signal event

    for example in mainwindow:

    @
    Qtimer *timer9 = new Qtimer(this);
    connect(timer9, SIGNAL(timeout()), this, SLOT(arrivopullman()));
    timer9->start(180000); // 3 minutes
    @

    @
    void MainWindow::arrivopullman(){
    /* if timer9:value = timer:value - 120000{
    ui->label2->show();
    }
    if timer9:value=180000
    ui->label2->close();
    ui->label5->show();
    }
    */
    }
    @

    I would not put 2-3 different timers, I would do it all with one, is possible?

    thanks for help


  • Moderators

    How should the new timer know when an event is signaled?

    In your case you probably mean you like to have a trigger after 2 min and after 3 min. there are different implementations possible. But you need to trigger first the 2 min event with a timer. In the slot of this event you can set for instance a single shot timer for starting the 3min event.



  • in this image is easier to understand what I wanna do:

    !http://img198.imageshack.us/img198/5408/immagineogj.jpg(Click here)!

    at 2 minute to do
    ui->label2->show();
    at 3 minute to do
    ui->label2->close();
    ui->label3->show();


  • Moderators

    I though that you were trying something like this.

    Personally I would write two slot methods. Use a singleshot timer to start the 2min slot and start then the first time a regular 3 min timer starting starting always this slot again. Every time the 2min slot is triggered it start a singleshot timer with 60 seconds. This singleshot timer shall trigger the 3min slot.



  • I'd even suggested having 2 slots and in slot#1 you start with single-shoting slot#2 in one sec, also in slot#2 you will make a single-shot for slot#1 that comes in 2 sec... Like so:

    @#include "mainwindow.hpp"
    #include "ui_mainwindow.h"

    #include <QTimer>

    const int delay1 = 2000, delay2 = 1000;
    quint64 Counter = 0; // a counter of passed time for this example

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    // lets say we have a label on form, and we show Counter value
    ui->lblShots->setText(QString("%1").arg(Counter));

    // sequence hasshin
    QTimer::singleShot(delay1, this, SLOT(slot1()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::slot1()
    {
    // a requesting the next shot
    QTimer::singleShot(delay2, this, SLOT(slot2()));

    // here we do whatever business we have
    Counter += delay1;
    ui->lblShots->setText(QString("%1").arg(Counter));
    }
    void MainWindow::slot2() // same here
    {
    QTimer::singleShot(delay1, this, SLOT(slot1()));

    Counter += delay2;
    ui->lblShots->setText(QString("%1").arg(Counter));
    }@



  • ok thanks all for your helps.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.