2017-02-02 159 views
0

When I use delete layout_newInfo; layout_main->update() happends this.如何从运行时的layout_main中删除layout_newInfo(按下按钮)?Qt从其他布局删除布局

I need get this back after pressing button (next image)

Delete orange section (layout_newInfo) by pressing button Add Component

代码我想:

QLayout *layout = this->layout(); 

    QLayoutItem *item; 
    while ((item = layout->takeAt(0)) != 0) 
     layout->removeItem (item); 
    delete layout_newInfo; 
    layout_main->update(); 
+0

显示您尝试过的代码。 – eyllanesc

+0

向Qestien添加了代码。 –

+0

请注意,在while循环中调用'layout-> removeItem(item);'什么也不做,因为'item'传递已经被调用['QLayout :: takeAt'](http:// doc.qt.io/qt-5/qlayout.html#takeAt)。 –

回答

0

我怎样才能在运行时从layout_main删除layout_newInfo给出layout_newInfo嵌套到layout_main?

语义上更清晰的方法:

layout_main->removeItem(layout_newInfo); // make sure layout_newInfo object deleted 
             // after either by parent or somehow else 

顺便说一句,通常这也应该做同样的去除嵌套布局:

delete layout_newInfo; // also removes it from upper layout 
layout_main->update(); // triggers update on the screen 

所以,仅仅2的代码示例的底线应该如果没有其他更新触发,只需要拨打layout_main->update()即可。

here的示例显示删除QLayoutItem(作为QLayout的父项)也将其从上层布局结构(它的析构函数)中删除。

+0

这部分'layout_main = new QVBoxLayout; layout_main-> addLayout(layout_header); layout_main-> addLayout(layout_data); layout_main-> addLayout(layout_buttons); setLayout(layout_main);'在构造函数中。 当按钮被点击出现这部分 'layout_main-> addLayout(layout_newInfo);' –

+0

如果'layout_newInfo'直接嵌套到'layout_main'只是'删除layout_newInfo'和'layout_main - > update()'作为好。 – AlexanderVX

+0

,但随后出现一些神器。布局不会让它停留在其他布局上。 –

2

你想达到什么目的? 如果要显示/隐藏现在位于layout_newInfo中的小部件,则 不使用布局。使用放置在layout_main(垂直布局)中的小部件,该小部件本身具有newInfo项目和布局,然后根据需要在小部件上使用setVisible(true/false)。

0

最后找到答案最好的办法是让像void showNewInfo(QString action);

无效的方法在类的cpp文件

void MainWind::showNewInfo(QString action) 
{ 
    if(action == "true") 
    { 
     bt_search->setEnabled(false); 
     bt_production->setEnabled(false); 
     bt_drying->setEnabled(false); 
     bt_storage->setEnabled(false); 
     ln_spent->show(); 
     cb_thickness1->show(); 
     cb_thickness2->show(); 
     cb_thickness3->show(); 
     cb_EFL1->show(); 
     cb_EFL2->show(); 
     bt_newItem->show(); 
    } 
    else if(action == "false") 
    { 
     bt_search->setEnabled(true); 
     bt_production->setEnabled(true); 
     bt_drying->setEnabled(true); 
     bt_storage->setEnabled(true); 
     ln_spent->hide(); 
     cb_thickness1->hide(); 
     cb_thickness2->hide(); 
     cb_thickness3->hide(); 
     cb_EFL1->hide(); 
     cb_EFL2->hide(); 
     bt_newItem->hide(); 
    } 
} 

也有可能使用的setText(“”),那么下一次展示片段,它将会清楚;