Results 1 to 13 of 13

Thread: what time should I delete the resource

  1. #1
    Join Date
    Jan 2011
    Posts
    127
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Question what time should I delete the resource

    According to the code come from
    C++ GUI Programming with Qt4, sec edition
    As the example of Ch2
    Qt Code:
    1. #ifndef FINDDIALOG_H
    2. #define FINDDIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. class QCheckBox;
    7. class QLabel;
    8. class QLineEdit;
    9.  
    10. class FindDialog : public QDialog
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. FindDialog(QWidget *parent = 0);
    16.  
    17. signals:
    18. void findNext(const QString &str, Qt::CaseSensitivity cs);
    19. void findPrevious(const QString &str, Qt::CaseSensitivity cs);
    20.  
    21. private slots:
    22. void findClicked();
    23. void enableFindButton(const QString &text);
    24.  
    25. private:
    26. QLabel *label;
    27. QLineEdit *lineEdit;
    28. QCheckBox *caseCheckBox;
    29. QCheckBox *backwardCheckBox;
    30. QPushButton *findButton;
    31. QPushButton *closeButton;
    32. };
    33.  
    34. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "finddialog.h"
    4.  
    5. FindDialog::FindDialog(QWidget *parent)
    6. : QDialog(parent)
    7. {
    8. label = new QLabel(tr("Find &what:"));
    9. lineEdit = new QLineEdit;
    10. label->setBuddy(lineEdit);
    11.  
    12. caseCheckBox = new QCheckBox(tr("Match &case"));
    13. backwardCheckBox = new QCheckBox(tr("Search &backward"));
    14.  
    15. findButton = new QPushButton(tr("&Find"));
    16. findButton->setDefault(true);
    17. findButton->setEnabled(false);
    18.  
    19. closeButton = new QPushButton(tr("Close"));
    20.  
    21. connect(lineEdit, SIGNAL(textChanged(const QString &)),
    22. this, SLOT(enableFindButton(const QString &)));
    23. connect(findButton, SIGNAL(clicked()),
    24. this, SLOT(findClicked()));
    25. connect(closeButton, SIGNAL(clicked()),
    26. this, SLOT(close()));
    27.  
    28. QHBoxLayout *topLeftLayout = new QHBoxLayout;
    29. topLeftLayout->addWidget(label);
    30. topLeftLayout->addWidget(lineEdit);
    31.  
    32. QVBoxLayout *leftLayout = new QVBoxLayout;
    33. leftLayout->addLayout(topLeftLayout);
    34. leftLayout->addWidget(caseCheckBox);
    35. leftLayout->addWidget(backwardCheckBox);
    36.  
    37. QVBoxLayout *rightLayout = new QVBoxLayout;
    38. rightLayout->addWidget(findButton);
    39. rightLayout->addWidget(closeButton);
    40. rightLayout->addStretch();
    41.  
    42. QHBoxLayout *mainLayout = new QHBoxLayout;
    43. mainLayout->addLayout(leftLayout);
    44. mainLayout->addLayout(rightLayout);
    45. setLayout(mainLayout);
    46.  
    47. setWindowTitle(tr("Find"));
    48. setFixedHeight(sizeHint().height());
    49. }
    50.  
    51. void FindDialog::findClicked()
    52. {
    53. QString text = lineEdit->text();
    54. Qt::CaseSensitivity cs =
    55. caseCheckBox->isChecked() ? Qt::CaseSensitive
    56. : Qt::CaseInsensitive;
    57. if (backwardCheckBox->isChecked()) {
    58. emit findPrevious(text, cs);
    59. } else {
    60. emit findNext(text, cs);
    61. }
    62. }
    63.  
    64. void FindDialog::enableFindButton(const QString &text)
    65. {
    66. findButton->setEnabled(!text.isEmpty());
    67. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "finddialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. FindDialog *dialog = new FindDialog;
    9. dialog->show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    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
    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
    Qt Code:
    1. std::unique_ptr
    To copy to clipboard, switch view to plain text mode 
    for it?
    for this example, the
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    should be declared as private members if I want to use
    Qt Code:
    1. std::unique_ptr
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 
    ?
    whatever, this makes me feel unsafe

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default 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:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QWidget *w = new QWidget(0);
    8.  
    9. QVBoxLayout *l = new QVBoxLayout(w); //has a parent
    10. QLabel *lbl = new QLabel("Hello World"); //no parent at construction, but it will get one later
    11. QPushButton *btn = new QPushButton("Close"); //no parent at construction, but it will get one later
    12. QObject::connect(btn, SIGNAL(clicked()), qApp, SLOT(quit()));
    13.  
    14. l->addWidget(lbl); //will reparent the label, w will be the parent
    15. l->addWidget(btn); //will reparent the button, w will be the parent
    16.  
    17. w->show();
    18.  
    19. int ret_val = app.exec();
    20.  
    21. delete w; //delete the parent, this will trigger the deletion of it's children
    22.  
    23. return ret_val;
    24. }
    To copy to clipboard, switch view to plain text mode 
    So yes, this code will leak:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. FindDialog *dialog = new FindDialog;
    5. dialog->show();
    6. return app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    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)

  3. The following user says thank you to Zlatomir for this useful post:

    stereoMatching (21st January 2011)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: what time should I delete the resource

    A very simplified explanation of what is going on:
    Qt Code:
    1. class QObject {
    2. public:
    3. QObject(QObject *parent = 0) : m_parent(0){
    4. setParent(parent);
    5. }
    6. void setParent(QObject *parent) {
    7. if(m_parent) m_parent->m_children.removeOne(this);
    8. m_parent = parent;
    9. if(!parent) return;
    10. parent->m_children.append(this);
    11. }
    12. ~QObject() {
    13. foreach(QObject *o, m_children) delete o;
    14. m_children.clear();
    15. setParent(0);
    16. }
    17. private:
    18. QList<QObject*> m_children;
    19. QObject *m_parent;
    20. };
    To copy to clipboard, switch view to plain text mode 

    Q_OBJECT macro is irrelevant here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    stereoMatching (21st January 2011)

  6. #4
    Join Date
    Jan 2011
    Posts
    127
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default 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

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: what time should I delete the resource

    Quote Originally Posted by stereoMatching View Post
    1 : What is the meaning of inheritance of the QDialog?
    I don't understand your question.
    2 : What make those widget and layout become the children of FindDialog?
    setLayout() reparents the layout and all items it manages.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    stereoMatching (21st January 2011)

  9. #6
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    18
    Thanked 68 Times in 66 Posts

    Default Re: what time should I delete the resource

    Quote Originally Posted by stereoMatching View Post
    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.

  10. The following user says thank you to FelixB for this useful post:

    stereoMatching (21st January 2011)

  11. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: what time should I delete the resource

    Quote Originally Posted by stereoMatching View Post
    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.

  12. The following user says thank you to nish for this useful post:

    stereoMatching (21st January 2011)

  13. #8
    Join Date
    Jan 2011
    Posts
    127
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default Re: what time should I delete the resource

    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

    setLayout() reparents the layout and all items it manages.
    so the
    Qt Code:
    1. setLayout(mainLayout)
    To copy to clipboard, switch view to plain text mode 
    would make those instances become the children of the
    Qt Code:
    1. mainLayout
    To copy to clipboard, switch view to plain text mode 
    ?
    Last edited by stereoMatching; 21st January 2011 at 09:53.

  14. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: what time should I delete the resource

    Quote Originally Posted by stereoMatching View Post
    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.
    I mean, what kind of situations should I inherit the QDialog?
    When u need a dialog like functionality
    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

  15. The following user says thank you to nish for this useful post:

    stereoMatching (21st January 2011)

  16. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: what time should I delete the resource

    Public inheritance is the same as Is a relationship, if you write:
    Qt Code:
    1. class FindDialog : public QDialog //here is the inheritance part
    2. {
    3. Q_OBJECT
    4. //rest of the class is extending the functionality of the QDialog class
    5. public:
    6. FindDialog(QWidget *parent = 0);
    7.  
    8. signals:
    9. void findNext(const QString &str, Qt::CaseSensitivity cs);
    10. void findPrevious(const QString &str, Qt::CaseSensitivity cs);
    11.  
    12. private slots:
    13. void findClicked();
    14. void enableFindButton(const QString &text);
    15.  
    16. private:
    17. QLabel *label;
    18. QLineEdit *lineEdit;
    19. QCheckBox *caseCheckBox;
    20. QCheckBox *backwardCheckBox;
    21. QPushButton *findButton;
    22. QPushButton *closeButton;
    23. };
    To copy to clipboard, switch view to plain text mode 
    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)

  17. The following user says thank you to Zlatomir for this useful post:

    stereoMatching (21st January 2011)

  18. #11
    Join Date
    Jan 2011
    Posts
    127
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default 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"

  19. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: what time should I delete the resource

    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

  20. The following user says thank you to stampede for this useful post:

    stereoMatching (21st January 2011)

  21. #13
    Join Date
    Jan 2011
    Posts
    127
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default Re: what time should I delete the resource

    Quote Originally Posted by stampede View Post
    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.

Similar Threads

  1. Replies: 5
    Last Post: 19th November 2010, 03:25
  2. Replies: 6
    Last Post: 18th August 2010, 13:52
  3. Replies: 2
    Last Post: 13th May 2009, 11:35
  4. Replies: 4
    Last Post: 19th February 2009, 12:10
  5. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 22:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.