Qt 6.12 LTS is out! See what's new in the release
blog
A QCheckBox used by other functions
-
I make a To Do list, so far I have 2 function, the 1 function adds a QCheckBox at click and the 2 function must delete the QCheckBox which are "Checked" but I don't know how to do that.
menuwindow.cpp:
this funktion must delete the QCheckBox
void MenuWindow::on_alle_loschen_2_clicked() { if(aufgabe->isChecked()){ aufgabe->hide(); } }this funktion add the new QCheckBox
void MenuWindow::on_einfugen_clicked() { QCheckBox *aufgabe = new QCheckBox(ui->aufgabe_einfugen->text()); ui->aufgaben_liste->addWidget(aufgabe); }menuwindow.h
private slots: void on_alle_loschen_2_clicked(); void on_einfugen_clicked(); private: QCheckBox *aufgabe; Ui::MenuWindow *ui; -
Hi and welcome to devnet,
If you want to remove all the check boxes that are checked you need to iterate through all the ones that you added to aufgaben_liste.
What you have here is a dangling pointer that will make your application crash when calling any function on it.
-
Yes with a loop.
To which type of widget are you adding these check boxes ?
-
Code that delete all checked checkbox from a formlayout at ui->formLayout:
QList<QCheckBox*> boxes; for(int i=0; i<ui->formLayout->rowCount(); i++) { QCheckBox* box=qobject_cast<QCheckBox*>(ui->formLayout->itemAt(i,QFormLayout::FieldRole)->widget()); if(box && box->isChecked()) boxes<<box; } for(QWidget* w : boxes) ui->formLayout->removeRow(w);