关于在Qt 4中删除,删除小部件和布局 [英] About deleting, removing widgets and layouts in Qt 4

查看:164
本文介绍了关于在Qt 4中删除,删除小部件和布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我使用的是Qt 4.7,Windows 7、64位).

(I use Qt 4.7, Windows 7, 64bit).

我创建了一个自定义表格.每行都是带有小部件的水平布局. 这些行和子项都保存在QList中,以便于访问.这些行也被添加到父窗口小部件内.

I created a custom table. Each row is a horizontal layout with widgets. The rows are kept in a QList for easy access, and the children too. The rows are also added inside the parent widget.

如果我调整父窗口小部件的大小,则会计算新的大小,删除所有内容,然后重新创建.

If I resize the parent widget, I calculate the new sizes, delete everything, and recreate it again.

我的问题是我不想删除任何小部件.只有当我清理桌子时,我才做.

My problem is that I don't want to delete any widget. Only when I clear the table, I do it.

由于我在QList内和父级布局中都有这些小部件,所以如何删除每行中的所有小部件,删除所有布局,然后将其添加到新的布局中?

Since I have the widgets inside a QList and inside the parent layouts, How can I remove all widgets in each row, delete all layouts, and then add those to new layouts?

如果我这样做:每个布局内的每个元素的takeAt(0)我都有一个内部带有小部件的QLayoutItem ...如何在不删除小部件的情况下删除layoutItem?....如何在不删除小部件的情况下删除小部件?杀死它,不管它是在父母还是孩子中?因为有多种删除方法:布局中的removeItem,removeWidget ...,但没有takeWidget ...只是takeAt(),它提供了一个Qlayoutitem.

If I do: takeAt(0) for every element inside each layout I have a QLayoutItem with a widget inside... How can I delete the layoutItem without deleting the widget?.... How do I remove the widget without killing it, no matter if it's in the parent or the child? Because there are many methods for deleting: removeItem, removeWidget... in a layout, but not takeWidget... just takeAt() and it gives a Qlayoutitem.

我尝试了几种方法,但是无论它们发生了什么,我仍然可以看到这些小部件.

I tried several ways, but I still see the widgets no matter what happened to them.

有关此问题:

  • 何时删除窗口小部件?如果我从布局中获取Widget(index),它是否会自行删除一些时间?如果我在另一个列表中有指向它的指针,会发生这种情况吗?

  • When does a widget get deleted? If I takeWidget(index) from a layout, is it deleted some time by itself? does it happen if I have a pointer to it in another list?

removeAt(index)是否执行小部件的删除方法?

removeAt(index) does execute the delete method of a widget?

推荐答案

好.我知道了 让我解释一下如何删除,保留小部件.

Ok. I got it working. Let me explain how this Removing, keeping widgets works.

窗口小部件通过其父布局而闻名.然后通过布局将其删除.通过这样做:

A widget is known by its parent layout. And you remove it through the layout. By doing:

layout()->removeAt(widget);
delete widget;

如果在QLayout(或其子级)中使用takeAt(index),它将为您提供QLayoutItem.要访问内部的小部件,只需使用widget().但是,如果不删除它,就无法删除它.因此,这种方法无效.

If you use takeAt(index) in a QLayout (or its children), it gives you a QLayoutItem. To access the widget inside, just use widget(). But there's no way to remove the widget without deleting it. So this approach is non valid.

在文档中,它介绍了一种删除元素的方法:

In the Docs it tells a way to delete the elements:

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

在Qt中需要特别注意的是以下几点: 如果您具有布局层次结构树,并且在布局内部添加了addLayout(),则无论您插入的窗口小部件深度如何,您都可以将其从子布局或任何父布局中删除布局(如果该布局中的树路径和该项目是从子布局中构建的.)

A special thing to note in Qt is the following: If you have a hierarchy tree of layouts, added with addLayout() inside layouts, no matter how deep your widget is inserted, you can remove it from the child layouts or any of the parent layouts, if the tree path from the layout and this item is built from child layouts.

最简单的方法是在自定义表中保留所有项目的指针列表.清除表格以重建表格时,只需在您的小部件内执行此操作即可:

The easiest thing is to keep a list of pointers to all the items, in a custom table. When clearing the table to reconstruct it, just do this inside your widget:

  CustomTableItem* item;
  while ( !items_.isEmpty() && ( (item = items_.takeFirst()) != 0 ) ){
    layout()->removeWidget(item);
    delete item; // It works no matter where the item is
  }

  items_.clear(); // clear the list afterwards.

它运行完美,也可以自己更新布局. 如果要保留元素,只需跳过删除项目"即可;并在以后使用它们.

And it works perfectly, updates the layout too by itself. If you want to keep the elements, just skip the "delete item;" and use them afterwards.

需要注意的重要一点是,不同的删除"功能在QList或类似的小部件以及QLayout中的工作方式不同(据我对Qt Docs的理解).

An important thing to note is that different "remove" functions work differently (as i understand on Qt Docs) in QList or similar widgets, and in a QLayout.

在QList中,removeAt实际上会删除对象.

In the QList, removeAt actually removes the object.

(Qt 4.7 QList文档)删除索引位置i上的项目.我必须是列表中的有效索引位置(即0 <== i

(Qt 4.7 QList Docs)"Removes the item at index position i. i must be a valid index position in the list (i.e., 0 <= i < size())."

在QLayout中,removeWidget或removeItem不会删除项目/小部件,您有责任像以前一样删除它.

In a QLayout, removeWidget or removeItem don't remove the item/widget, you have the responsability to delete it, as I did before.

(Qt 4.7 QLayout Docs)从布局中删除小部件小部件.在此调用之后,它是 呼叫者的责任是为小部件提供合理的几何形状或 将小部件放回布局中."

(Qt 4.7 QLayout Docs) "Removes the widget 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."

希望它会有所帮助.如果您发现任何错误,可以告诉我,我将编辑答案!

Hope it helps. If you see any error, you could tell me and I will edit the answer!

有关在此处删除的更多信息: 其他stackoverflow帖子

More on deleting here: Other stackoverflow post

这篇关于关于在Qt 4中删除,删除小部件和布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆