how to access the new object
-
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); }
-
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); }
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 ?
-
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 ?
@SGaist
I would like to do something like this in other function under the same class.- change the contents of the object. Such as ui->pLayout->label_p->setText("ABC");
- Delete/remove the object since it is use "new" for dynamic allocation. delete label_p;
-
@SGaist
I would like to do something like this in other function under the same class.- change the contents of the object. Such as ui->pLayout->label_p->setText("ABC");
- Delete/remove the object since it is use "new" for dynamic allocation. delete label_p;
@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 (withdelete 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 -
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..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(); }
-
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..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(); }
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)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) -
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..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(); }
@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 -
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)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)@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. -
@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.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:
and you should look at
QListWidget
, as mentioned by @mpergand .
You can probably replace some parts of your "design" with already existingQWidget
classes -
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); }
@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.
-
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:
and you should look at
QListWidget
, as mentioned by @mpergand .
You can probably replace some parts of your "design" with already existingQWidget
classes@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.
-
-
@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.
@ChrisW67
finally, I find that use for loop would be same.for(int i=0;i<25;i++) { ui->pLayout->setRowStretch(i,25); }