@high_flyer
I understand what you said. Thank you!
In standard C++ programming, there should be delete/free operation corresponding to new/malloc. Howere, in Qt, sometimes delete seems to be unnecessary. Here is the first program in C++ GUI Programming with QT4:
#include <QApplication>
#include <QLabel>
int main(int argc, char* argv[])
{
label->show();
return app.exec();//memory leak? correction: int ret = app.exec(); delete label; return ret; right?
}
#include <QApplication>
#include <QLabel>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();//memory leak? correction: int ret = app.exec(); delete label; return ret; right?
}
To copy to clipboard, switch view to plain text mode
And what about other examples?No memory leak? and what's the reason plz?
//findfiledialog.h
class FindFileDialog
: public QDialog{
Q_OBJECT
public:
FindFileDialog
(QWidget *parent
= 0);
private:
}
//findfiledialog.cpp
#include <QtGui>
#include "findfiledialog.h"
FindFileDialog
::FindFileDialog(QWidget *parent
){
namedLabel
= new QLabel(tr
("&Named:"));
namedLabel->setBuddy(namedLineEdit);
....
....
}
//main.cpp
#include <QApplication>
#include "findfiledialog.h"
int main(int argc, char *argv[])
{
FindFileDialog dialog;
dialog.show();
return app.exec();
}
//findfiledialog.h
class FindFileDialog : public QDialog
{
Q_OBJECT
public:
FindFileDialog(QWidget *parent = 0);
private:
QLabel *namedLabel;
QLabel *lookInLabel;
QLineEdit *lookInLineEdit;
}
//findfiledialog.cpp
#include <QtGui>
#include "findfiledialog.h"
FindFileDialog::FindFileDialog(QWidget *parent)
: QDialog(parent)
{
namedLabel = new QLabel(tr("&Named:"));
namedLineEdit = new QLineEdit;
namedLabel->setBuddy(namedLineEdit);
....
QGridLayout *leftLayout = new QGridLayout;
....
}
//main.cpp
#include <QApplication>
#include "findfiledialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindFileDialog dialog;
dialog.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
@wysota
Thank you! By saying examples in the book call a method that sets a QObject parent to object created on the heap without explicitly passing a parent to the constructor of the object, then can you plz explain to me what's the METHOD? or any documentation on this question?
Bookmarks