Active Topics

 


Reply
Thread Tools
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#11
Parent can be any class that inherits from QObject. QWidgets are inherited from QObject, so yes.
If you create a dialog, give all member pointer variables the dialog as parent and you don't have to care about their memory
With Qt the memory handling is a lot easier than with blank c++.

PS: You could also take a look at QSharedPointer when using non-QObject classes. (or boost::shared_ptr if you don't like the Qt implementation)
 
Posts: 3,319 | Thanked: 5,610 times | Joined on Aug 2008 @ Finland
#12
Qt also provides some convenience classes for people wishing more pointer automatism:

http://doc.qt.nokia.com/4.6/qscopedpointer.html

http://doc.qt.nokia.com/4.6/qsharedpointer.html

Not exactly garbage collection, but it's as close as it gets to it in 'standard' C++.
__________________
Blogging about mobile linux - The Penguin Moves!
Maintainer of PyQt (see introduction and docs), AppWatch, QuickBrownFox, etc
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#13
Originally Posted by gri View Post
Parent can be any class that inherits from QObject. QWidgets are inherited from QObject, so yes.
If you create a dialog, give all member pointer variables the dialog as parent and you don't have to care about their memory
With Qt the memory handling is a lot easier than with blank c++.
so in constructor method i've to define:

className(parameters, QObject parent);

am i right?
 
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#14
Originally Posted by saxen View Post
so in constructor method i've to define:

className(parameters, QObject parent);

am i right?
Not sure what you mean.

example implementation code:
Code:
MyClass::MyClass(QObject* parent)
: QObject(parent) // Not passing the parent to the QObject base will also result in memory leakage
{
    mySubObject = new QObject(this); // Will be deleted when "MyClass" dies
    myLeakObject = new QObject; // Will live forever
}
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#15
Originally Posted by gri View Post
Not sure what you mean.

example implementation code:
Code:
MyClass::MyClass(QObject* parent)
: QObject(parent) // Not passing the parent to the QObject base will also result in memory leakage
{
    mySubObject = new QObject(this); // Will be deleted when "MyClass" dies
    myLeakObject = new QObject; // Will live forever
}
yes, that's what i meant!! really thanks
 
Posts: 124 | Thanked: 10 times | Joined on Jan 2007 @ Italy
#16
while i'm bothering you with this silly questions, i've another doubt about inclusions

how to use #include? i mean it's different from java import, so using include(from what i remember) means include "piece of code"(correct if i'm wrong) and not just import a name space!

so, how to avoid duplicate include?
suppose i have main class, firstclass and secondclass, if i include secondclass in firstclass, and first class in main, does main class automatically include secondclass?

i hope u can understand what i'm trying to say
 
damnshock's Avatar
Posts: 179 | Thanked: 86 times | Joined on Dec 2009 @ Barcelona
#17
Originally Posted by saxen View Post
while i'm bothering you with this silly questions, i've another doubt about inclusions

how to use #include? i mean it's different from java import, so using include(from what i remember) means include "piece of code"(correct if i'm wrong) and not just import a name space!

so, how to avoid duplicate include?
suppose i have main class, firstclass and secondclass, if i include secondclass in firstclass, and first class in main, does main class automatically include secondclass?

i hope u can understand what i'm trying to say
#include is macro directive, an it does what it says: *includes*. You'll make your program bigger in size


#ifndef H_WHATEVER
#define H_WHATEVER


//CODE

#endif

Does this solve your problem?

PS:I'm too learning C++ to develop with Qt
 

The Following User Says Thank You to damnshock For This Useful Post:
Posts: 190 | Thanked: 129 times | Joined on Mar 2010 @ Bavaria, Germany
#18
You can also use "#pragma once" instead of the ifndef guards. But this is not supported by every compiler.
So with ifndefs you are on the safe side, with pragma once you type less ... of course, you could also use both of them in one file.
 

The Following User Says Thank You to gri For This Useful Post:
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#19
Compilation of C++ (and C) happens in a couple of different steps:

- Preprocessor (handles #include, #define, #if, #ifdef and the like)
- Compiler (parses all the code and builds an abstract representation, AST, that's then changed in different ways for some kinds of optimization and other compiler magic)
- Generate assembler (the compiler backend translates the AST to platform specific assembler)
- Assembler (the assembler read the assembly code and translates it into object code with link references as special markers)
- Linker (links the compilation units (object code blobs) together and fixes link markers to reference shared libraries)

The preprocessor can be thought of as just a file manipulator that puts together the file that is to be parsed by the compiler. #include is a character by character insertion of another file.

As explained above, #ifdef is the classical way of preventing multiple includes.
 

The Following User Says Thank You to Joorin For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 03:23.