Updating Qlabel image
Solved
General and Desktop
-
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; }
-
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 callQCoreApplication::processEvents()
to process all queued events (unless you want ensure that any user input is blocked until you leave your task loop).