Home > Net >  Why does my QT C application not update a pushbuttons' text inside the event routine when dir
Why does my QT C application not update a pushbuttons' text inside the event routine when dir

Time:04-06

I'm very new to Qt Creator and have a question regarding the reason that an update to a pushbuttons text is not occurring at the time I am expecting it to. Below is a snippet of the code showing the pushbutton event. The pushbutton launches another external process (AVRDUDE), which in turn reads the contents of the EEPROM of an Arduino board connected through USB. Data is then processed and displayed in the UI. As the process of reading the EEPROM takes a few seconds to perform, I would like to change the UI pushbutton that calls this routine from "READ FROM EEPROM" to "READING !!" during the execution of this routine. When routine is completed, then return the pushbutton label to "READ FROM EEPROM". Here is the code

void MainWindow::on_READ_FROM_EEPROM_clicked() // Read EEPROM into file "fromEEPROM.bin", then output values to UI
{
    ui->READ_FROM_EEPROM->setText("READING !!");
    call_AVRDUDE_read();
    int mBufferLength = 1024;                    // AVR 1K EEPROM space in 328p
    char mBuffer[mBufferLength];
    float fB[mBufferLength/4];
    QString filename = "fromEEPROM.bin";
    QFile mFile(filename);
    ui->fileCurrentlyDisplayed->setText(filename); // make this filename visable in UI
    if (mFile.exists())
    {
        if (mFile.open(QFile::ReadOnly))
            while (!mFile.atEnd())
                mFile.read(mBuffer,sizeof(mBuffer));
     mFile.close();
     memcpy(&fB, &mBuffer, mBufferLength);  // Next, take mBuffer array of chars and copy them into a float array format
     moveFloatArrayToGUI(fB);       // Fill in the tables in the GUI from this array of floats. Pass fB ARRAY to function
    }                               // that fills in UI
    ui->READ_FROM_EEPROM->setText("READ FROM EEPROM");
}

Everything in the routine performs exactly as expected with the exception of the very first line

ui->READ_FROM_EEPROM->setText("READING !!)"

and the last line

ui->READ_FROM_EEPROM->setText("READ FROM EEPROM");

What I see in the pushbutton text is just the READ FROM EEPROM label before, throughout, and after execution of the routine. If I change the text string of the last line, this new text will appear in the UI pushbutton. If I COMMENT OUT that last line, then the "READING !!" will appear in the UI pushbutton, BUT ONLY AFTER THE ROUTINE IS COMPLETED.

What I was expecting here was the "READING !!" msg to be displayed immediately upon entry, then the time consuming AVRDUDE call would take place, then the pushbutton text would be restored to READ FROM EEPROM. I tried executing ui->READ_FROM_EEPROM->setText("READING !!)" from a separate pushbutton, and it updates the read from EEPROM pushbutton immediately, so I am bewildered as why that line does not execute right upon entry. What am I missing and/or what am I doing wrong? Regards, Mark

CodePudding user response:

The UI is updated only when the code gets to the Event Loop, which won't happen until your application returns. You can force events processing by calling QCoreApplication::processEvents(); after your UI updates, but the recommended way would be to push that blocking code to another thread and use signals/slots to update your UI. See https://doc.qt.io/archives/qq/qq27-responsive-guis.html#manualeventprocessing for reference. The article is old but still holds.

  • Related