[SOLVED]Loop for QLabels?
-
wrote on 4 Dec 2012, 14:54 last edited by
hi,
i need to create a custom number of Labelse.g.
@ for (int i=0; i<10; i++){
QLabel *label_i;
label_i = new QLabel;
}@I know, this is not working, but just to show what I mean.
Is this somehow possible to do it?
i guess it is done using something like this
@QLabel *label_("%1"), i;@
-
wrote on 4 Dec 2012, 15:14 last edited by
Ui, a question popped up in my mind.
How good is your knowledge about C/C++?If it would be good you wouldn't ask such question.
BTW. I don't know your problem, but an array of QLabels could solve your problem.
-
wrote on 4 Dec 2012, 15:19 last edited by
Do you think something like this?:
@for (int i = 0; i < 10; i++)
{QLabel* label = new QLabel(QString("label%1").arg(QString::number(i)));
...
}
@ -
wrote on 4 Dec 2012, 15:22 last edited by
[quote author="messi" date="1354634051"]Ui, a question popped up in my mind.
How good is your knowledge about C/C++?
[/quote]i passed the first two semester -_-
alright, I solved it.
I didnt know, you can use an array for everything
e.g.
@QLabel *label[10];@I thought this simplicity, just shouldnt work
thx anyway
and for renaming i used this, what was actually just copied
@QLabel *label[10];@
@for (int i=0; i< 10; i++){
label[i] = new QLabel;
label[i]->setText(QString("%1").arg(i));
label[i]->show(); //Test purpose
}@ -
wrote on 4 Dec 2012, 15:23 last edited by
Your idea is typical for someone who programs in general with script languages, like perl,phyton etc.
I think there you can do this approach but not in C++.Put the QLabel array as an member in your class so that you can access it later if you need it.
And do not forget to initialize the array.
@for(int i=0; i<5; ++i)
mLabelArray[i] = new QLabel;@ -
wrote on 4 Dec 2012, 15:31 last edited by
You can also use QString::number() instead of QString("%1").arg(i)
-
wrote on 4 Dec 2012, 15:39 last edited by
[quote author="messi" date="1354635099"]You can also use QString::number() instead of QString("%1").arg(i)[/quote]
okay thanks too,
I am gonna remember this
1/7