what time should I delete the resource
According to the code come from
Quote:
C++ GUI Programming with Qt4, sec edition
As the example of Ch2
Code:
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
{
Q_OBJECT
public:
signals:
void findNext
(const QString &str, Qt
::CaseSensitivity cs
);
void findPrevious
(const QString &str, Qt
::CaseSensitivity cs
);
private slots:
void findClicked();
void enableFindButton
(const QString &text
);
private:
};
#endif
Code:
#include <QtGui>
#include "finddialog.h"
FindDialog
::FindDialog(QWidget *parent
){
label
= new QLabel(tr
("Find &what:"));
label->setBuddy(lineEdit);
caseCheckBox
= new QCheckBox(tr
("Match &case"));
backwardCheckBox
= new QCheckBox(tr
("Search &backward"));
findButton->setDefault(true);
findButton->setEnabled(false);
connect(lineEdit,
SIGNAL(textChanged
(const QString &)),
this,
SLOT(enableFindButton
(const QString &)));
connect(findButton, SIGNAL(clicked()),
this, SLOT(findClicked()));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(close()));
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
void FindDialog::findClicked()
{
Qt::CaseSensitivity cs =
caseCheckBox->isChecked() ? Qt::CaseSensitive
: Qt::CaseInsensitive;
if (backwardCheckBox->isChecked()) {
emit findPrevious(text, cs);
} else {
emit findNext(text, cs);
}
}
void FindDialog
::enableFindButton(const QString &text
) {
findButton->setEnabled(!text.isEmpty());
}
Code:
#include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
I do not see the code release any resource of the class
and I haven't see they use anything like smart pointer
the book said
Quote:
Qt automatically deletes child objects when the parent is destroyed, and
the child widgets and layouts are all descendants of the FindDialog
did that mean if all of the dynamic memory are allocated within class
Qt would release the resource for us?
Could I just use smart pointer like for it?
for this example, the and should be declared as private members if I want to use
I am very worry about the coding style like that
it do not "seems like" they release the memory they claim for
I don't want to fall into the trap of memory allocation in the future
Thanks a lot
ps : the class would release the resource because we declare the ?
whatever, this makes me feel unsafe
Re: what time should I delete the resource
Let's take the Hello World example and explain how to deal with allocating memory on heap for the parent:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
QLabel *lbl
= new QLabel("Hello World");
//no parent at construction, but it will get one later QObject::connect(btn,
SIGNAL(clicked
()),
qApp,
SLOT(quit
()));
l->addWidget(lbl); //will reparent the label, w will be the parent
l->addWidget(btn); //will reparent the button, w will be the parent
w->show();
int ret_val = app.exec();
delete w; //delete the parent, this will trigger the deletion of it's children
return ret_val;
}
So yes, this code will leak:
Code:
int main(int argc, char *argv[])
{
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
You can allocate memory on the stack (here in main.cpp) but in any other parts you will need to be careful about the lifetime of your objects (it's a pretty common mistake to allocate on the stack and the objects get deleted before you try to use them)
Re: what time should I delete the resource
A very simplified explanation of what is going on:
Code:
public:
setParent(parent);
}
if(m_parent) m_parent->m_children.removeOne(this);
m_parent = parent;
if(!parent) return;
parent->m_children.append(this);
}
foreach
(QObject *o, m_children
) delete o;
m_children.clear();
setParent(0);
}
private:
QList<QObject*> m_children;
};
Q_OBJECT macro is irrelevant here.
Re: what time should I delete the resource
Thanks, but I still have some questions
1 : What is the meaning of inheritance of the QDialog?
2 : What make those widget and layout become the children of FindDialog?
Thank you
Re: what time should I delete the resource
Quote:
Originally Posted by
stereoMatching
1 : What is the meaning of inheritance of the QDialog?
I don't understand your question.
Quote:
2 : What make those widget and layout become the children of FindDialog?
setLayout() reparents the layout and all items it manages.
Re: what time should I delete the resource
Quote:
Originally Posted by
stereoMatching
1 : What is the meaning of inheritance of the QDialog?
The FindDialog is a dialog. By inheritance, the FindDialog behaves like a QDialog. You only have to define your custom behaviour.
Re: what time should I delete the resource
Quote:
Originally Posted by
stereoMatching
1 : What is the meaning of inheritance of the QDialog?
at your first question i gave you the benefit of doubt, but this question proves that you need to learn a little bit of C++ before going into Qt. This really helps.
Re: what time should I delete the resource
Quote:
I don't understand your question.
I mean, what kind of situations should I inherit the QDialog?
what is the jobs QDialog do for me in this FindDialog class?
when should I inherit the QDialog?
The book just inherit it anyway...
I don't even know what jobs(behavior) of the QDialog could do for me
Quote:
setLayout() reparents the layout and all items it manages.
so the
Code:
setLayout(mainLayout)
would make those instances become the children of the ?
Re: what time should I delete the resource
Quote:
Originally Posted by
stereoMatching
I mean, what is the jobs QDialog do for me in this FindDialog class?
Its giving you all the functions which are defined in QDialog class. The most important function is of showing/drawing the Window to the screen.
Quote:
I mean, what kind of situations should I inherit the QDialog?
When u need a dialog like functionality
Quote:
what is the jobs QDialog do for me in this FindDialog class?
when should I inherit the QDialog?
The book just inherit it anyway...
I don't even know what jobs(behavior) of the QDialog could do for me
i think i explained already
Re: what time should I delete the resource
Public inheritance is the same as Is a relationship, if you write:
Code:
class FindDialog
: public QDialog //here is the inheritance part{
Q_OBJECT
//rest of the class is extending the functionality of the QDialog class
public:
signals:
void findNext
(const QString &str, Qt
::CaseSensitivity cs
);
void findPrevious
(const QString &str, Qt
::CaseSensitivity cs
);
private slots:
void findClicked();
void enableFindButton
(const QString &text
);
private:
};
Your class will still behave like a QDialog (even like a QWidget see that you can pass your class pointer as a parent to other QWidgets or QWidget derived instances)
But mainly inheritance is used for extending the functionality of another class, and also when inherit from some class you can "redefine" some functions (the ones declared virtual and override is the term for that), but this is more a polymorphism topic, don't you love how OOP topics come together? ;)
Take a C++ book, make sure it contains OOP topics before you buy it, and there you will have a few chapters explaining this (since we can't do that in a forum post ;) we just give some ideas, but this requires a few chapters in a book)
Re: what time should I delete the resource
Thanks, I am not a master of OOP but I think I know what is "is a" relationship and "has a" relation ship
But I still can't figure out what is protected inheritance want to do
I only wonder what kind of behaviour the QDialog could give me and what time should I inherit it
And the previous post gave me the answer
I don't need to know how are they implement it(at least for now)
PS : I don't like to use inheritance in most of the time
I would prefer "has a" and "static polymorphism" rather than "dynamic polymorphism"
Re: what time should I delete the resource
Quote:
But I still can't figure out what is protected inheritance
It's a kind of "has-a" relationship with possibility to access protected members of contained object. There is more about it, but maybe better leave it alone for now, just focus on the basics.
If you are curious: private and protected inheritance
Re: what time should I delete the resource
Quote:
Originally Posted by
stampede
It's a kind of "has-a" relationship with possibility to access protected members of contained object. There is more about it, but maybe better leave it alone for now, just focus on the basics.
If you are curious:
private and protected inheritance
Thanks, but I don't think I would use any protected inheritance since public inheritance
and private inheritance(only some extreme cases would be considered) are capable to mimic most of the cases.