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

創(chuàng)建和釋放 Qt 小部件對象

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

問題描述

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

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

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

這個說:

<塊引用>

如果 parent 為 0,則新小部件變?yōu)橐粋€窗口.如果 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.

這篇關于創(chuàng)建和釋放 Qt 小部件對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 精品成人69xx.xyz | 免费久久网站 | 日韩免费看视频 | 男女视频在线观看免费 | 中文字幕精品一区二区三区精品 | 97超碰免费| 日韩精品在线看 | 97av在线| 99视频久 | 日日操视频 | 爱综合| 影音先锋中文字幕在线观看 | 日本三级电影在线观看视频 | 国产成人精品一区二三区在线观看 | 成人福利在线 | 一道本在线 | 日韩av在线不卡 | 日本三级全黄三级a | 国产美女在线免费观看 | 国产97在线视频 | 久久精品国产免费 | 日日夜夜精品视频 | 日韩中文字幕网 | 久产久精国产品 | 丝袜久久 | 国产免费一级片 | 久久午夜视频 | 国产精品久久欧美久久一区 | 免费大黄视频 | 亚洲一区二区网站 | 国产精品国产精品国产专区不片 | 欧美精品99 | 91资源在线 | 成人欧美一区二区三区白人 | 久久久久成人精品免费播放动漫 | 精品一级| 国产男人的天堂 | 成人在线免费 | 久久精品网 | 成人久久久久久久久 | 亚洲人人|