QLayout 動態布局(如何清空布局)

來源:互聯網
上載者:User

例子就是qtdemo裡面的動態布局的例子,詳細代碼就不具體列出了,這裡主要講下如何清空布局,以及遇到的問題及其解決辦法。

核心代碼:

 void Dialog::rotateWidgets()
 {
     Q_ASSERT(rotableWidgets.count() % 2 == 0);

     foreach (QWidget *widget, rotableWidgets)
         rotableLayout->removeWidget(widget);

     rotableWidgets.enqueue(rotableWidgets.dequeue());

     const int n = rotableWidgets.count();
     for (int i = 0; i < n / 2; ++i) {
         rotableLayout->addWidget(rotableWidgets[n - i - 1], 0, i);
         rotableLayout->addWidget(rotableWidgets[i], 1, i);
     }
 }

    通過隊列來儲存widget的列表,從而刪除、添加到layout,原理很簡單,但是很多人會遇到這樣的問題,布局內的widget刪除後並沒有消失,這是怎麼回事呢?

    API文檔裡提供了一種安全的刪除layout中控制項的方法:

    QLayoutItem *child;
    while ((child = layout->takeAt(0)) != 0) {
     ...
     delete child;
    }

    刪除是執行了,但是widget仍然沒有消失。。。

    最後還是找到辦法了,http://www.qtcn.org/bbs/simple/?t15751.html

xtowx 2009-02-24 13:43
我用delete後,程式老是報錯,後來我在網上查到用Widget.setParent(None),然後再layout.removeWidget(Widget),就可以了。呵呵

    原文作者可能是疏忽,None應該是NULL,即widget->setParent(NULL);編譯運行,OK!非常高興!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.