Results 1 to 8 of 8

Thread: I have a class with Qtimer, if I dont create it as new it does not works...

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default I have a class with Qtimer, if I dont create it as new it does not works...

    I dont know very well what is happen....

    I have a class with some utilities, one of them is a cronometer.

    Qt Code:
    1. W_qtutil *w_qt=new W_qtutil();
    2. w_qt->time_start_crono(ui->my_label);
    To copy to clipboard, switch view to plain text mode 
    This works .

    But this one does not :
    Qt Code:
    1. W_qtutil w_qt;
    2. w_qt.time_start_crono(ui->my_label);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void W_qtutil::time_start_crono(QWidget *indicador) {
    2. m_timer = new QTimer();
    3. m_timer->setInterval(1000);
    4. QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(time_update_crono()));
    5. m_timer->start();
    6. }
    To copy to clipboard, switch view to plain text mode 
    (m_timer is public, and declared at .h section)

    Any idea ?

  2. #2
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    In the case that doesn't work, I would imagine that the reason is that w_qt gets out of scope before your timer has time to emit its timeout signal.

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    Thanks
    So, If I use the *var = new ... it means that my object are going to live outside of the method where it was created?

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    Sorry, but you need to study about C++ and only later about Qt.

    You have to know about stack variables and heap variables.
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    Quote Originally Posted by tonnot View Post
    Thanks
    So, If I use the *var = new ... it means that my object are going to live outside of the method where it was created?
    Yes, and unless you free it, it will become what we call a memory leak (i.e. the kind of thing that should be avoided at all costs). Please note that Qt widget classes may be instantiated by providing a parent to the widget. So, unless you don't provide a parent, the widget object will be automatically freed when its parent is deleted.

  6. #6
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    Thanks agarny.
    And I can be sure that an object that has parent is destroyed when the parent die ? Or have I to delete it ?

  7. #7
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    If you were to check the source code (on line 1643), you would realise that this is exactly what it does...

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I have a class with Qtimer, if I dont create it as new it does not works...

    Quote Originally Posted by tonnot View Post
    Thanks agarny.
    And I can be sure that an object that has parent is destroyed when the parent die ? Or have I to delete it ?
    If a heap-allocated QObject (QTimer is one) has a parent QObject then it will be deleted when the parent is deleted. You can still delete the child manually if you want. A QObject without a parent is like any other C++ object with regard to lifetime: scoped if allocated on the stack, or until deleted if on the heap.

Similar Threads

  1. A class with Qtimer
    By tonnot in forum Newbie
    Replies: 6
    Last Post: 9th February 2011, 17:40
  2. i dont get actions with "connect" works
    By pakine in forum Newbie
    Replies: 2
    Last Post: 7th July 2010, 15:17
  3. class and event create
    By electronicboy in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2009, 23:07
  4. Replies: 2
    Last Post: 9th September 2009, 00:26
  5. Replies: 4
    Last Post: 1st May 2009, 11:00

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.