Qt篇——横向/竖向布局动态添加和移除控件

QLineEdit *aaa = new QLineEdit();
QLineEdit *bbb = new QLineEdit();

ui->horizontalLayout_3->insertWidget(ui->horizontalLayout_3->count(), aaa);
ui->horizontalLayout_3->insertWidget(ui->horizontalLayout_3->count(), bbb);
QWidget* qRemove = ui->horizontalLayout_3->itemAt(ui->horizontalLayout_3->count() - 1)->widget();
qRemove->setParent(NULL);    //注意,这里要做,否则移除后会导致父布局中子控件的显示异常,如高度异常、宽度异常
ui->horizontalLayout_3->removeWidget(qRemove);

效果:

        

                       最开始效果,                                                          添加2个输入框后

              只有一个子控件输入框

       

               移除一个输入框后                                     如果要移除的子控件不执行setParent(NULL),

                                                                                 而直接remove的话会导致以下情况产生,

                                                                                 其他子控件出现显示异常的情况

猜你喜欢

转载自blog.csdn.net/u011391361/article/details/133940390