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

創建和釋放 Qt 小部件對象

Creating and deallocating a Qt widget object(創建和釋放 Qt 小部件對象)
本文介紹了創建和釋放 Qt 小部件對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

聽說widget應該在堆上分配(使用new),然后就不需要刪除了(自動完成).

  1. 有人能解釋一下原因嗎?
  2. 如果小部件不是以這種方式分配而是在堆棧中,會發生什么情況?

我不確定這是否重要,但我創建的所有小部件都有一個父級.

這個說:

<塊引用>

如果 parent 為 0,則新小部件變為一個窗口.如果 parent 是另一個小部件,這個小部件成為一個子窗口內父.新的小部件是刪除其父項時刪除.

解決方案

沒有魔法.簡單地說,一個 QObject 會在它的析構函數中自動刪除它的孩子.所以,只要你的小部件有一個父級并且你銷毀了那個父級,你就不必擔心子級.因此,如果您想知道 QObject * parent 參數是什么,那么,這就是它的用途.

此外,來自文檔:

<塊引用><塊引用>

刪除所有子對象.如果這些對象中的任何一個在堆棧上或全局上,您的程序遲早會崩潰.

因此,避免將父級分配給堆棧分配的對象.

I heard that the widgets should be allocated on the heap (using new), and then there are no needs to delete them (it is done automatically).

  1. Can someone explain why?
  2. What happens if a widget is not allocated that way, but on a stack?

I am not sure if it matters, but all widgets I am creating have a parent.

This says :

If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent. The new widget is deleted when its parent is deleted.

解決方案

There's no magic involved. Simply put, a QObject automatically deletes its children in its destructor. So, as long as your widget has a parent and that you destroy that parent, you don't have to worry about the children. So if you wondered what was that QObject * parent parameter, well, that's what it's there for.

Also, from the doc:

All child objects are deleted. If any of these objects are on the stack or global, sooner or later your program will crash.

So, avoid giving parents to objects that are stack-allocated.

這篇關于創建和釋放 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 ()?環形?)
主站蜘蛛池模板: 欧美精品一区二区三区四区 在线 | 91大神在线资源观看无广告 | 精品无码久久久久久国产 | av手机在线免费观看 | 欧美日韩中文字幕在线 | 日韩欧美国产一区二区三区 | 国产91久久精品一区二区 | 亚洲成人在线网 | 成年人在线观看视频 | 亚洲免费av一区 | 欧美一区免费 | 久久久国产一区二区三区 | 国产一区日韩在线 | 日美女逼逼 | av手机在线免费观看 | 中文字幕欧美在线观看 | 婷婷国产一区 | 国产偷录视频叫床高潮对白 | 激情欧美日韩一区二区 | 免费黄色录像视频 | 岛国av在线免费观看 | 国产精品美女久久久久久免费 | 在线观看亚洲专区 | 久久精品欧美一区二区三区不卡 | 国产一二区免费视频 | 一区二区三区免费 | 久久免费香蕉视频 | 一区二区三区免费看 | 亚洲欧美一区二区三区在线 | 小早川怜子xxxxaⅴ在线 | 色综合久久久 | 免费骚视频 | 亚洲看片网站 | 亚洲欧洲一区二区 | 婷婷激情综合 | 久草热视频| 欧美精品一 | 国产精品无码久久久久 | 欧美激情精品久久久久 | 日日爽| 国产福利资源 |