Results 1 to 6 of 6

Thread: Check has created with new

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Check has created with new

    How to check object has created with new ? I tried like this, but not work


    Qt Code:
    1. Tahede *_tahede;
    2. //_tahede=new Tahede(this);
    3. if (_tahede)
    4. {
    5. qDebug() << "Created before";
    6.  
    7. _tahede->exec();
    8. }
    9. else
    10. {
    11. qDebug() << "Create now";
    12.  
    13. _tahede=new Tahede(this);
    14. _tahede->exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Application output window only give me message...

    Created before
    QDialog::exec: Recursive call detected

  2. #2
    Join Date
    Dec 2009
    Posts
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check has created with new

    If you intend there to be only one Tahede instance in your program, please look at the singleton pattern. If you want to have a delayed construction of a member variable, you need to initialize it to 0 first. Perhaps the following would work (might not compile verbatim, inline code for a brief example):
    Qt Code:
    1. class MyObject : public QObject
    2. {
    3. public:
    4. MyObject() : _tahede(0) {}
    5.  
    6. void doExec() {
    7. tahede()->exec();
    8. }
    9.  
    10. protected:
    11. Tahede* tahede() {
    12. if(!_tahede)
    13. _tahede = new Tahede(this);
    14. return _tahede;
    15. }
    16.  
    17. private:
    18. Tadede* _tahede;
    19. };
    To copy to clipboard, switch view to plain text mode 

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

    wirasto (11th December 2009)

  4. #3
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: Check has created with new

    Thanks for your replay. But, how delete that object. I modified your code sample...

    Qt Code:
    1. void doExec() {
    2. tahede()->exec();
    3. }
    4.  
    5. void doDelete() {
    6. delete _tahede;
    7. }
    8.  
    9. protected:
    10. void changeEvent(QEvent *e);
    11.  
    12. Tahede* tahede() {
    13. if(!_tahede)
    14. {
    15. _tahede = new Tahede(this);
    16.  
    17. qDebug() << "Create now";
    18. }
    19. else
    20. {
    21. qDebug() << "Created before";
    22. }
    23. return _tahede;
    24. }
    25.  
    26. private:
    27. Tahede* _tahede;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Dialog::on_pushButton_clicked()
    2. {
    3. doExec();
    4. }
    5.  
    6. void Dialog::on_pushButton_2_clicked()
    7. {
    8. doDelete();
    9. }
    To copy to clipboard, switch view to plain text mode 


    After I deleted with doDelete() function, and doExec() again, Application output still give me message "Created before" and my application crash What's wrong ?

  5. #4
    Join Date
    Aug 2009
    Posts
    52
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check has created with new

    when you delete your object, your had better:

    delete _tahede;
    _tahede = 0;

  6. The following user says thank you to dbzhang800 for this useful post:

    wirasto (11th December 2009)

  7. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Check has created with new

    or use a QPointer whis sets automagicaly itself to 0 when the object is deleted:
    Qt Code:
    1. private:
    2. QPointer<Tadede> _tahede;
    3. };
    To copy to clipboard, switch view to plain text mode 
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    wirasto (11th December 2009)

  9. #6
    Join Date
    Sep 2007
    Location
    Germany
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Check has created with new

    Hi There,

    i have a small problem while casting from QPointer<T> to QPointer<ChildT>

    I tried this.

    QPointer<T> tObject=new T();
    //QPointer<ChildT> childtObject=(QPointer<T>)tObjec;// this does not work;
    QPointer<ChildT> childtObject=(ChildT*)(T*)tObjec;// this work;
    Last edited by cafu; 16th March 2010 at 16:12.
    CAFU......

Similar Threads

  1. Help: How to save Check box state
    By Garibalde in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 20:24
  2. Trouble while viewing a created a toolbar!!
    By aditya.kaole in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2009, 21:04
  3. Replies: 0
    Last Post: 2nd May 2008, 07:57
  4. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43
  5. Line not being created
    By Kapil in forum Newbie
    Replies: 4
    Last Post: 30th March 2006, 06:49

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.