久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Qt - 從布局中刪除所有小部件?

Qt - remove all widgets from layout?(Qt - 從布局中刪除所有小部件?)
本文介紹了Qt - 從布局中刪除所有小部件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這似乎并不容易.基本上,我通過一個函數將 QPushButton s 添加到布局中,當函數執行時,我想先清除布局(刪除所有 QPushButton s 和其他任何內容那里),因為更多的按鈕會附加到 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,在其中添加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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 亚洲精品一区二区在线 | 久久久久久久久久久久久9999 | 欧美大片一区二区 | 黄色日批视频 | 亚洲天堂一区二区 | 一区二区三区av | 欧美精品一区在线发布 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 国产福利一区二区 | 一区二区三区在线电影 | 日韩精品在线观看一区二区三区 | 在线a视频网站 | 国产原创在线观看 | 亚洲精品乱码久久久久久按摩 | 国产精品波多野结衣 | 在线免费观看a级片 | 高清亚洲 | 精品九九| 久久久www | 超碰精品在线 | 免费精品在线视频 | 久久久久久免费精品一区二区三区 | 亚洲视频三区 | 午夜影院视频在线观看 | 中文字幕久久精品 | 日韩免费福利视频 | 久久99蜜桃综合影院免费观看 | 欧美激情视频一区二区三区在线播放 | 91色视频在线观看 | 91在线看视频 | 亚洲精品电影网在线观看 | 久草在线在线精品观看 | 激情自拍偷拍 | 麻豆一区 | 黄色国产| 国产精品69毛片高清亚洲 | 久久精品一级 | 日日干干| 黄网在线观看 | a黄视频 | 久久精品99国产精品日本 |