QTimer and QLCDNumber not working correctly together.



  • Hi,
    I'm trying to simulate the pulses of a machine we're building and I've decided to do it with a QTimer. I've read the documentation and I've seen how it works, however, I can't seem to get it to display in the QLCDNumber I want it to work with.
    I've tried with signals and slots, but in the end the QLCDOutpot is an "A".
    Here's the piece of my code where I attempt to make the connection, and then further where the methods are developed:

    //Part of the .cpp 
        timer = new QTimer(this);
    
        ui->lcdLitres->setStyleSheet("background-color: rgb(162, 195, 228)");
        ui->lcdPrice->setStyleSheet("background-color: rgb(162, 195, 228)");
        ui->btnStart->setStyleSheet("background-color: rgb(161, 249, 7)");
        ui->btnCancel->setStyleSheet("background-color: rgb(245, 1, 3)");
        ui->lcdLitres->setDigitCount(10);
        ui->lcdPrice->setDigitCount(10);
    
        connect(ui->btnStart, SIGNAL(clicked()),
                timer, SLOT(start()));
        connect(timer, SIGNAL(timeout()),
                this, SLOT(elapseLiters()));
    
    //Still .cpp
    void QDispenseDisplay::on_btnStart_clicked()
    {
        ui->btnCancel->setText("Detener");
        bCancelLoop = false;
    
        QSqlQuery query;
        query.exec("SELECT id, fPrecioUnit, fLitros, fImporte FROM Servicios WHERE id = last_insert_rowid()");
    
        query.next();
        QSqlRecord record = query.record();
        fUnitPrice = record.value(1).toFloat();
        fLitres = record.value(2).toFloat();
        fTotal = record.value(3).toFloat();
    
    //    qDebug() << "Last ID: " << sLastId;
        qDebug() << "fPrecioUnit: " << fUnitPrice;
        qDebug() << "fLitros: " << fLitres;
        qDebug() << "fImporte: " << fTotal;
    //    qDebug() << query.lastError();
    
        timer->setInterval(1000);
    
        QString sLitros = QString::number(fElapsedLitres, 'f', 2);
        QString sPrecio = QString::number(fUnitPrice * fElapsedLitres, 'f', 2);
    
        ui->lcdLitres->display(sLitros);
        ui->lcdPrice->display(sPrecio);
    
        if(fElapsedLitres == fLitres)
        {
            timer->stop();
        }
    }
    float QDispenseDisplay::getFElapsedLitres() const
    {
        return fElapsedLitres;
    }
    
    void QDispenseDisplay::setFElapsedLitres(float value)
    {
        fElapsedLitres = value;
    }
    
    
    void QDispenseDisplay::elapseLiters()
    {
        setFElapsedLitres(fElapsedLitres + 1);
    }
    
    
    //header file
    
    #ifndef QDISPENSEDISPLAY_H
    #define QDISPENSEDISPLAY_H
    
    #include <QWidget>
    #include <QTimer>
    #include <QLCDNumber>
    #include <QtSql>
    #include <QFileInfo>
    #include <QElapsedTimer>
    
    namespace Ui {
    class QDispenseDisplay;
    }
    
    class QDispenseDisplay : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit QDispenseDisplay(QWidget *parent = 0);
        ~QDispenseDisplay();
    
    
    
    public slots:
        void cancel(bool bCancel)
        {
            if ( bCancel != this->bCancel )
            {
                this->bCancel = bCancel;
                emit cancelling(this->bCancel);
                this->bCancel = false;
            }
        }
    
        void delay(int millisecondsToWait);
    
        void elapseLiters();
        float getFElapsedLitres() const;
        void setFElapsedLitres(float value);
    
    signals:
        cancelling(bool bCancel);
    
    private slots:
        void on_btnCancel_clicked();
    
        void on_btnNext_clicked();
    
        void on_btnStart_clicked();
    
    private:
        Ui::QDispenseDisplay *ui;
    
        bool bCancel;
    
        bool bCancelLoop;
    
        QTime *time;
    
        QTimer *timer;
    
        QElapsedTimer *eTimer;
    
        QSqlDatabase sqlLiquascanDB;
    
        float fUnitPrice;
        float fLitres;
        float fTotal;
    
        float fElapsedLitres;
    };
    
    #endif // QDISPENSEDISPLAY_H
    

    Summarizing (is that a word?), I want the QLCDNumber to display a simple tick every second and stop if it reaches the amount of fLitres.
    Thanks in advance.


  • Lifetime Qt Champion

    Hi,

    Unless I'm mistaken your QLCDNumber widget is in hexadecimal mode, change for Dec and you should be good to go.



  • @SGaist Thanks for the reply.
    I've checked in the designer and it is set to "Dec". It still displays an "A". I´ve updated the signals and slots, but still nothing.

    void QDispenseDisplay::elapseLiters()
    {
        setFElapsedLitres(fElapsedLitres + 1);
        QString sLitros = QString::number(fElapsedLitres, 'f', 2);
        QString sPrecio = QString::number(fUnitPrice * fElapsedLitres, 'f', 2);
    
        ui->lcdLitres->display(sLitros);
        ui->lcdPrice->display(sPrecio);
    
        if(fElapsedLitres == fLitres)
        {
            timer->stop();
        }
    }
    

  • Lifetime Qt Champion

    When does it start to show that A ?


Log in to reply
 

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