Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qtimer parent delete?

Qtimer parent delete?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sifourquier
    wrote on last edited by
    #1

    Hi
    I creat a Qtimer with a parent

    QNetworkReply* reply=manager->get(req);
    QTimer* timouteReply=new QTimer(reply);
    timouteReply->singleShot(TIMEOUT_REQUEST,this,SLOT(timouteReply()));
    

    after if reply call the finished i delete reply

    reply->deletelater();
    

    But somme time after the timeout() of timouteReply are call!

    I think when i call reply->deletelater(); when reply are deleted he need delete childrent then he need delete timouteReply!

    what is wrong?

    If you can help me thanks you.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What is "some time" ?
      What is the timeout value of your timer ?
      Why not cancel it when your reply has finished ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3
        1. use deletelater.
        2. Hope you have connected the network reply with slots. When the reply comes back from network reply, inside the slot just call timouteReply.stop(..)

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sifourquier
          wrote on last edited by sifourquier
          #4

          thanks for your reply but after many test i fund if i creat a timer, run singelshot() and delete timer
          for exemple

          	QTimer * t=new QTimer();
          	t->singleShot(1000,this,SLOT(timeout()));
          	delete(t);
          

          the timout are call

          and i have found this thread
          https://lists.qt-project.org/pipermail/qt-interest-old/2009-October/013926.html

          I'is normal? or i need open a issue?

          (use deletelater do not solve problems)

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            I'is normal? or i need open a issue?

            Yes, it's normal. singleShot() is a static method so it doesn't matter if you call it statically ( with ::) or on an object (with ->). It always calls the same static function. You don't need that timer object at all.

            As for your problem - the slot timeout tries to access an object that lives shorter than the connection that is made with singleShot. To make sure the connection lives only as long as that object you need to pass a scope object for that connection. That scope object is the second parameter of singleShot. You gave it this which makes little sense in that scenario. Give it an object that lives only as long as the reply, so, for example, the reply object itself:

            QNetworkReply* reply=manager->get(req);
            QTimer::singleShot(TIMEOUT_REQUEST, reply, [=]{ timeout(); });
            

            This way the connection is severed when reply is deleted and the timer will cancel the timeout.

            1 Reply Last reply
            2
            • S Offline
              S Offline
              sifourquier
              wrote on last edited by
              #6

              thanks you
              i confirme

              connect(timouteReply,SIGNAL(timeout()),this,SLOT(timoutReply()));
              timouteReply->setInterval(TIMEOUT_REQUEST);
              timouteReply->setSingleShot(1);
              timouteReply->start();
              

              work fine

              i have never see single shoot are not only a fast way to set a timer in setSingleShot.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved