PDA

View Full Version : Manipulating member name to be controlled by string



jwflammer
28th February 2011, 00:01
I have a 10 by 10 grid of frames, i can change their backgrounds with
ui->box_9_9->setStyleSheet("background-color: rgb(255,0,0)"); but that's a lot of code, id like to have a For Loop



int count;
QString boxnum;
for (count = 0; count <= 10; count++)
{
boxnum += "box_";
boxnum += QString::number(count);
boxnum += "_";
boxnum += QString::number(count);
ui->boxnum->setStyleSheet("background-color: rgb(255,0,0)");
} but i get an error. What am i doing wrong?

wysota
28th February 2011, 01:35
This is C++, a static-typed compilable language. An identifier has to be an identifier and not a string containing textual representation of an identifier. You can probably do this instead:

QFrame *frm = findChild<QFrame*>(QString("box_%1_%1").arg(count));
if(frm)
frm->setStyleSheet(...);

jwflammer
28th February 2011, 02:03
that worked for a diagonal line, how do i get it to go row by row or column by column?

ChrisW67
28th February 2011, 02:19
Wysota's solution does what your pseudo-code does. Think about what Wysota gave you and adapt.

Two hints: more than one loop, and find a good introductory programming text.