[SOLVED]Loop for QLabels?



  • hi,
    i need to create a custom number of Labels

    e.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;@



  • 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.



  • Do you think something like this?:
    @for (int i = 0; i < 10; i++)
    {

        QLabel* label = new QLabel(QString("label%1").arg(QString::number(i)));
    

    ...
    }
    @



  • [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
    }@



  • 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;@



  • You can also use QString::number() instead of QString("%1").arg(i)



  • [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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.