Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

How can I connect one checkbox and other checkboxs



  • I want to use one checkbox to check other checkboxs. And when one of other checkbox is unchecked, the first checkbox will be partially checked. Also when all of other checkboxs are unchecked, the first checkbox will be unchecked. Now I can change the first checkbox state(tristate) when I check other checkboxs. The problem is when I check the first checkbox, it should control other checkboxs to become checked or unchecked, but I use setcheckstate() to change them one by one , it will send the statechanged(int) signal at the same time.

    //connection
    connect(ui->AllCheck,SIGNAL(clicked()),this,SLOT(Controler(int)));
        connect(ui->NoteCheck,SIGNAL(stateChanged(int)),this,SLOT(Controler(int)),Qt::QueuedConnection);
        connect(ui->TestCheck,SIGNAL(stateChanged(int)),this,SLOT(Controler(int)),Qt::QueuedConnection);
        connect(ui->WarnningCheck,SIGNAL(stateChanged(int)),this,SLOT(Controler(int)),Qt::QueuedConnection);
        connect(ui->OthersCheck,SIGNAL(stateChanged(int)),this,SLOT(Controler(int)),Qt::QueuedConnection);
    ...
    //SLOT
    if(sender()->objectName() == "AllCheck")
        {
            switch (ui->AllCheck->checkState()) {
            case Qt::Unchecked:
                ui->AllCheck->setCheckState(Qt::Checked);
                ui->NoteCheck->setCheckState(Qt::Checked);
    ui->TestCheck->setCheckState(Qt::Checked);
                ui->WarnningCheck->setCheckState(Qt::Checked);
                ui->OthersCheck->setCheckState(Qt::Checked);
                break;
            case Qt::PartiallyChecked:
                break;
    case Qt::Checked:
                ui->AllCheck->setCheckState(Qt::Unchecked);
                ui->NoteCheck->setCheckState(Qt::Unchecked);
                ui->TestCheck->setCheckState(Qt::Unchecked);
                ui->WarnningCheck->setCheckState(Qt::Unchecked);
                ui->OthersCheck->setCheckState(Qt::Unchecked);
                break;
            default:
                break;
            }
        }
    if(ui->TestCheck->checkState() == Qt::Checked && ui->NoteCheck->checkState() == Qt::Checked
        && ui->WarnningCheck->checkState() == Qt::Checked && ui->OthersCheck->checkState() == Qt::Checked)
        {
            ui->AllCheck->setCheckState(Qt::Checked);
        }
        else if(ui->TestCheck->checkState() == Qt::Unchecked && ui->NoteCheck->checkState() == Qt::Unchecked
             && ui->WarnningCheck->checkState() == Qt::Unchecked && ui->OthersCheck->checkState() == Qt::Unchecked)
        {
            ui->AllCheck->setCheckState(Qt::Unchecked);
        }
        else
        {
            ui->AllCheck->setCheckState(Qt::PartiallyChecked);
        }
    

  • Lifetime Qt Champion

    Hi
    maybe you can use
    https://doc.qt.io/qt-5/qsignalblocker.html
    to avoid state change signals while you update the set of checkboxes ?



  • @mrjj wow,it's very useful,thank you so much


Log in to reply