PDA

View Full Version : Updating QLabel Image



jvalerio
17th December 2014, 22:45
Hello,

I have a program that communicates with an Arduino board and have made a simple protocol. Now I'm trying to automate things and want some advice on how to accomplish my current task.
So Basically I have the below function which when called sends data over to the Aurdino telling it which pin to turn on. So the idea is to update the QLabel with a stat_sBy button. This is a yellow .png image and there are other states I will work with later. But I'm getting a error as I'm not sure what to use to Automate the process for replacing the QLabel Objecname in the loop.




void MainWindow::TestIOPCB()
{
QPixmap stat_OFF (":/res/images/stat_OFF.png");
QPixmap stat_noGO (":/res/images/stat_noGO.png");
QPixmap stat_GO (":/res/images/stat_GO.png");
QPixmap stat_sBy (":/res/images/stat_sBy.png");

QByteArray testOuts;
testOuts.resize(4);
testOuts[0] = 24v_ena;
testOuts[1] = spare_cmd;
testOuts[2] = shut_cmd;
testOuts[3] = mux_cmd;

QStringList testLabels;
testLabels << "Label25" << "Laberl45";

char db0 = 0x53;
char db1 = 0x50;
//Initiate I/O Tests

for (int x = 0; x < 4; x++)
{
setOutput(db0, 0x48, db1, testOuts[x]);
ui->testLabels[x]->setPixmap(stat_sBy);

//The below works fine, but I have too many ports and want to simplify a little.//
/*
setOutput(db0, 0x48, db1, shutter_cmd);
ui->label_27->setPixmap(stat_sBy);

setOutput(db0, 0x48, db1, mux_1064_cmd);
ui->label_28->setPixmap(stat_sBy);

setOutput(db0, 0x48, db1, mux_532_cmd);
ui->label_29->setPixmap(stat_sBy);

setOutput(db0, 0x48, db1, amp_atten_cmd);
ui->label_30->setPixmap(stat_sBy);
*/
}

}

anda_skoa
17th December 2014, 23:14
Is there is fixed mapping of value to pixmap?

E.g. "shutter_cmd" always means "stat_sBy"?

If so use a switch() or put the pixmaps into a map/hash with the value as the key.

Cheers,
_

jvalerio
18th December 2014, 01:10
No, they can have either of the other values: stat_OFF; stat_noGO; stat_GO; stat_sBy;

d_stranz
18th December 2014, 03:20
Your error is because you are looping over 4 things, but your "testLabels" list only contains 2 things. But even if it had 4 things in it, those are QString instances, not pointers to QLabels. The solution is to stuff your QLabel pointers into a list as you make them:



QList< QLabel * > testLabels;
testLabels << ui->label_27 << ui->label_28 << ui->label_29 << ui->label_30;

for (int x = 0; x < 4; x++)
{
setOutput(db0, 0x48, db1, testOuts[x]);
ui->testLabels[x]->setPixmap(stat_sBy);
}

jvalerio
18th December 2014, 15:02
Thanks, that did it. Just had to remove ui-> from line 7. of your post.
The below works perfect. I would like to know if anyone out there had any other ways of doing this.


QList< QLabel * > testLabels;
testLabels << ui->label_27 << ui->label_28 << ui->label_29 << ui->label_30;

for (int x = 0; x < 4; x++)
{
setOutput(db0, 0x48, db1, testOuts[x]);
testLabels[x]->setPixmap(stat_sBy);
}

d_stranz
18th December 2014, 16:06
Just had to remove ui-> from line 7. of your post.

Yes, sorry. Too much cut and paste after a long day at the keyboard. Glad it worked.