Namespaces
Variants
Actions
(Difference between revisions)

How to delete a QObject derived class from within one of its own methods

Jump to: navigation, search
m (Text replace - "Category:MeeGo" to "Category:MeeGo Harmattan")
m (Hamishwillee - Bot update - Add ArticleMetaData)
Line 1: Line 1:
[[Category:Qt]][[Category:How To]][[Category:Code Examples]]
+
[[Category:Qt]][[Category:How To]][[Category:Code Snippet]]
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.2 -->
|id=
+
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|platform= All
 
|platform= All
|devices=
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|category= Qt
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= Qt, deleteLater
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20-Jun-2010
 +
|author= [[User:Gnuton]]
 +
<!-- The following are not in current metadata -->
 
|subcategory=  
 
|subcategory=  
|creationdate=20-Jun-2010
 
|keywords= Qt, deleteLater
 
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|sdk=<!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
 
|author=[[User:Gnuton]]
 
 
}}
 
}}
  
Line 24: Line 31:
 
Since this is a public slot, it can be connected to a signal or it can be just called in any method of the class.
 
Since this is a public slot, it can be connected to a signal or it can be just called in any method of the class.
  
There are no problem if a signal or a method calls more than one time [http://doc.qt.nokia.com/4.6/qobject.html#deleteLater QObject::deleteLater()]
+
There are no problem if a signal or a method calls more than one time [http://doc.qt.nokia.com/4.6/qobject.html#deleteLater QObject::deleteLater()]
  
Beginner developers sometimes use  
+
Beginner developers sometimes use  
 
<code cpp>
 
<code cpp>
 
delete this;
 
delete this;
 
</code>
 
</code>
and this is dangerous since the Qt event loop could invoke a method of the deleted class. Hence, the application or library will crash.
+
and this is dangerous since the Qt event loop could invoke a method of the deleted class. Hence, the application or library will crash.
 
* Related Qt Documentation link
 
* Related Qt Documentation link
  
 
Qt 4.6 [http://doc.qt.nokia.com/4.6/qobject.html#deleteLater deleteLater()]
 
Qt 4.6 [http://doc.qt.nokia.com/4.6/qobject.html#deleteLater deleteLater()]
 
[[Category:Qt]][[Category:MeeGo Harmattan]] [[Category:Symbian]]
 
[[Category:Qt]][[Category:MeeGo Harmattan]] [[Category:Symbian]]

Revision as of 15:36, 23 July 2012

Article Metadata

Compatibility
Platform(s): All

Article
Keywords: Qt, deleteLater
Created: gnuton (20 Jun 2010)
Last edited: hamishwillee (23 Jul 2012)

In order to delete a QObject-derived class from one of its own methods, developers have to use

deleteLater();

Since this is a public slot, it can be connected to a signal or it can be just called in any method of the class.

There are no problem if a signal or a method calls more than one time QObject::deleteLater()

Beginner developers sometimes use

delete this;

and this is dangerous since the Qt event loop could invoke a method of the deleted class. Hence, the application or library will crash.

  • Related Qt Documentation link

Qt 4.6 deleteLater()

91 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved