Updating Qlabel image



  • Hello,

    I have a small switch function running that I want to update the image of a label depending on the result. The issue I have is that all images are only updated at the end of the run. How can I update each image as each case is evaluated?

    int MainWindow::readADC(int range)
    {
        QPixmap stat_GO (":/res/images/stat_GO.png");
        QByteArray data;
        bool ok = false;
        float r = 0.0;
    
        switch (range)
        {
        case 24:
            serial->waitForReadyRead(1000);
            data = serial->readAll();
            qDebug() << data;
            r = data.toFloat(&ok);
            if (!ok) qDebug() << "24VDC Conversion Failed";
            qDebug() << r;
    
            if ((r > 23.2) && (r < 24.8))   //Check tolerance (3.3%)
                ui->_tstr24Vok->setPixmap(stat_GO);
            return 1;
            break;
    
        case 3:
            serial->waitForReadyRead(1000);
            data = serial->readAll();
            qDebug() << data;
            r = data.toFloat(&ok);
            if (!ok) qDebug() << "3.3VDC Conversion Failed";
            qDebug() << r;
    
            if ((r > 3.2) && (r < 3.4))   //Check tolerance (3%)
                 ui->_but3v3Vok->setPixmap(stat_GO);
            return 1;
            break;
    
        case 12:
            serial->waitForReadyRead(1000);
            data = serial->readAll();
            qDebug() << data;
            r = data.toFloat(&ok);
            if (!ok) qDebug() << "12VDC Conversion Failed";
            qDebug() << r;
            
            if ((r > 11.6) && (r < 12.4))   //Check tolerance (3.3%)
            ui->_but12Vok->setPixmap(stat_GO);
            return 1;
            break;
        }
        return 0;
    }
    


  • I don't know what you're doing after returning from that method but it seems that the event loop is still busy so the label is not updated immediately.
    Try calling ui->myLabel->repaint(); to enforce an update.



  • @micland
    After the return is validated it just continues on basically repeating similar tasks.
    adding ui->myLabel->repaint(); after each label change does what I'mm looking for. Thanks.



  • If you're running your tasks in an endless loop and can't move it to a background thread your UI will freeze so instead of calling repaint() it might be better to call QCoreApplication::processEvents() to process all queued events (unless you want ensure that any user input is blocked until you leave your task loop).


Log in to reply
 

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