简体   繁体   中英

Connect two signals to one slot

I have a slot named paintButton . There is swtich condition within the slot as shown below:

In each case I need to recive a signal named newValue

void Program::paintButton(int id)
{

    switch(id)
    {
    case 1 :

       // recive  signal: "newValue" here
        int  portNo = 1;
        setValue(portNo, newValue);

        ui->btn1->setText("1");
        ui->btn2->setText("");
        ui->btn3->setText("");
        break;

    case 2 :

       // recive  signal: "newValue" here
        int  portNo = 2;
        setValue(portNo, newValue);

         ui->btn2->setText("2");
         ui->btn1->setText("");
         ui->btn3->setText("");
        break;

    case 3 :

      // recive  signal: "newValue" here
        int  portNo = 3;
        setValue(portNo, newValue);

        ui->btn3->setText("3");
        ui->btn1->setText("");
        ui->btn2->setText("");
        break;

    }

}

How do I modify the paintButton slot inorder to receive both the signals ( id and newValue ). Or is there a better way to do this?

Thanks in advance.

Does someone already emit mysignal(id,value) ? If so, use a specific method for that. I'm going to re-use your overloaded name to show:

void Program::paintButton(int id, int value)
{
     setValue(id, value);
     paintButton(id); 
}

void Program::paintButton(int id)
{
    switch(id)
    {
    case 1 :
        ui->btn1->setText("1");
        ui->btn2->setText("");
        ui->btn3->setText("");
        break;
    case 2 :
         ui->btn2->setText("2");
         ui->btn1->setText("");
         ui->btn3->setText("");
        break;
    case 3 :
        ui->btn3->setText("3");
        ui->btn1->setText("");
        ui->btn2->setText("");
        break;
    }
}

You can't wait for a signal in the middle of your code. If you need to respond to the second of two different signals, you might want to attach each signal to its own slot:

void Program::slotId(int id)
{
    if (m_dopaint)
    {
        m_dopaint = false;
        paintButton(id, m_value)
    }
    else
    {
        m_id = id;
        m_dopaint = true;
    }
}

void Program::slotValue(int value)
{
    if (m_dopaint)
    {
        m_dopaint = false;
        paintButton(m_id, value)
    }
    else
    {
        m_value = value;
        m_dopaint = true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM