简体   繁体   English

从布局中删除QLayoutItem,但以后仍然使用吗?

[英]Remove QLayoutItem from a Layout, but still use it later on?

Here is the environment first: I have a self defined "Property Editor" which is a QGroupBox (derives from QWidget) Currently I have a class let's call it "Holder", which has a reference to two of the Property Editors. 首先是环境:我有一个自定义的“属性编辑器”,它是QGroupBox(来自QWidget的派生类)。目前,我有一个类称为“ Holder”,它引用了两个属性编辑器。

Now I have multiple "Holder" classes and one vertical QVBoxLayout (called Sidebar). 现在,我有多个“ Holder”类和一个垂直QVBoxLayout(称为“边栏”)。 In this layout I want both of the Property Editors of the currently selected Holder class to be displayed. 在此布局中,我希望显示当前选定的Holder类的两个属性编辑器。

And there is the issue: When the user selects another holder class, I want the Property Editors of the previously selected Holder class to disappear, and add the Property Editors of the new selected Holder class. 问题是:当用户选择另一个Holder类时,我希望先前选择的Holder类的属性编辑器消失,并添加新选择的Holder类的属性编辑器。

Selecting another Holder class works once. 选择另一个Holder类一次即可。 But when I select the first Holder class again, The editors don't seem to change. 但是当我再次选择第一个Holder类时,编辑器似乎没有变化。 Why? 为什么? Does "takeAt(..)" destroy the reference in the holder class? “ takeAt(..)”会销毁holder类中的引用吗? How can I get the desired behavior? 如何获得所需的行为?

Here is the code, thanks in advance: 这是代码,在此先感谢:

void App::setSelection(Holder * holder){
    if(m_currentlySelected == holder) return;

    m_viewer->sideBarRemoveAt(0);
    m_viewer->sideBarInsertAt(0, holder->firstPropEditor);
    m_viewer->sideBarRemoveAt(1);
    m_viewer->sideBarInsertAt(1, holder->secondPropEditor);

    m_currentlySelected = holder;
}

void QtViewer::sideBarRemoveAt(int i){
    m_sideBar->m_layout->takeAt(i);
}

void QtViewer::sideBarInsertAt(int i, QWidget * widget){
    m_sideBar->m_layout->insertWidget(i, widget);
}

QLayout::takeAt() doesn't remove the widget of the QLayoutItem from its parent widget. QLayout::takeAt()不会从其父窗口小部件中删除QLayoutItem窗口小部件。 The only reason it may seem to work the first time is probably because the other widgets were above (z-index wise) the first ones. 它似乎第一次起作用的唯一原因可能是因为其他小部件在第一个小部件上方(在z-index方向上)。

Rather than playing with the layout, you could 而不是使用布局,您可以

  • just hide/show your 2 PropertyEditor whenever the holder changes, hidden items don't generate holes in the layout, the next visible item is displayed as if the hidden items were not part of the layout, or 只需在持有人更改时隐藏/显示2 PropertyEditor ,隐藏项不会在布局中产生孔,显示下一个可见项,就好像隐藏项不在布局中,或者
  • use a QStackedWidget to stack all the PropertyEditor at the same place and select which one is displayed (with QStackedWidget::setCurrentIndex() ). 使用QStackedWidget将所有PropertyEditor堆叠在同一位置,然后选择显示哪个(使用QStackedWidget::setCurrentIndex() )。

Does "takeAt(..)" destroy the reference in the holder class? “ takeAt(..)”会销毁holder类中的引用吗?

No, this method removes the QLayoutItem from the layout. 不,此方法从布局中删除QLayoutItem。 See reference page for takeAt . 请参见参考页上的takeAt This class doesn't release the layout item (it is your responsibility to do). 此类不会释放布局项目(这是您的责任)。

But when I select the first Holder class again, The editors don't seem to change. 但是当我再次选择第一个Holder类时,编辑器似乎没有变化。 Why? 为什么?

I am not quite clear what you are trying to achieve (not enough code in your example), but if you are trying to change the layout using QLayoutItem's, then it is the simplest to create new layout and add items you want to display to it. 我不清楚您要实现的目标(示例中的代码不足),但是如果您尝试使用QLayoutItem更改布局,那么创建新的布局并添加要显示的项目是最简单的。 Or simply, remove all items from the layout, and add the items that should be visible. 或者简单地,从布局中删除所有项目,然后添加应该可见的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM