From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


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