Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A QCheckBox used by other functions
Qt 6.12 LTS is out! See what's new in the release blog

A QCheckBox used by other functions

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.7k Views 2 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.
  • M Offline
    M Offline
    Maksims
    wrote on last edited by
    #1

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

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Maksims
        wrote on last edited by
        #3

        Do they mean for loop? I don't understand exactly how can I get to the added QCheckBoxes

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Yes with a loop.

          To which type of widget are you adding these check boxes ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Maksims
            wrote on last edited by
            #5

            I use QFormLayout

            M 1 Reply Last reply
            0
            • M Maksims

              I use QFormLayout

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              @Maksims
              Delete the checkBox as soon it was clicked,
              is really what you want to do ?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Maksims
                wrote on last edited by
                #7

                The checkbox must be deleted when button was printed and ChekBox is "Checked"

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Why a QFormLayout if there's label added with the checkbox ?

                  Use itemAt and from there the widget to check its state. You can then remove and delete it.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mpergand
                    wrote on last edited by mpergand
                    #9

                    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);
                    
                    1 Reply Last reply
                    1

                    • Login

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