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

How in QComboBox with QCheckBox check every CheckBox?



  • I used this code: [https://gist.github.com/mistic100/c3b7f3eabc65309687153fe3e0a9a720](link url)
    to create QComboBox with QCheckBox.
    I created the widget so:
    ```
    ui->SearchChecComboBox->addCheckItem("text0", 0, Qt::Unchecked);
    ui->SearchCheckComboBox->addCheckItem("text1", 1, Qt::Unchecked);
    ui->SearchCheckComboBox->addCheckItem("text2", 2, Qt::Unchecked);
    ui->SearchCheckComboBox->addCheckItem("text3", 3, Qt::Unchecked);

    How to check every CheckBox?

  • Lifetime Qt Champion

    Hi
    "How to check every CheckBox?"
    You mean how to check each if its checked or
    how to create all as checked from start ?

    Anyway, you can do like

     // grab the model
      QStandardItemModel* model = qobject_cast<QStandardItemModel*> ( ui->combobox->model() );
      if (!model) { // check is important or u can/might crash 
        qDebug() << "not QStandardItemModel !";
        return;
      }
    
    -----
    
      // check all if checked
      for (int row = 0; row < model->rowCount(); ++row) {
        if (model->item(row)->checkState() == (Qt::CheckState::Checked)) {
          qDebug() << "checked";
        }
      }
    
      // set all to checked
      for (int row = 0; row < model->rowCount(); ++row) {
        model->item(row)->setCheckState(Qt::CheckState::Checked);
      }
    
    

Log in to reply