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

查看:95
本文介绍了关于在 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?

如果我这样做:对于每个布局中的每个元素,我有一个 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 Docs)删除索引位置 i 处的项目.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帖子

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

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