問題描述
這似乎并不容易.基本上,我通過一個函數將 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 - 從布局中刪除所有小部件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!