0

我希望只有一个 QToolBar 实例并在我的应用程序执行期间对其进行多次修改。但是,我担心 Qt 完成的内存管理。

考虑以下:

QToolBar toolBar;
std::cout << toolBar.actions().size() << std::endl; // Prints 0
toolBar.addSeparator(); // will add an action
std::cout << toolBar.actions().size() << std::endl; // Prints 1
toolBar.clear();
std::cout << toolBar.actions().size() << std::endl; // Prints 0 again. Good!

最初,QToolBar 中的操作列表是空的。因此第一个 cout 打印“0”。通过“addSeparator”将一个内部操作添加到该列表中。所以第二个 cout 打印“1”。最后,“清除”,如预期的那样,删除所有操作,最后一个 cout 再次打印“0”。

现在,考虑一下“孩子列表”会发生什么:

QToolBar toolBar;
std::cout << toolBar.children().size() << std::endl; // Prints 3. Why?
toolBar.addSeparator(); // will add an action
std::cout << toolBar.children().size() << std::endl; // Prints 5. "addSeparator" has added two children.
toolBar.clear();
std::cout << toolBar.children().size() << std::endl; // Still prints 5. "Clear" did not remove any children!

最初,孩子列表的大小为 3。然后我调用“addSeparator”并将两个人添加到该列表中。好吧,我可以忍受。然而,在调用“清除”这些家伙之后,这些家伙并没有被删除。对于每个“addSeparator”或“addWidget”调用,都会添加两个孩子并且永远不会删除它们。

我将 Qt 5.4.1 用于 MSVC 2013,Windows。


编辑:添加peppe建议的代码。请阅读行注释。

QToolBar toolBar;
std::cout << toolBar.children().size() << std::endl; // Prints 3.
toolBar.addSeparator();
std::cout << toolBar.children().size() << std::endl; // Prints 5. "addSeparator" has added two children.

auto actions = toolBar.actions();

for (auto& a : actions) {
    delete a;
}

std::cout << toolBar.children().size() << std::endl; // Now this prints 4. Shouldn't be 3?
4

1 回答 1

2

只需看一下以下的实现addSeparator

QAction *QToolBar::addSeparator()
{
    QAction *action = new QAction(this);
    action->setSeparator(true);
    addAction(action);
    return action;
}

这会创建一个新的孩子QAction并将其添加到小部件的操作列表中。clear清除动作列表,但不破坏动作!因此,它们仍将作为工具栏的子项存在。

Qt 不知道您没有在其他地方使用这些操作——它们旨在用于多个小部件。如果要回收该内存,请删除addSeparator.

于 2016-02-04T15:33:03.970 回答