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. Hi, I need help with dynamic controls.

Hi, I need help with dynamic controls.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 969 Views 2 Watching
  • 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.
  • Allan5A Offline
    Allan5A Offline
    Allan5
    wrote on last edited by
    #1

    I want to add 3 controls dynamically to a QWidget, I'm trying with QLabel but it does not work as expected, I really do not know how to do it, look for information but it did not help me at all.

    0_1544669854109_31ea1efe-7005-4d0e-8777-1ef93e5394c5-image.png

    I would like to add them as we see the two QLabel with the QChekcBox above.
    And go through them to see if any checkbox was clicked, I do not know if I should save them so QList <QCheckBox *> to know if it was clicked?

    Thank you very much for your answers.

    JKSHJ 1 Reply Last reply
    0
    • Allan5A Allan5

      I want to add 3 controls dynamically to a QWidget, I'm trying with QLabel but it does not work as expected, I really do not know how to do it, look for information but it did not help me at all.

      0_1544669854109_31ea1efe-7005-4d0e-8777-1ef93e5394c5-image.png

      I would like to add them as we see the two QLabel with the QChekcBox above.
      And go through them to see if any checkbox was clicked, I do not know if I should save them so QList <QCheckBox *> to know if it was clicked?

      Thank you very much for your answers.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      @Allan5 Your widget (widget_controles) already has a layout. That's why you cannot add new layout to it.

      Instead, add your new widget to the existing layout:

      auto existingLayout = ui->widget_controles->layout();
      existingLayout->addWidget(new QLabel("Campo"));
      

      @Allan5 said in Hi, I need help with dynamic controls.:

      And go through them to see if any checkbox was clicked, I do not know if I should save them so QList <QCheckBox *> to know if it was clicked?

      Call the isChecked() function to find out if it was clicked.

      bool c = ui->checkbox->isChecked();
      

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      Allan5A 1 Reply Last reply
      5
      • JKSHJ JKSH

        @Allan5 Your widget (widget_controles) already has a layout. That's why you cannot add new layout to it.

        Instead, add your new widget to the existing layout:

        auto existingLayout = ui->widget_controles->layout();
        existingLayout->addWidget(new QLabel("Campo"));
        

        @Allan5 said in Hi, I need help with dynamic controls.:

        And go through them to see if any checkbox was clicked, I do not know if I should save them so QList <QCheckBox *> to know if it was clicked?

        Call the isChecked() function to find out if it was clicked.

        bool c = ui->checkbox->isChecked();
        
        Allan5A Offline
        Allan5A Offline
        Allan5
        wrote on last edited by
        #3

        @JKSH

        Is there any other way to add controls dynamically?

        0_1544713865141_0591a592-1abf-41bd-a246-44a8c6a52715-image.png

        I also want to add a QCheckbox because I plan to do something similar to this, but I could not get it to add controls dynamically and go through each QCheckBox to see if it was clicked.

        Thanks for your response

        0_1544713960274_e45fc757-6c43-42ab-bfbb-ba45851ef501-image.png

        In Workbench I can import fields from a csv file, in a GroupBox it shows the columns that csv brings, I want to do that. But to do it I need to create controls dynamically.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          are you sure capa is not NULL ?
          seems to crash so i guess you did not apply a layout in Designer ?
          Also even the layout is inside a Widget, the reference from UI is
          kinda flat.
          like
          ui->verticalLayout->addWidget( new QCheckBox("test") );

          small sample
          https://www.dropbox.com/s/1t1hll6gt32usp9/DynamicCheckBoxes.zip?dl=0

          alt text

          Allan5A 1 Reply Last reply
          2
          • mrjjM mrjj

            Hi
            are you sure capa is not NULL ?
            seems to crash so i guess you did not apply a layout in Designer ?
            Also even the layout is inside a Widget, the reference from UI is
            kinda flat.
            like
            ui->verticalLayout->addWidget( new QCheckBox("test") );

            small sample
            https://www.dropbox.com/s/1t1hll6gt32usp9/DynamicCheckBoxes.zip?dl=0

            alt text

            Allan5A Offline
            Allan5A Offline
            Allan5
            wrote on last edited by
            #5

            @mrjj

            Thank you for your example project, now the question, to go through each new control that I add, can I do this?

            QList <QCheckBox *> listCheckBox;
            listCheckBox.append (newCheckBox);

            It will be that way I will be able to detect if some checkbox was clicked and it is true?

            Because when you press a button you have to go through the controls and look for the ones that were clicked.

            Thanks for your help

            1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              @Allan5 said in Hi, I need help with dynamic controls.:

              QList <QCheckBox *> listCheckBox;
              listCheckBox.append (newCheckBox);

              You can do this. Hope you are doing signal/slot connection for each checkbox as well.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              3
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                Alternatively to keeping your own list.
                QList<QCheckBox*> AllChecks = ui->groupBox->findChildren<QCheckBox*>();
                Just make sure that
                ui->groupBox matches your parent. That is - the widget that has the checkboxes.

                1 Reply Last reply
                1

                • Login

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