void MainWindow::on_pushButton_clicked(){ for(int i = 0; i < 3; i++) { box = new QCheckBox(this); ui ->verticalLayout->addWidget(box); // Добавление box->setChecked(true); // Устанавливаем галочку по умолчанию connect(box, SIGNAL(clicked()), this, SLOT(on_pushButton2_clicked())); connect(box, SIGNAL(clicked()), this, SLOT(on_pushButton3_clicked())); }} void MainWindow::on_pushButton2_clicked(){ for(int i = 0; i < 3; i++) { ui->verticalLayout->removeWidget(box); }} void MainWindow::on_pushButton3_clicked(){ for(int i = 0; i < 3; i++) { box = new QCheckBox(this); ui ->verticalLayout->addWidget(box); // Добавление box->setChecked(false); // Устанавливаем галочку по умолчанию } }
void MainWindow::on_pushButton_clicked(){ for(int i =0 ; i < 3; i++) { box1 = new QCheckBox; ui ->verticalLayout->addWidget(box1); box1 ->setChecked(true); connect(box1, SIGNAL(clicked()), this, SLOT(on_pushButton2_clicked())); }}void MainWindow::on_pushButton2_clicked(){ for(QCheckBox*box : findChildren<QCheckBox*>()) { box->setChecked(false); connect(box, SIGNAL(clicked()), this, SLOT(on_pushButton3_clicked())); }}void MainWindow::on_pushButton3_clicked(){ for(QCheckBox*box : findChildren<QCheckBox*>()) { box->setChecked(true); connect(box, SIGNAL(clicked()), this, SLOT(on_pushButton2_clicked())); }}
#include "mainwindow.h"#include <QCheckBox>#include <QHBoxLayout>#define COUNT_BOX 3 //5, 10, .... 300MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ QWidget *w = new QWidget; setCentralWidget(w); QHBoxLayout *l = new QHBoxLayout; w->setLayout(l); QCheckBox *listBox[COUNT_BOX] for(QCheckBox*box : listBox ) l->addWidget(box = new QCheckBox); for(QCheckBox*box : findChildren<QCheckBox*>()) //или for(QCheckBox *box : listBox ) { for(QCheckBox*box1 : findChildren<QCheckBox*>()) //или for(QCheckBox *box1 : listBox ) { if(box != box1) connect(box, SIGNAL(clicked(bool)), box1, SLOT(setChecked(bool))); } }}