这似乎并不容易.基本上,我通过一个函数将 QPushButton
s 添加到布局中,当函数执行时,我想先清除布局(删除所有 QPushButton
s 和其他任何内容那里),因为更多的按钮会附加到 scrollview
.
This doesn't seem easy. Basically, I add QPushButton
s through a function to a layout, and when the function executes, I want to clear the layout first (removing all QPushButton
s 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,在其中添加QLabels,然后将它们的像素图设置为某些图片(具有透明部分的像素图).第一层显示正常,但是当切换到第二层时,仍然可以看到第一层的像素图在新的后面(像素图是透明的).
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 并实例化一个新的,但后来了解到删除布局不会删除添加到其中的小部件.(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 - 从布局中删除所有小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!