Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Loop for QLabels?

[SOLVED]Loop for QLabels?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrNoway
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      messi
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        broadpeak
        wrote on last edited by
        #3

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

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

        ...
        }
        @

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MrNoway
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            messi
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              messi
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MrNoway
                wrote on last edited by
                #7

                [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 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved