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. How can I connect one checkbox and other checkboxs
Forum Updated to NodeBB v4.3 + New Features

How can I connect one checkbox and other checkboxs

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.3k Views 1 Watching
  • 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.
  • MihanM Offline
    MihanM Offline
    Mihan
    wrote on last edited by
    #1

    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);
        }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      MihanM 1 Reply Last reply
      2
      • mrjjM mrjj

        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 ?

        MihanM Offline
        MihanM Offline
        Mihan
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

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