Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [SOLVED]Loop for QLabels?

    General and Desktop
    3
    7
    4608
    Loading More Posts
    • 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
      MrNoway last edited by

      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 Reply Quote 0
      • M
        messi 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.

        1 Reply Last reply Reply Quote 0
        • B
          broadpeak 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)));
          

          ...
          }
          @

          1 Reply Last reply Reply Quote 0
          • M
            MrNoway 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
            }@

            1 Reply Last reply Reply Quote 0
            • M
              messi 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;@

              1 Reply Last reply Reply Quote 0
              • M
                messi last edited by

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

                1 Reply Last reply Reply Quote 0
                • M
                  MrNoway 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 Reply Last reply Reply Quote 0
                  • First post
                    Last post