updating qlabel image in switch statement
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?
Code:
int MainWindow::readADC(int range)
{
QPixmap stat_GO
(":/res/images/stat_GO.png");
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;
}
Re: updating qlabel image in switch statement
This method is OK. The problem is how this method is invoke. Refreshing the screen needs working event loop. So if You do this calling readADC 3 times You have such a symptom.
Re: updating qlabel image in switch statement
Also blocking the main thread and thus the GUI for up to a second (through waitForReadyRead) is usually also a very bad idea.
There really doesn't seem to be a need for this, all branches do that, so the switch could easily be done in a slot connected to readyRead().
The code suprisingly also doesn't even care how much data is reads.
Cheers,
_