简体   繁体   English

Qt布局/窗口小部件交互 - 布局中的布局

[英]Qt Layout / Widget interactions - Layouts within Layouts

I have an interface in Qt 4.7 that I'm having a little difficulty getting to behave as I'd like. 我在Qt 4.7中有一个界面,我有点难以按照我的意愿行事。

Basic Description of Desired Behaviour : user makes a selection in a combo box, which causes a query to go to another function which returns a QHBoxLayout object that generally includes several labels, line edits, etc. This will adjust the contents of a layout on the interface to remove the previous selection, and add the new details. 所需行为的基本描述 :用户在组合框中进行选择,这会导致查询转到另一个返回QHBoxLayout对象的函数,该对象通常包括多个标签,行编辑等。这将调整布局的内容。界面删除以前的选择,并添加新的详细信息。

What's Actually Happening : The new layout is appearing, but the old one remains also, so they draw on top of each other resulting in a mess. 究竟发生了什么 :新的布局正在出现,但旧布局仍然存在,因此它们相互叠加导致混乱。

This is the code I have for the slot that the currentIndexChanged signal is linked to: 这是我对currentIndexChanged信号链接到的插槽的代码:

void updateAxisLabels(const QString & value)
{
  if ( m_current != "" )
  {
    QHBoxLayout* xOld = m_transforms[m_current]->xAxis();
    m_uiForm.layoutXAxis->removeItem(m_transforms[m_current]->xAxis());
    delete m_transforms[m_current]->xAxis();
    m_transforms[m_current]->init();
  }

  m_uiForm.layoutXAxis->addLayout(m_transforms[value]->xAxis());
  m_current = value;

  m_uiForm.layoutXAxis->update();
}

Here m_transforms is a map linking the contents of the combo box to an object with the xAxis() function (returning a QHBoxLayout*), and an init() function which will re-create the layout when it's been deleted. 这里m_transforms是一个映射,它将组合框的内容链接到一个具有xAxis()函数的对象(返回一个QHBoxLayout *),以及一个init()函数,该函数将在删除时重新创建该布局。 m_current is just a QString that I'm using to tell me which one I need to remove. m_current只是一个QString,我用来告诉我需要删除哪一个。

Reason I'm deleting it: because this is what half an hour of googling led me to believe was the right thing to do. 原因我正在删除它:因为这是半小时的谷歌搜索让我相信是正确的事情。

Any help much appreciated. 任何帮助非常感谢。 :) :)

I've done this kind of thing before, and you definitely have not only remove the widget from the layout but also delete it. 我以前做过这种事情,你绝对不仅要从布局中删除小部件,还要删除它。

Normally when I do this I create a layout especially for hosting my widget. 通常当我这样做时,我创建一个布局,特别是托管我的小部件。 So that I can just delete everything from inside the layout, and not have to worry about anything else that might be in there. 这样我就可以从布局中删除所有内容,而不必担心可能存在的任何其他内容。

Here is the recommended way to loop through all the items in a layout and remove them. 以下是循环浏览布局中所有项目并将其删除的推荐方法。

QLayoutItem *child;
 while ((child = m_uiForm.layoutXAxis->takeAt(0)) != 0) 
 {
     delete child;
 }

So then you can call any cleanup you want to do before it gets deleted. 因此,您可以在删除之前调用要执行的任何清理。

You can also call layout->removeWidget(your widget) or layout->removeItem(layout) to remove one specific thing from a layout. 您还可以调用layout-> removeWidget(您的小部件)或layout-> removeItem(布局)来从布局中删除一个特定的东西。 But you have to be careful with this. 但你必须小心这一点。 The doc says: 医生说:

Removes the widget from the layout. 从布局中删除小部件。 After this call, it is the caller's responsibility to give the widget a reasonable geometry or to put the widget back into a layout. 在此调用之后,调用者有责任为窗口小部件提供合理的几何图形或将窗口小部件放回布局中。

So you want to make sure you also delete the widget afterwards to make sure that it doesn't show anywhere. 因此,您希望确保之后删除小部件,以确保它不会显示在任何地方。

However, the other problem with this approach is if the widget/layout you are inserting/removing is somewhere in the middle of a bunch of other widgets in your layout, then you will have a lot of fun trying to replace it in the correct location. 但是,这种方法的另一个问题是,如果您插入/移除的小部件/布局位于布局中的一堆其他小部件的中间位置,那么尝试在正确的位置替换它将会有很多乐趣。

Which is why I usually try to create a layout specifically for this purpose with nothing else in it. 这就是为什么我通常会尝试专门为此目的创建一个布局而没有别的东西。

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

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