当前位置:网站首页>Some instructions on whether to call destructor when QT window closes and application stops
Some instructions on whether to call destructor when QT window closes and application stops
2022-07-06 16:53:00 【Flower dog fdog】
I have been imitating these days QQ Make an instant messaging software , The process is not very smooth , When the window closes , Applications still exist , The application ended abnormally , Close subwindow , The main window closes , So I summarized some contents , Convenient for future access .
If you are interested in this project, you can go to GitHub:https://github.com/FdogMain/FdogInstantMessaging
main Function , Windows created on the stack , Call destructor automatically when closing , This situation cannot be used this->setAttribute(Qt::WA_DeleteOnClose);, An exception occurs when closing , If it is created on the heap , You can use this->setAttribute(Qt::WA_DeleteOnClose), When calling destructors , There will be no exceptions .
When the window is closed close function , This function sends a close event QCloseEvent, Then the window will be hidden , Ask if you want to close , Can intercept QCloseEvent event , Which is rewriting QCloseEvent, Users can choose to close , Or cancel .
In does not add Qt::WA_DeleteOnClose when , Choose to turn off , The window will disappear , Actually called hide, If you add it , In addition to the call hide, It also calls deleteLater Method to release the window , Without this attribute ,close and hide, also servisibel The function of is the same , It just hides the window object , It doesn't destroy objects .
stay main, Create a window on the stack A, close window A when , Will call the destructor .
If in this window A Create another window in the constructor of B, And in A In the destructor of B To release .
The first form :
MainWindow * b = new MainWindow();
When closing the window A, Then close the window B when , establish B The destructor of is called , window A The destructor of is called ( This closing method has obvious jamming , When off A, According to the rules ,B Should be turned off , Release , however B The window is also displayed on the desktop , Multiple runs , Find that there will still exist A The problem of non execution of deconstruction ( The printed statements in the destructor are not printed on the console ), So there are problems in this way )
In turn, , Close the window first B, Then close the window A,B The destructor of is called , window A The destructor of is called ( This closing method has no jamming , It's actually B The window is hidden , Failure to actively perform deconstruction , And in the A Passive execution in the destructor of , That's why it's closed B when , The display does not call B destructor , Shut down A when , To display the call B Cause of destructions )
We give the window B add to Qt::WA_DeleteOnClose Have a try ( Add... To the constructor setAttribute(Qt::WA_DeleteOnClose)), Because of the window B It's the window on the pile , have access to setAttribute(Qt::WA_DeleteOnClose)),
Or close the window first A, Then close the window B, Show that the window was called B Destructor of , Then there's an exception , This exception should be repeated destructions B Sent exception .
Put the window A About windows B Remove the released code , Show that the window was called B Destructor of , Call window A Destructor of , But there were no exceptions ( There's stuck , Multiple runs , Find that there will still exist A The problem of non execution of deconstruction ( The printed statements in the destructor are not printed on the console )).
Now the reverse is true , Close the window first B, Because of the window B Set up setAttribute(Qt::WA_DeleteOnClose)) attribute , The destructor is executed immediately , Then close the window A, If nothing happens , There should be an exception , Because of the window B Has been released , In the window A Release again B Abnormal transactions , hold A Release in destructor in B The code is commented again , function , The display calls the windows in turn B Destructor of , window A Destructor of ( No caton ).
The second form , Specify the parent window
MainWindow * b = new MainWindow(this);
A The window destructor has no write release B Window code case :
close A window ( Be released ),B The window closes ( Be released )( No caton ).
close B window ( Just hide ), close A window ( Be released )( No caton ).
Try again b add to setAttribute(Qt::WA_DeleteOnClose)) attribute , close B window , perform B destructor , To shut down A, perform A The analysis of ( No caton ).
close A window , perform A The analysis of , perform B The analysis of , also B The window is closed ( No caton ).
This is because QT The parent-child object mechanism of is at work , The reason lies in that this.
When we use the parent object to create an object , The parent object will add this object to its child object list . When the parent object is deleted , It will traverse its sub object class table and delete each sub object , Then the sub objects themselves delete their own sub objects , This calls recursively until all objects are deleted , So if new Out of the control , If a parent object is specified , We don't need to delete it manually .
Another discovery , If other controls are specified A Window as parent window , Will be embedded in A Window , however MainWindow Windows of this class will not be embedded
The opposite is not true .
But the above is only in the basic case , When I set the window property to borderless , After no taskbar and other different attributes , Close the window again , Destructors are not called automatically , In other words, the window is just closed , But the application itself has not been closed , The most obvious feature is when you close the window ,qt The output window of the application still shows red squares instead of green triangles .
At this time, you can add the following code where you want to close , The application will be closed .
QApplication* app;
app->quit();
There is also a situation where MainWindow Created in widget window , But one closes the last widget,MainWindow Will be shut down , Is it inconceivable , Before finding a solution , I can only judge whether it is the last widget, If it was me, I would hide , Instead of closing , for instance .
for example qq The main interface is MainWindow Double click friend generation widget window .
When closing these widget When the window comes to the last , The main window will close , One of the reasons for this symptom , Is to set the properties of the window
That is to use this function setWindowFlags(), In for a penny, in for a pound , Put this widget Add another attribute to the window setWindowFlag(Qt::CoverWindow);
This problem may not exist ~
Another problem is to close the window , The program may end abnormally , This mistake is also related to main The location of the window created in .
边栏推荐
- 图像处理一百题(1-10)
- ~77 linear gradient
- China tetrabutyl urea (TBU) market trend report, technical dynamic innovation and market forecast
- LeetCode 1558. Get the minimum number of function calls of the target array
- Restful style interface design
- LeetCode 1447. Simplest fraction
- 视频压缩编码和音频压缩编码基本原理
- Shell_ 05_ operator
- ~Introduction to form 80
- LeetCode 1584. Minimum cost of connecting all points
猜你喜欢
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
FLV格式详解
图像处理一百题(1-10)
Hbuilder x format shortcut key settings
Shell_ 00_ First meeting shell
Chapter 5 namenode and secondarynamenode
was unable to send heartbeat
姚班智班齐上阵,竞赛高手聚一堂,这是什么神仙编程大赛?
ByteDance open source Gan model compression framework, saving up to 97.8% of computing power - iccv 2021
LeetCode 1552. Magnetic force between two balls
随机推荐
~79 Movie card exercise
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
「博士毕业一年,我拿下 ACL Best Paper」
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
7-7 ring the stupid bell
LeetCode 1558. Get the minimum number of function calls of the target array
ByteDance new programmer's growth secret: those glittering treasures mentors
Simple records of business system migration from Oracle to opengauss database
~77 linear gradient
LeetCode 1640. Can I connect to form an array
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
Hbuilder x format shortcut key settings
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
LeetCode 1584. Minimum cost of connecting all points
One hundred questions of image processing (1-10)
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
~72 horizontal and vertical alignment of text
LeetCode 1545. Find the k-th bit in the nth binary string
How to configure hosts when setting up Eureka
SQL快速入门