Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Create arrays of widgets dynamically at runtime with the purpose of adding them to the window

Create arrays of widgets dynamically at runtime with the purpose of adding them to the window

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
widgetsarrayruntimedynamic gui
8 Posts 4 Posters 6.1k 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.
  • A Offline
    A Offline
    AthanD
    wrote on 10 Feb 2016, 14:00 last edited by
    #1

    Hello, I am just starting with Qt for a simple application. Although I got to know the very basics I run into a problem. At runtime the user will be presented with a spinbox and a button, depending on the number that the user has selected in the spinbox, when they click on the putton there are going to be spinboxes with labels on them. For example, the user sets teh spinbox to be equal to 10, then by clicking on the button they will get 10 spinboxes with each one's number on top of them.
    Here is something I tried but had messed up layout:

        int hiddenLayers = ui->layersNum->value()-2;
    
        QSpinBox* hiddenBoxes[hiddenLayers];
        QLabel* hiddenLabels[hiddenLayers];
        QVBoxLayout* hiddenLayouts[hiddenLayers];
    
        for(int i = 0; i< hiddenLayers; i++){
            hiddenLayouts[i] = new QVBoxLayout;
    
            hiddenLabels[i] = new QLabel;
            hiddenLayouts[i]->addWidget(hiddenLabels[i],1);
    
            hiddenLabels[i]->setText("layerNum");
            hiddenLabels[i]->show();
    
            hiddenBoxes[i] = new QSpinBox;
            hiddenLayouts[i]->addWidget(hiddenBoxes[i],0);
            hiddenBoxes[i]->show();
    
            ui->gridLayoutHidden->addLayout(hiddenLayouts[i],i%2,i,10,10,0);
    
        }
    

    Here is the result: Program Screenshot

    R 1 Reply Last reply 10 Feb 2016, 14:23
    0
    • A AthanD
      10 Feb 2016, 14:00

      Hello, I am just starting with Qt for a simple application. Although I got to know the very basics I run into a problem. At runtime the user will be presented with a spinbox and a button, depending on the number that the user has selected in the spinbox, when they click on the putton there are going to be spinboxes with labels on them. For example, the user sets teh spinbox to be equal to 10, then by clicking on the button they will get 10 spinboxes with each one's number on top of them.
      Here is something I tried but had messed up layout:

          int hiddenLayers = ui->layersNum->value()-2;
      
          QSpinBox* hiddenBoxes[hiddenLayers];
          QLabel* hiddenLabels[hiddenLayers];
          QVBoxLayout* hiddenLayouts[hiddenLayers];
      
          for(int i = 0; i< hiddenLayers; i++){
              hiddenLayouts[i] = new QVBoxLayout;
      
              hiddenLabels[i] = new QLabel;
              hiddenLayouts[i]->addWidget(hiddenLabels[i],1);
      
              hiddenLabels[i]->setText("layerNum");
              hiddenLabels[i]->show();
      
              hiddenBoxes[i] = new QSpinBox;
              hiddenLayouts[i]->addWidget(hiddenBoxes[i],0);
              hiddenBoxes[i]->show();
      
              ui->gridLayoutHidden->addLayout(hiddenLayouts[i],i%2,i,10,10,0);
      
          }
      

      Here is the result: Program Screenshot

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 10 Feb 2016, 14:23 last edited by
      #2

      @AthanD said:

      At runtime the user will be presented with a spinbox and a button, depending on the number that the user has selected in the spinbox, when they click on the putton there are going to be spinboxes with labels on them.

      But why do you subtract 2 from the entered value?

      The issue is pretty much caused by the line:

      ui->gridLayoutHidden->addLayout(hiddenLayouts[i],i%2,i,10,10,0);
      

      I think you mixed up some parameters.

      Why do you modulo the row value by 2?
      Why a row- and col-span value of 10?

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AthanD
        wrote on 10 Feb 2016, 14:27 last edited by
        #3

        About the first question this is just how it works in the program, it doesn't have to do with the layout. I just need the number the user gives minus 2.

        About the second question, if I would leave it like this : addLayout(hiddenLayouts[i]); , then it wouldn't work. I tried other values and I couldn't figure it out.

        R 1 Reply Last reply 10 Feb 2016, 14:36
        0
        • A AthanD
          10 Feb 2016, 14:27

          About the first question this is just how it works in the program, it doesn't have to do with the layout. I just need the number the user gives minus 2.

          About the second question, if I would leave it like this : addLayout(hiddenLayouts[i]); , then it wouldn't work. I tried other values and I couldn't figure it out.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 10 Feb 2016, 14:36 last edited by
          #4

          @AthanD said:

          About the first question this is just how it works in the program, it doesn't have to do with the layout. I just need the number the user gives minus 2.

          then you should make sure the user can't enter a value less than 3.

          Why do you use a gridlayout?

          Does the following do what you want?

          ui->gridLayoutHidden->addLayout(hiddenLayouts[i],0,i);
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • A Offline
            A Offline
            AthanD
            wrote on 10 Feb 2016, 14:46 last edited by
            #5

            I thought it would be easier to add in a grid layout. And yes work code works fine! I will just make it to a 3 in each row and then change row.
            Thank you!

            R 1 Reply Last reply 10 Feb 2016, 14:49
            0
            • A AthanD
              10 Feb 2016, 14:46

              I thought it would be easier to add in a grid layout. And yes work code works fine! I will just make it to a 3 in each row and then change row.
              Thank you!

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 10 Feb 2016, 14:49 last edited by
              #6

              @AthanD
              actually IMO i believe the grid layout is the most complicated one offered by Qt :)
              All the others are pretty much straight forward.

              But since you anyway want to display them in a grid it's your way to go ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Elg1
                wrote on 18 Mar 2021, 16:05 last edited by
                #7

                I would like to see how you handled the widgets data lamda or custom slot. Please provide what worked for you. I'm having problems with a similar task.

                JonBJ 1 Reply Last reply 18 Mar 2021, 16:11
                0
                • E Elg1
                  18 Mar 2021, 16:05

                  I would like to see how you handled the widgets data lamda or custom slot. Please provide what worked for you. I'm having problems with a similar task.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 18 Mar 2021, 16:11 last edited by JonB
                  #8

                  @Elg1
                  I don't think anyone/the OP is around any longer to answer from a thread from 5 years ago! Suggest you open your own new thread question.

                  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