Results 1 to 11 of 11

Thread: Program crashes when Canon ball reaches "water"

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes when Canon ball reaches "water"

    Oh wow, that looks much better and easier to do, thank you !! Tho may i just ask, What is the point of having & operator at the beginning of QObject::destroyed and Ship:: onCannonBallDestroyed ? And why do you have to call onCannonballDestroyed using scope, if the method is part of the class Ship?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes when Canon ball reaches "water"

    What is the point of having & operator at the beginning of QObject:: destroyed and Ship:: onCannonBallDestroyed
    Qt Code:
    1. connect( canonball, &QObject::destroyed, this, &Ship::onCannonballDestroyed );
    To copy to clipboard, switch view to plain text mode 

    This is the new, type-safe form of QObject::connect(), which uses the "pointer to member function" style of giving the signal and slot pair. "&QObject:: destroyed" is the address of (pointer to) the "destroyed" signal (a member function) of the QObject class.

    It is better to use this new style instead of the old one:

    Qt Code:
    1. connect( cannonball, SIGNAL( destroyed( QObject * ), this, SLOT( onCannonballDestroyed( QObject * ) ) );
    To copy to clipboard, switch view to plain text mode 

    because the new style will not compile if you give the wrong arguments. The old style will compile, but will fail at run time if you give it the wrong arguments. That isn't so good, because unless you read the messages coming from the runtime output in the debugger, you will not know that the connect() failed and will wonder why your app isn't handling the signals in the way you think it should be.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    MongKong (29th March 2019)

  4. #3
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes when Canon ball reaches "water"

    Oh i see, then i believe this works with timers aswell and timeout() method?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Program crashes when Canon ball reaches "water"

    Just as added info: the life time of a QObject can also be tracked with a QPointer
    Qt Code:
    1. class Ship : public QObject
    2. {
    3. private:
    4. QPointer<Canon> m_cannonBall;
    5. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Ship::fireCannonball()
    2. {
    3. if ( canFireCannon() )
    4. {
    5. m_cannonBall = new Canon( this );
    6. }
    7. }
    8.  
    9. bool Ship::canFireCannon()
    10. {
    11. return m_cannonBall.isNull();
    12. }
    To copy to clipboard, switch view to plain text mode 

    I.e. a QPointer tracks the QObject it is holding and resets itself to nullptr if the object is deleted

    Cheers,
    _

  6. The following 2 users say thank you to anda_skoa for this useful post:

    d_stranz (29th March 2019), MongKong (29th March 2019)

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes when Canon ball reaches "water"

    Thanks, anda_skoa. I've learned something new. QPointer<> is a bit more useful in this circumstance and leads to less code and overhead.

    As I understand the docs, QPointer<> does not do reference counting in the way that std:: shared_ptr<> does. Also unlike shared_ptr, QPointer is not responsible for the lifetime of its object pointer. This also implies that you could potentially have several QPointer instances wrapping the same QObject pointer, correct? Presumably all of them connect to the same QObject:: destroyed() signal, and would simply set their internal copy of the pointer to null.

    I will have to think of a good use case for this.

    Oh i see, then i believe this works with timers aswell and timeout() method?
    Qt Code:
    1. connect( myTimer, &QTimer::timeout, this, &ThisClass::onTimeout );
    To copy to clipboard, switch view to plain text mode 

    (Of course, substitute the real name of your class for "ThisClass")

    Qt Code:
    1. connect( myTimer, &QTimer::timeout, [=]() { cannonball->deleteLater(); } );
    To copy to clipboard, switch view to plain text mode 

    Works with lambdas, too.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    MongKong (29th March 2019)

  9. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Program crashes when Canon ball reaches "water"

    Quote Originally Posted by d_stranz View Post
    Thanks, anda_skoa. I've learned something new.


    Quote Originally Posted by d_stranz View Post
    As I understand the docs, QPointer<> does not do reference counting in the way that std:: shared_ptr<> does.
    Also unlike shared_ptr, QPointer is not responsible for the lifetime of its object pointer.
    In terms of smart pointers you can think of QPointer as a weak-pointer, created from a shared-pointer inside the object itself.

    Quote Originally Posted by d_stranz View Post
    This also implies that you could potentially have several QPointer instances wrapping the same QObject pointer, correct?
    Yes, correct.

    Quote Originally Posted by d_stranz View Post
    Presumably all of them connect to the same QObject:: destroyed() signal, and would simply set their internal copy of the pointer to null.
    It is a bit more sophisticated than that

    Originally the implementation used some Qt internal functions to register the QPointer instances with the objects they were holding and the object would then notify the pointers as part of their destruction code.
    I.e. without going through the signal mechansim (as that would have required QPointer to be a QObject).

    The internals of the implementation changed and now literally uses a QWeakPointer for storing and monitoring.

    Quote Originally Posted by d_stranz View Post
    I will have to think of a good use case for this.
    Super useful in many circumstances.

    Cheers,
    _

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

    MongKong (29th March 2019)

  11. #7
    Join Date
    Mar 2019
    Posts
    22
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Program crashes when Canon ball reaches "water"

    d_stranz and anda_skoa thank you both for help and advices, i appreciate it a lot thank you again !!!!!

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:02
  2. Replies: 3
    Last Post: 16th March 2015, 07:31
  3. Replies: 1
    Last Post: 5th February 2011, 21:14
  4. "Cannot run program "C:\Qt\4.3.3\bin\qmake": file not found
    By PeteH in forum Installation and Deployment
    Replies: 1
    Last Post: 7th February 2009, 00:48
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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