Qt-从布局中删除所有小部件? [英] Qt - remove all widgets from layout?

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

问题描述

这似乎并不容易.基本上,我通过函数将QPushButton添加到布局中,并且在函数执行时,我想先清除布局(删除所有QPushButton和其中的所有其他内容),因为只是将更多按钮附加到了scrollview.

This doesn't seem easy. Basically, I add QPushButtons through a function to a layout, and when the function executes, I want to clear the layout first (removing all QPushButtons and whatever else is in there), because more buttons just get appended to the scrollview.

标题

QVBoxLayout* _layout;

cpp

void MainWindow::removeButtonsThenAddMore(const QString &item) {

//remove buttons/widgets

QVBoxLayout* _layout = new QVBoxLayout(this);

QPushButton button = new QPushButton(item);
_layout->addWidget(button);

QPushButton button = new QPushButton("button");
_layout->addWidget(button);

QWidget* widget = new QWidget();
widget->setLayout(_layout);

QScrollArea* scroll = new QScrollArea();
scroll->setWidget(widget);
scroll->show();

}

推荐答案

我遇到了同样的问题:我有一个游戏应用程序,其主窗口类继承了QMainWindow.它的构造函数部分看起来像这样:

I had the same problem: I have a game app whose main window class inherits QMainWindow. Its constructor looks partly like this:

m_scene = new QGraphicsScene;
m_scene->setBackgroundBrush( Qt::black );
...
m_view = new QGraphicsView( m_scene );
...
setCentralWidget( m_view );

当我想显示游戏级别时,我实例化一个QGridLayout,在其中添加QLabel,然后将其像素图设置为某些图片(具有透明部分的像素图).第一级显示正常,但是当切换到第二级时,仍然可以在新的像素后面看到第一级的像素图(该像素图是透明的).

When I want to display a level of the game, I instantiate a QGridLayout, into which I add QLabels, and then set their pixmaps to certain pictures (pixmaps with transparent parts). The first level displays fine, but when switching to the second level, the pixmaps from the first level could still be seen behind the new ones (where the pixmap was transparent).

我尝试了几件事来删除旧的小部件. (a)我尝试删除QGridLayout并实例化一个新的QGridLayout,但随后了解到删除布局不会删除添加到其中的小部件. (b)我尝试在新的像素图上调用QLabel :: clear(),但那当然只会对新的像素图产生影响,而对僵尸图像没有影响. (c)我什至尝试删除我的m_view和m_scene,并在每次显示新关卡时重新构建它们,但仍然没有运气.

I tried several things to delete the old widgets. (a) I tried deleting the QGridLayout and instantiating a new one, but then learned that deleting a layout does not delete the widgets added to it. (b) I tried calling QLabel::clear() on the new pixmaps, but that of course had only an effect on the new ones, not the zombie ones. (c) I even tried deleting my m_view and m_scene, and reconstructing them every time I displayed a new level, but still no luck.

然后(d)我尝试了上面给出的一种解决方案,即

Then (d) I tried one of the solutions given above, namely

QLayoutItem *wItem;
while (wItem = widget->layout()->takeAt(0) != 0)
    delete wItem;

但这也不起作用.

但是,进一步谷歌搜索,我找到了一个有效的答案. (d)中缺少的是对delete item->widget()的调用.现在,以下内容适用于我:

However, googling further, I found an answer that worked. What was missing from (d) was a call to delete item->widget(). The following now works for me:

// THIS IS THE SOLUTION!
// Delete all existing widgets, if any.
if ( m_view->layout() != NULL )
{
    QLayoutItem* item;
    while ( ( item = m_view->layout()->takeAt( 0 ) ) != NULL )
    {
        delete item->widget();
        delete item;
    }
    delete m_view->layout();
}

然后我实例化一个新的QGridLayout,与第一个级别一样,向其添加新级别的小部件,等等.

and then I instantiate a new QGridLayout as with the first level, add the new level's widgets to it, etc.

Qt在很多方面都很棒,但是我确实认为这个问题表明这里的事情可能会容易一些.

Qt is great in many ways, but I do think this problems shows that things could be a bit easier here.

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

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