Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting two signals inside one slot



  • Hi All,

    I'm a bit confused getting two signals inside one slot

    void Program::nextBtn()
    {
        m_currentBtn++;
        if(m_currentBtn > btnGrp->buttons().size())
        {
            m_currentBtn = 0;
        }
    
        emit buttonChanged(m_currentBtn);  // signal 1
    }
    
    void Program::changeValue(int value)
    {
    
        CurPoint1->setY(value);
        CurPoint2->setY(value);
        update();
    
        emit pulseWidthValue(adjust_PW_range(CurPoint1->y())); // signal 2
    
    }
    

    At present , I do connect:

    connect(ui->btn_nextPhase, &QPushButton::clicked, this, &Program::nextBtn);
    connect(this, &Program::buttonChanged, this, &Program::paintBtn);
    

    to get value of buttonChanged signal inside paintBtn slot.

    But now I need value from pulseWidthValue signal as well inside paintBtn slot..

    How do I modify the paintBtn definition to get both the signal values?

    Or is there better workaround to this problem?


  • Lifetime Qt Champion

    Hi
    You can simply add the extra parameter to the signal and slot definitions
    and emit it with the new value
    emit buttonChanged(m_currentBtn, adjust_PW_range(CurPoint1->y()) );
    make sure also change paintBtn so it has this extra paramter.



  • @mrjj Hi, thanks a lot for your feedback.

    I could not find what's the issue with the following scenario that not works:

    Not Working - Run time error: One signal implimentation

    void nextBtn();
    void paintBtn(int id, int pwvalue);
    
    signals:
            void buttonChanged(int id, int pwvalue);
    
    // connections
     connect(ui->btn_nextPhase, &QPushButton::clicked, this, &Program::nextBtn);
     connect(this, &Program::buttonChanged, this, &Program::paintBtn);
    
    void Program::nextBtn()
    {
        m_currentBtn++;
        if(m_currentBtn > btnGrp->buttons().size())
        {
            m_currentBtn = 0;
        }
    
        emit buttonChanged(m_currentBtn, adjust_PW_range(CurPoint1->y()));
    }
    
    
    void Program::paintBtn(int id, int pwvalue)
    {
        ui->label_7->setText(QString::number(pwvalue));
    }
    

    Working scenario: Two signals

    void changeValue(int value );
    void nextBtn(int pwvalue);
    void paintBtn(int id, int pwvalue);
    
    signals:
            void buttonChanged(int id, int pwvalue);
            void pulseWidthValue(int pwvalue);
    
    
     connect(ui->btn_nextPhase, &QPushButton::clicked, this, &ProgramPalmerGrasp::nextBtn);
    
        connect(this, &Program::buttonChanged, this, &Program::paintBtn);
        connect(this, &Program::pulseWidthValue, this, &Program::nextBtn);
    
    
    void Program::changeValue(int value)
    {
    
        CurPoint1->setY(value);
        CurPoint2->setY(value);
        update();
    
        emit pulseWidthValue(adjust_PW_range(CurPoint1->y())); 
    
    }
    
    
    void Program::nextBtn(int pwvalue)
    {
        m_currentBtn++;
        if(m_currentBtn > btnGrp->buttons().size())
        {
            m_currentBtn = 0;
        }
    
         emit buttonChanged(m_currentBtn, pwvalue);
    }
    
    void Program::paintBtn(int id, int pwvalue)
    {
        ui->label_7->setText(QString::number(pwvalue));
    }
    
    


  • @viniltc said in Getting two signals inside one slot:

    Not Working - Run time error: One signal implimentation

    What run time error message?



  • @JonB , It just stops the application. Even the debug terminal crashes, I cant spot where exactly is the issue ..



  • The debug took long time to point at:

    emit buttonChanged(m_currentBtn, adjust_PW_range(CurPoint1->y()));
    

    Throws the following message:

    ed3e64f1-1089-417a-89c6-bb075aa296cd-image.png


  • Lifetime Qt Champion

    Hi
    I would guess on that
    emit buttonChanged(m_currentBtn, adjust_PW_range(CurPoint1->y()));

    When you call this, you have not yet ALLOCATED CurPoint1

    so it crashes.

    Make sure its valid at this point in time.



  • @viniltc
    And further to @mrjj, if you run your app from within the debugger it should break on that exception, and give you a stack trace showing you are executing expressionCurPoint1->y().


Log in to reply