How can I get my MainWindow to update?



  • Hello, I'm new to Qt. In the past I have used C/C++ for about 30 years and have also developed windows apps using VB. Now, I'm tinkering with a Raspberry Pi and Qt.

    I developed an 8 channel ADC board and have written routines to read data from it using direct register access. Some of these routines are quite complex doing fourier transforms etc. before saving the data to a file. I now want to start using Qt and my first attempt is a very simple voltmeter. I put a lcdNumber on my MainWindow and used the following code to display a value from the adc

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "adcio.cpp"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    gpioGetHWRev();
    ui->lcdNumber->display((double) ((val3208(0,0)*3.3)/4096.0));

    }

    This works. The next step is to continuously read the adc and update the lcdNumber until I exit the program. I tried putting the 'ui->lcdNumer' line into a loop but it doesn't update the display until the loop ends.

    Any advice on how to accomplish this?

    Thanks, Steve.



  • Hi
    Maybe you could use a QTimer set to go off at a suitable interval and the update the UI

    HTH



  • @GrahamL Thanks for your reply. I did wonder about using QTimer. I found this example

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QTimer::singleShot(5, this, SLOT(timeout());
    }

    I assume I would have to create a slot such as this;

    void MainWindow::timeout()
    {
    ui->lcdNumber->display((double) ((val3208(0,0)*3.3)/4096.0));
    }

    Is this what you would suggest? otherwise, how would you accomplish it with QTimer?

    Steve.



  • Hi
    I would have a QTimer member in MainWindow and initialise it and start it for the desired interval. Then you need to supply a slot that is executed when the timer fires.
    So in MainWindow.h

    QTimer* m_timer;
    
    slots:
    void update();
    

    and in MainWindow.cpp

    m_timer = new QTimer(this);
    connect (m_timer,&QTimer::timeout,this,&MainWindow::update);
    m_timer->start(1000); // for a 1 second interval
    

    HTH



  • @GrahamL Thanks for the advice.

    I will try that later. Steve.


Log in to reply
 

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