Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multiple timers problem
Qt 6.11 is out! See what's new in the release blog

Multiple timers problem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 7.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jastmc
    wrote on last edited by
    #1

    I want to use two timers to update graphics on the screen. I have two timers. The first is connected to a slot to change the graphic every 150ms and repeat after 10 iterations. The second is to simulate the completion of internal tests every 2 seconds.

    There are a couple of problems which I cannot solve. They are in the Dialog module.

    1. My timer->stop(); command crashes the program with a "SIGSEGV" segmentation fault.
    2. My timer2 seems to count up in steps of two rather than one. It skips every second one of the graphics that I give it to display. With the debugger I can stop the code in each state of test but it does not display the graphic.

    Could I get some advice please?

    Regards,
    James

    Here is the Dialog module
    @#include <QtGui>
    #include <QDialog>
    #include <QLabel>
    #include <QDebug>
    #include <QTimer>

    #include "dialog.h"
    #include "ui_dialog.h"
    #include "probe.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    Dialog::setWindowFlags(Qt::SplashScreen); // no decoration on the screen
    ui->setupUi(this);

    // Set up the rolling logo timer
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));
    timer->start(150);
    
    // Set up the test results timer
    QTimer *timer2 = new QTimer(this);
    connect(timer2, SIGNAL(timeout()), this, SLOT(results_update()));
    timer2->start(2000);
    

    }

    void Dialog::state_update()
    {
    static int counter=0;

    switch (counter)
    {
    case 1:
       ui->label->setPixmap(QPixmap(":/logo/logo1.jpg",0,Qt::AutoColor));
        break;
    case 2:
       ui->label->setPixmap(QPixmap(":/logo/logo2.jpg",0,Qt::AutoColor));
        break;
    case 3:
       ui->label->setPixmap(QPixmap(":/logo/logo3.jpg",0,Qt::AutoColor));
        break;
    case 4:
       ui->label->setPixmap(QPixmap(":/logo/logo4.jpg",0,Qt::AutoColor));
        break;
    case 5:
       ui->label->setPixmap(QPixmap(":/logo/logo5.jpg",0,Qt::AutoColor));
        break;
    case 6:
       ui->label->setPixmap(QPixmap(":/logo/logo6.jpg",0,Qt::AutoColor));
        break;
    case 7:
       ui->label->setPixmap(QPixmap(":/logo/logo7.jpg",0,Qt::AutoColor));
        break;
    case 8:
       ui->label->setPixmap(QPixmap(":/logo/logo8.jpg",0,Qt::AutoColor));
        break;
    case 9:
       ui->label->setPixmap(QPixmap(":/logo/logo9.jpg",0,Qt::AutoColor));
        break;
    case 10:
       ui->label->setPixmap(QPixmap(":/logo/logo10.jpg",0,Qt::AutoColor));
        break;
    }
    
    if (++counter > 10) counter = 1;
    

    }

    void Dialog::results_update()
    {
    static int test = 0;
    QString str;

    switch (test)
    {
        case 0:
            ui->label_Clk_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor));
            break;
        case 1:
            ui->label_USB_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor));
            break;
        case 2:
            ui->label_Probe_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor));
            break;
        case 3:
            ui->label_HV_Result->setPixmap(QPixmap(":/logo/Fail.jpg",0,Qt::AutoColor));
            break;
        case 4:
            ui->label_LV_Result->setPixmap(QPixmap(":/logo/Pass.jpg",0,Qt::AutoColor));
            break;
        case 5:
            break;
        case 6:
            timer->stop();
    

    // timer2->stop();

            Probe *j = new Probe();
            j->showMaximized();
            this->close();
    
            break;
    
    }
    test++;
    str = QString("Test = %1").arg(test);
    ui->label_test->setText(str);
    

    }

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

    void Dialog::on_buttonBox_accepted()
    {
    qApp->quit();
    }

    @

    Here is the Main module
    @#include "dialog.h"
    #include "probe.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Dialog w;
    Dialog Probe;

    w.show();
    
    return a.exec&#40;&#41;;
    

    }
    @

    Here is the Probe module
    @#include "probe.h"
    #include "ui_probe.h"

    Probe::Probe(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Probe)
    {
    Probe::setWindowFlags(Qt::SplashScreen); // no decoration on the screen
    ui->setupUi(this);
    }

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

    void Probe::on_buttonBox_accepted()
    {
    qApp->quit();
    }
    @

    Here is Dialog.h
    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QtGui>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();

    private slots:
    void on_buttonBox_accepted();
    void state_update();
    void results_update();

    private:
    Ui::Dialog *ui;
    QDialogButtonBox *buttonBox;
    QTimer *timer;
    QTimer *timer2;

    signals:

    };

    #endif // DIALOG_H
    @

    Here is Probe.h
    @#ifndef PROBE_H
    #define PROBE_H

    #include <QDialog>

    namespace Ui {
    class Probe;
    }

    class Probe : public QDialog
    {
    Q_OBJECT

    public:
    explicit Probe(QWidget *parent = 0);
    ~Probe();

    private slots:
    void on_buttonBox_accepted();

    private:
    Ui::Probe *ui;
    };

    #endif // PROBE_H
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You shadowed both your Dialog timer variable in the constructor so it's no initialized.
      Try this:
      @
      timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));
      timer->start(150);

      timer2 = new QTimer(this);
      connect(timer2, SIGNAL(timeout()), this, SLOT(results_update()));
      timer2->start(2000);
      

      @

      I would recommend to change the class variables naming with something like _timer or m_timer. It helps separating local variables from class variable.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jastmc
        wrote on last edited by
        #3

        Hi,

        Sorry but being new to Qt, I don't understand what you mean by
        [quote]You shadowed both your Dialog timer variable in the constructor so it’s no initialized.
        [/quote]

        Can you explain or point me to something where I can learn about this?

        Regards,
        James

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jastmc
          wrote on last edited by
          #4

          Hi,

          Thanks for your suggestion, that helped with the crashing problem.

          Any ideas about the counting up in twos?

          Regards
          James

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            [quote author="jastmc" date="1366645117"]Hi,

            Sorry but being new to Qt, I don't understand what you mean by
            [quote]You shadowed both your Dialog timer variable in the constructor so it’s no initialized.
            [/quote]

            Can you explain or point me to something where I can learn about this?
            [/quote]

            http://en.wikipedia.org/wiki/Variable_shadowing

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved