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. Help Please! Signals & Slots: When objects destroyed
QtWS25 Last Chance

Help Please! Signals & Slots: When objects destroyed

Scheduled Pinned Locked Moved Unsolved General and Desktop
signals & slotsqobjectqtimer
3 Posts 3 Posters 4.8k Views
  • 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
    scottnat
    wrote on 5 Jan 2018, 07:20 last edited by
    #1

    Hi all, I want to set up three timer objects such that,

    • When two of them have completed their slots after some X iterations, they both get disconnected and destroyed.
    • Only when both are destroyed, the third timer activates.
    • I am having trouble with the destroyed(QObject*) signals for the first two timers.
    • They have to be interval type.
    • I am not able to get the object name of the destroyed object from the signal emitted by destroyed()
    • Why is it that slot B is displayed first, even though I called timerA->start() first?

    Here is what I have done so far.

    QDebug Output:
    slot B
    slot A
    Destroyed ""
    Destroyed ""

    int A,B,C;
    bool Adestroyed =false, Bdestroyed= false;
    
    void MainWindow::doThat()
    {
        timerA = new QTimer(this);
        timerB = new QTimer(this);
        timerC = new QTimer(this);
        timerA->setInterval(100);
        timerB->setInterval(100);
        timerC->setInterval(100);
        connect(timerA,SIGNAL(timeout()),this,SLOT(slotA()));
        connect(timerB,SIGNAL(timeout()),this,SLOT(slotB()));
        connect(timerC,SIGNAL(timeout()),this,SLOT(slotC()));
    
        connect(timerA,SIGNAL(destroyed(QObject*)),this,SLOT(sigIsDisconnected(QObject*)));
        connect(timerB,SIGNAL(destroyed(QObject*)),this,SLOT(sigIsDisconnected(QObject*)));
    
        timerA->start();
        timerB->start();
    
    }
    
    void MainWindow::slotA()
    {
        if(A<1){
            qDebug()<<"slot A";
            A++;
        }
        else{
            timerA->stop();
            disconnect(timerA,SIGNAL(timeout()),this,SLOT(slotA()));
            timerA->destroyed(this->timerA);
        }
    
    }
    
    void MainWindow::slotB()
    {
        if(B<1){
            qDebug()<<"slot B";
            B++;
        }
        else{
            timerB->stop();
            disconnect(timerB,SIGNAL(timeout()),this,SLOT(slotB()));
            timerB->destroyed(this->timerB);
        }
    }
    
    void MainWindow::slotC()
    {
        if(C<2){
            qDebug()<<"slot C";
            C++;
        }
        else{
            timerC->stop();
            disconnect(timerC,SIGNAL(timeout()),this,SLOT(slotC()));
        }
    }
    
    void MainWindow::sigIsDisconnected(QObject* obj)
    {
        qDebug()<<"Destroyed "<<obj->objectName();
    
            if(obj->objectName()=="timerA"){
                Adestroyed=true;
            }
            if(obj->objectName()=="timerB"){
                Bdestroyed=true;
            }
    
            if(Adestroyed&&Bdestroyed){
                timerC->start();
            }
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Jan 2018, 07:42 last edited by
      #2

      Hi,

      You don't set any object name on your timer objects so there's no reasons for the function to return anything.

      Calling a signal from an object like that is wrong. The signals are called from within the class implementation.

      The destroyed signal will get called once an object is deleted which you don't do for timerA nor timerB.

      Based on your slots implementation, why do the timers have to be interval since they will only run once anyway ?

      Also, why use static variables outside of your MainWindow class to handle that case ?

      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
      • V Offline
        V Offline
        VRonin
        wrote on 5 Jan 2018, 09:20 last edited by
        #3
        void MainWindow::doThat()
        {
        // private: int m_numDestroyed;
        m_numDestroyed = 0;
        for(QTimer*& singleTim : {timerA ,timerB ,timerC }){
            singleTim = new QTimer(this);
            singleTim ->setInterval(100);
        singleTim->setSingleShot(true);
        }
        const auto checkDestroyed=[=]()->void{
        if(++m_numDestroyed>1){
        delete numDestroyed;
        timerC->start();
        };
            connect(timerA,&QTimer::timeout,this,[=]()->void{
        qDebug("slot A");
        checkDestroyed();
        }
        });
            connect(timerB,&QTimer::timeout,this,[=]()->void{
        qDebug("slot B");
        checkDestroyed();
        }
        });
            connect(timerC,&QTimer::timeout,this,[=]()->void{
        qDebug("slot C");
        }
        });
        connect(timerA,&QTimer::timeout,timerA,&QTimer::deleteLater);
        connect(timerB,&QTimer::timeout,timerB,&QTimer::deleteLater);
        
            timerA->start();
            timerB->start();
        
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0

        1/3

        5 Jan 2018, 07:20

        • Login

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