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. how to access the new object
QtWS25 Last Chance

how to access the new object

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 575 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.
  • R Offline
    R Offline
    richard0117
    wrote on last edited by
    #1

    May anyone know how to access the objects after the object(qlabel or qcombobox) created.
    Now, I cannot delete/ modify those object after created.
    And then it will create another new object on top of the original layout.
    I would like to remove/modify those objects in other function or before the new objects created.
    I am a beginner for OO program. I am not sure where I should create another parameter to store the address of those object.
    ui->pLayout which is a QGridLayout created in Ui Form.

     void MainWindow::show_item()
    {
      ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);
        for(int i=0;i<selected_parameter_list.size();i++)
        {
        QLabel *label_p=new QLabel(this);
        QComboBox *c_box=new QComboBox(this);
        QLabel *label_comment=new QLabel(this);
        label_p->setText("Test");
        label_comment->setText("Comment");
        ui->pLayout->setColumnMinimumWidth(1,20);
        ui->pLayout->addWidget(label_p,i,1,1,1,Qt::AlignTop|Qt::AlignLeft);
        ui->pLayout->addWidget(c_box,i,2,1,1,Qt::AlignTop|Qt::AlignLeft);
        ui->pLayout->addWidget(label_comment,i,3,1,1,Qt::AlignTop|Qt::AlignLeft);
        ui->pLayout->setHorizontalSpacing(5);
        ui->pLayout->setVerticalSpacing(1);
        }
        ui->pLayout->setContentsMargins(10, 10, 10, 10);
        setLayout(ui->pLayout);
    }
    
    SGaistS C 2 Replies Last reply
    0
    • R richard0117

      May anyone know how to access the objects after the object(qlabel or qcombobox) created.
      Now, I cannot delete/ modify those object after created.
      And then it will create another new object on top of the original layout.
      I would like to remove/modify those objects in other function or before the new objects created.
      I am a beginner for OO program. I am not sure where I should create another parameter to store the address of those object.
      ui->pLayout which is a QGridLayout created in Ui Form.

       void MainWindow::show_item()
      {
        ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);
          for(int i=0;i<selected_parameter_list.size();i++)
          {
          QLabel *label_p=new QLabel(this);
          QComboBox *c_box=new QComboBox(this);
          QLabel *label_comment=new QLabel(this);
          label_p->setText("Test");
          label_comment->setText("Comment");
          ui->pLayout->setColumnMinimumWidth(1,20);
          ui->pLayout->addWidget(label_p,i,1,1,1,Qt::AlignTop|Qt::AlignLeft);
          ui->pLayout->addWidget(c_box,i,2,1,1,Qt::AlignTop|Qt::AlignLeft);
          ui->pLayout->addWidget(label_comment,i,3,1,1,Qt::AlignTop|Qt::AlignLeft);
          ui->pLayout->setHorizontalSpacing(5);
          ui->pLayout->setVerticalSpacing(1);
          }
          ui->pLayout->setContentsMargins(10, 10, 10, 10);
          setLayout(ui->pLayout);
      }
      
      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      There are several possible strategies depending on what you want to do with them.

      You can use a QVector to store the pointers or you can retrieve them from the layout.

      Thus: what do you want to do with them ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi and welcome to devnet,

        There are several possible strategies depending on what you want to do with them.

        You can use a QVector to store the pointers or you can retrieve them from the layout.

        Thus: what do you want to do with them ?

        R Offline
        R Offline
        richard0117
        wrote on last edited by
        #3

        @SGaist
        I would like to do something like this in other function under the same class.

        1. change the contents of the object. Such as ui->pLayout->label_p->setText("ABC");
        2. Delete/remove the object since it is use "new" for dynamic allocation. delete label_p;
        Pl45m4P 1 Reply Last reply
        0
        • R richard0117

          @SGaist
          I would like to do something like this in other function under the same class.

          1. change the contents of the object. Such as ui->pLayout->label_p->setText("ABC");
          2. Delete/remove the object since it is use "new" for dynamic allocation. delete label_p;
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @richard0117 said in how to access the new object:

          change the contents of the object. Such as ui->pLayout->label_p->setText("ABC");

          You can modify everything from ui-> using the pointer inside your class.

          Delete/remove the object since it is use "new" for dynamic allocation. delete label_p;

          There is no need to delete objects from your ui.
          They are cleaned up automatically when your class is destroyed (with delete ui; and the widget's destructor since they are all child object of your class)

          If you dont have your label in your ui file and you still want it to be accessible, you can declare it as member of your class.
          (or a vector of labels, where you store multiple labels, as @SGaist suggested)

          private:
                    QLabel *label_p;
          

          If your current widget is parent of label_p you still dont have to delete it. It's cleaned up following the object tree


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • R Offline
            R Offline
            richard0117
            wrote on last edited by
            #5

            I try to use QVector to store the pointers.
            However, looks like I cannot remove widget and result to the overlap.I would like to remove the previous widget and apply the new widget..

            9dc9fff8-6edc-4883-a90a-74d2271e2a1c-image.png

            In .h

            private:
                QVector<QLabel*>label_p1;
                QVector<QComboBox*>c_box1;
                QVector<QLabel*> label_comment1;
            

            In .cpp

            void MainWindow::show_item()
            {
            label_p1.clear();
            c_box1.clear();
            label_comment1.clear();
                ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);
                for(int i=0;i<selected_parameter_list.size();i++)
                {
                QLabel *label_p=new QLabel();
                    label_p1.append(label_p);
                QComboBox *c_box=new QComboBox();
                    c_box1.append(c_box);
                QLabel *label_comment=new QLabel();
                    label_comment1.append(label_comment);
                label_p->setText(selected_parameter_list[i]);
                label_comment->setText("Comment");
                ui->pLayout->setColumnMinimumWidth(1,20);
                ui->pLayout->addWidget(label_p,i,1,1,1,Qt::AlignTop|Qt::AlignLeft);
                ui->pLayout->addWidget(c_box,i,2,1,1,Qt::AlignTop|Qt::AlignLeft);
                ui->pLayout->addWidget(label_comment,i,3,1,1,Qt::AlignTop|Qt::AlignLeft);
                ui->pLayout->setHorizontalSpacing(5);
                ui->pLayout->setVerticalSpacing(1);
                }
                ui->pLayout->setContentsMargins(10, 10, 10, 10);
                setLayout(ui->pLayout);
            }
            
            void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
            {
                for(int i=0;i<selected_parameter_list.size();i++)
                {
                    ui->pLayout->removeWidget(label_p1[i]);
                    ui->pLayout->removeWidget(c_box1[i]);
                    ui->pLayout->removeWidget(label_comment1[i]);
                }
                update_select();
                show_item();
            }
            
            Pl45m4P M 2 Replies Last reply
            0
            • R richard0117

              I try to use QVector to store the pointers.
              However, looks like I cannot remove widget and result to the overlap.I would like to remove the previous widget and apply the new widget..

              9dc9fff8-6edc-4883-a90a-74d2271e2a1c-image.png

              In .h

              private:
                  QVector<QLabel*>label_p1;
                  QVector<QComboBox*>c_box1;
                  QVector<QLabel*> label_comment1;
              

              In .cpp

              void MainWindow::show_item()
              {
              label_p1.clear();
              c_box1.clear();
              label_comment1.clear();
                  ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);
                  for(int i=0;i<selected_parameter_list.size();i++)
                  {
                  QLabel *label_p=new QLabel();
                      label_p1.append(label_p);
                  QComboBox *c_box=new QComboBox();
                      c_box1.append(c_box);
                  QLabel *label_comment=new QLabel();
                      label_comment1.append(label_comment);
                  label_p->setText(selected_parameter_list[i]);
                  label_comment->setText("Comment");
                  ui->pLayout->setColumnMinimumWidth(1,20);
                  ui->pLayout->addWidget(label_p,i,1,1,1,Qt::AlignTop|Qt::AlignLeft);
                  ui->pLayout->addWidget(c_box,i,2,1,1,Qt::AlignTop|Qt::AlignLeft);
                  ui->pLayout->addWidget(label_comment,i,3,1,1,Qt::AlignTop|Qt::AlignLeft);
                  ui->pLayout->setHorizontalSpacing(5);
                  ui->pLayout->setVerticalSpacing(1);
                  }
                  ui->pLayout->setContentsMargins(10, 10, 10, 10);
                  setLayout(ui->pLayout);
              }
              
              void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
              {
                  for(int i=0;i<selected_parameter_list.size();i++)
                  {
                      ui->pLayout->removeWidget(label_p1[i]);
                      ui->pLayout->removeWidget(c_box1[i]);
                      ui->pLayout->removeWidget(label_comment1[i]);
                  }
                  update_select();
                  show_item();
              }
              
              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @richard0117

              What is your ultimate goal?
              I think where this is heading, it is already bad design.
              If you want to replace/exchange more than one or two widgets, make a custom widget which holds your layout with your labels, buttons and comboboxes and switch between your composed widgets. Or show and hide them.

              You could also use QStackedWidget and "flip" the pages (= widgets)

              • https://doc.qt.io/qt-6/qstackedwidget.html#details

              FYI:

              setLayout(ui->pLayout);

              This line is not needed. Since in your UI file you already set pLayout as layout of your current class. Even when changing something, the layout stays in place (as long as you dont remove it)


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              R 1 Reply Last reply
              1
              • R richard0117

                I try to use QVector to store the pointers.
                However, looks like I cannot remove widget and result to the overlap.I would like to remove the previous widget and apply the new widget..

                9dc9fff8-6edc-4883-a90a-74d2271e2a1c-image.png

                In .h

                private:
                    QVector<QLabel*>label_p1;
                    QVector<QComboBox*>c_box1;
                    QVector<QLabel*> label_comment1;
                

                In .cpp

                void MainWindow::show_item()
                {
                label_p1.clear();
                c_box1.clear();
                label_comment1.clear();
                    ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);
                    for(int i=0;i<selected_parameter_list.size();i++)
                    {
                    QLabel *label_p=new QLabel();
                        label_p1.append(label_p);
                    QComboBox *c_box=new QComboBox();
                        c_box1.append(c_box);
                    QLabel *label_comment=new QLabel();
                        label_comment1.append(label_comment);
                    label_p->setText(selected_parameter_list[i]);
                    label_comment->setText("Comment");
                    ui->pLayout->setColumnMinimumWidth(1,20);
                    ui->pLayout->addWidget(label_p,i,1,1,1,Qt::AlignTop|Qt::AlignLeft);
                    ui->pLayout->addWidget(c_box,i,2,1,1,Qt::AlignTop|Qt::AlignLeft);
                    ui->pLayout->addWidget(label_comment,i,3,1,1,Qt::AlignTop|Qt::AlignLeft);
                    ui->pLayout->setHorizontalSpacing(5);
                    ui->pLayout->setVerticalSpacing(1);
                    }
                    ui->pLayout->setContentsMargins(10, 10, 10, 10);
                    setLayout(ui->pLayout);
                }
                
                void MainWindow::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column)
                {
                    for(int i=0;i<selected_parameter_list.size();i++)
                    {
                        ui->pLayout->removeWidget(label_p1[i]);
                        ui->pLayout->removeWidget(c_box1[i]);
                        ui->pLayout->removeWidget(label_comment1[i]);
                    }
                    update_select();
                    show_item();
                }
                
                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @richard0117
                I think you should look at QListWidget.
                https://akshaybabloo.medium.com/creating-custom-widget-for-qlistwidget-in-qt-6-3ae03f051906
                https://github.com/akshaybabloo/qlistwidget-custom-widget

                1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @richard0117

                  What is your ultimate goal?
                  I think where this is heading, it is already bad design.
                  If you want to replace/exchange more than one or two widgets, make a custom widget which holds your layout with your labels, buttons and comboboxes and switch between your composed widgets. Or show and hide them.

                  You could also use QStackedWidget and "flip" the pages (= widgets)

                  • https://doc.qt.io/qt-6/qstackedwidget.html#details

                  FYI:

                  setLayout(ui->pLayout);

                  This line is not needed. Since in your UI file you already set pLayout as layout of your current class. Even when changing something, the layout stays in place (as long as you dont remove it)

                  R Offline
                  R Offline
                  richard0117
                  wrote on last edited by
                  #8

                  @Pl45m4
                  My goal is click the list in the treeWidget at the left hand side and right hand side to change the number of items.
                  use the show and hide and statically which is one of my optional.
                  Originally, I have already manually put 14 items in Form and use show and hide method one by one.
                  Now I try to use girdlayout to help to arrange it.
                  Since the layout is blank in the beginning..So I need to use "new" to create the object.Maybe I should create those object in the beginning instead of handle it after click the left hand side TreeWidget.

                  f097d841-607f-46be-8261-b203346ba570-image.png

                  Pl45m4P 1 Reply Last reply
                  0
                  • R richard0117

                    @Pl45m4
                    My goal is click the list in the treeWidget at the left hand side and right hand side to change the number of items.
                    use the show and hide and statically which is one of my optional.
                    Originally, I have already manually put 14 items in Form and use show and hide method one by one.
                    Now I try to use girdlayout to help to arrange it.
                    Since the layout is blank in the beginning..So I need to use "new" to create the object.Maybe I should create those object in the beginning instead of handle it after click the left hand side TreeWidget.

                    f097d841-607f-46be-8261-b203346ba570-image.png

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #9

                    @richard0117

                    What you are trying to do doesn't look right ...
                    I can hardly think of a use case, where you want to create dozens of labels and comboBoxes one by one, just to remove (or eventually add ) them later.
                    Think about re-designing your structure.
                    If two labels and one comboBox are one "unit" to display some of your "data", you can group them into one widget. Then you just have to show X instances of your widget, instead of creating every single label and comboBox again and again.

                    What should these labels and the comboBox do? Display/modify something? Some sort of editor widget?

                    Have a look here:

                    • https://doc.qt.io/qt-6/modelview.html

                    and you should look at QListWidget, as mentioned by @mpergand .
                    You can probably replace some parts of your "design" with already existing QWidget classes


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    R 1 Reply Last reply
                    0
                    • R richard0117

                      May anyone know how to access the objects after the object(qlabel or qcombobox) created.
                      Now, I cannot delete/ modify those object after created.
                      And then it will create another new object on top of the original layout.
                      I would like to remove/modify those objects in other function or before the new objects created.
                      I am a beginner for OO program. I am not sure where I should create another parameter to store the address of those object.
                      ui->pLayout which is a QGridLayout created in Ui Form.

                       void MainWindow::show_item()
                      {
                        ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);
                          for(int i=0;i<selected_parameter_list.size();i++)
                          {
                          QLabel *label_p=new QLabel(this);
                          QComboBox *c_box=new QComboBox(this);
                          QLabel *label_comment=new QLabel(this);
                          label_p->setText("Test");
                          label_comment->setText("Comment");
                          ui->pLayout->setColumnMinimumWidth(1,20);
                          ui->pLayout->addWidget(label_p,i,1,1,1,Qt::AlignTop|Qt::AlignLeft);
                          ui->pLayout->addWidget(c_box,i,2,1,1,Qt::AlignTop|Qt::AlignLeft);
                          ui->pLayout->addWidget(label_comment,i,3,1,1,Qt::AlignTop|Qt::AlignLeft);
                          ui->pLayout->setHorizontalSpacing(5);
                          ui->pLayout->setVerticalSpacing(1);
                          }
                          ui->pLayout->setContentsMargins(10, 10, 10, 10);
                          setLayout(ui->pLayout);
                      }
                      
                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by ChrisW67
                      #10

                      @richard0117 said in how to access the new object:

                      ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);

                      BTW: What is the intent of this line? Unless I am missing something obvious this will set the stretch factor of row 31 to 25.

                      R 1 Reply Last reply
                      1
                      • Pl45m4P Pl45m4

                        @richard0117

                        What you are trying to do doesn't look right ...
                        I can hardly think of a use case, where you want to create dozens of labels and comboBoxes one by one, just to remove (or eventually add ) them later.
                        Think about re-designing your structure.
                        If two labels and one comboBox are one "unit" to display some of your "data", you can group them into one widget. Then you just have to show X instances of your widget, instead of creating every single label and comboBox again and again.

                        What should these labels and the comboBox do? Display/modify something? Some sort of editor widget?

                        Have a look here:

                        • https://doc.qt.io/qt-6/modelview.html

                        and you should look at QListWidget, as mentioned by @mpergand .
                        You can probably replace some parts of your "design" with already existing QWidget classes

                        R Offline
                        R Offline
                        richard0117
                        wrote on last edited by richard0117
                        #11

                        @Pl45m4
                        you are correct.Previously design I create 14 label and comboBox one by one. And then I use hide and show for display.
                        Right now I would like to try to use gridlayout and use array to manage those items.

                        @ChrisW67 It help me to keep the first 25 row in the same size and at the top even there are only 3item. If there are only 3items added in the widget. even use align Top. the position will distribute evenly.

                        1 Reply Last reply
                        0
                        • R richard0117 has marked this topic as solved on
                        • C ChrisW67

                          @richard0117 said in how to access the new object:

                          ui->pLayout->setRowStretch(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24,25);

                          BTW: What is the intent of this line? Unless I am missing something obvious this will set the stretch factor of row 31 to 25.

                          R Offline
                          R Offline
                          richard0117
                          wrote on last edited by
                          #12

                          @ChrisW67
                          finally, I find that use for loop would be same.

                          for(int i=0;i<25;i++)
                          {
                                  ui->pLayout->setRowStretch(i,25);
                          }
                          
                          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