Qt-interest Archive, July 2006
deletion of the objects qt3
Message 1 in thread
Hi,
could someone explain me good habits when using qt?
1)
i understood that all things which are of type QObject or QWidget are
deleted automatically during quit of the program. If I want to use this
mechanism, is it then necessary to inherit my classes (which have
nothing to do with Qt, which are for example just data holders) from
QObject and create them with call of a parentclass pointer? Do I
understand correctly, that the only case when I make use of 'delete
myclass' myself is when I need to recreate it during the process of the
program? (for example when one data structure is replaced by other one?)
So in destructor no deletion of the object should be requested?
2)
what is the best way to store the default/last inserted values into a
dialog box?
3)
could someone show me a hint how to do following thing? I would like to
access the menu items via a key combination. For example I have a menu
'Edit/Cut'. I would like to run this
just by pressing a key combination 'EC' instead of Alt-E, C. I think
that this has something to do with QAccel, but I'm not sure how to
handle it correctly
4) statusbar problem: when I run this from constructor:
--------------------------------------------
if ( (qp_coordsIndicator = new QLabel (statusBar(), QString
("xyinfo"))) != NULL)
{
// set some starting text
qp_coordsIndicator->setText (QString ("x= y="));
statusBar()->addWidget (qp_coordsIndicator, 0, TRUE);
}
----------------------------------------------
it gives corrects results, ie. there is a small box in statusbar on the
right which displays the text. However, when I do the same thing on the
runtime somewhere
in the program, the box is not inserted and status bar is empty. Does
this sort of insertion needs a special action after to make the widget
visible?
thanks for any response. although i'm not very new to qt, i have the
impression that some of my programming habits which concern QT are not
very good. that's
why i would like you to give me hints how you solve this sort of problems.
thx.
d.
--
[ signature omitted ]
Message 2 in thread
dejfson wrote:
> 1)
> i understood that all things which are of type QObject or QWidget
> are deleted automatically during quit of the program.
Everything is freed when you quit the program.
The mechanism you are referring to works like this:
In normal C++, you create and destroy objects with new and delete.
QObject* test = new QObject();
// do stuff
delete test;
A problem arises when you do the following:
QObject* test = new QObject();
QObject* test = new QObject();
You now have created two objects, but in test, only the address of
the second object is stored. The first object is now completely
unaccessible and will stay in memory for the rest of your program's
runtime. If you run "delete test", only the second one is deleted.
The Qt guys thought "that's bad", so they implemented parenting. If
you create a Qt object like that:
QObject* parent = new QObject();
... you are able to create other objects as its children, like this:
QObject* child = new QObject(parent);
QObject* child = new QObject(parent);
QObject* child = new QObject(parent);
By specifying a pointer to the parent object in the child
constructor, the childs "register" with the parent object, so that
the parent knows all children. If you now say
delete parent;
... the *parent* takes care of deleting all three children.
> If I want to
> use this mechanism, is it then necessary to inherit my classes
> (which have nothing to do with Qt, which are for example just data
> holders) from QObject and create them with call of a parentclass
> pointer?
No, it must be a pointer to a parent class *instance* (which must
also be derived from or be a QObject).
(It doesn't make any primary difference in C++, but e.g. in Python
classes and class instances are objects, which may lead to
confusion.)
> Do I understand correctly, that the only case when I make
> use of 'delete myclass' myself is when I need to recreate it
> during the process of the program? (for example when one data
> structure is replaced by other one?) So in destructor no deletion
> of the object should be requested?
I'm not sure if I understand you right. You may also delete any
child at any time, which "unregisters" it at the parent object and
destroys it. The only thing the parent does for you is to make sure
that no child survives if the parent is destroyed (uh,
horribe :) ), which prevents memory leaks.
BTW, now that I wrote that I remember a doc link which describes it
too.
http://doc.trolltech.com/4.2/objecttrees.html
(don't know anything about the menu stuff, because I use Qt on
console -- sorry)
Regards,
Björn
--
[ signature omitted ]
Message 3 in thread
Bjoern Schliessmann wrote:
> dejfson wrote:
>> ...
>>Do I understand correctly, that the only case when I make
>>use of 'delete myclass' myself is when I need to recreate it
>> ...
> I'm not sure if I understand you right. You may also delete any
> child at any time, which "unregisters" it at the parent object and
> destroys it. The only thing the parent does for you is to make sure
> that no child survives if the parent is destroyed (uh,
> horribe :) ), which prevents memory leaks.
Just for the record: a common situation is a dialog which is to be
temporarily shown, for example a file browser dialog (and I'm not
talking about the static functions QFileDialog::getOpenFileName etc. for
now).
The parent would usually be a QMainWindow. But since the QMainWindow
usually survives until the end of the lifetime of the application all
dialog instances would remain in memory, too (until they are correctly
deleted at the very end, when the application quits), eating up
unnecessary resources.
So deleting such dialogs is perfectly possible here (the other or
preferred way would be allocating such dialogs on the stack, so they are
automatically removed, but there might be a situation where this is not
possible and you really want to allocate the dialog - or whatever other
QObject based object - on the heap; see the Qt interest archive for an
extensive discussion/matter of taste about "heap vs. stack" allocation -
but be warned! ;)
void Foo::showFileDialog()
{
QFileDialog *fd;
// m_myMainWindow being a QMainWindow for example
fd = new QFileDialog (m_myMainWindow);
...
// now we are done with it, 'fd' correctly informs
// its parent, the 'm_myMainWindow'
delete fd;
}
or on the stack:
void Foo::showFileDialog()
{
QFileDialog fd (m_myMainWindow);
...
// and that's it, no worry about 'fd', it gets deleted
// as soon as it goes out of scope here
}
--
[ signature omitted ]