Deleting QPushButtons?



  • I've created dynamically several QPushbutton using "Button1", however, I cannot delete all of them using another QPushButton. How Can I do it?

    void Window1::db(){
        for(int i = 0; i < DefN.size(); i++){
             QPushButton *bu = new QPushButton(this);
             if(DefN[i].indexOf(" ") != -1){
                 nom = DefN[i].replace(DefN[i].indexOf(" "),1,"\n");
                 bu->setText(nom+"\n"+DefS[i]);
             }else{
                 bu->setText(DefN[i]+" "+"\n"+DefS[i]);
             }           
             bu->setParent(this);
             bu->setMaximumWidth(121);
             bu->setMinimumWidth(121);
             bu->move(140+120*val,140*val);
             bu->show();
        }
    }
    

  • Moderators

    @meepo1 said in Deleting QPushButtons?:

    I cannot delete all of them

    Why not? How did you try to delete them?



  • @jsulm I dont know how to detect all those Pushbutton and delete them. Well , in summary i think i have no idea.


  • Moderators

    @meepo1 Well, you can put the pointers in a list when you create them, then just iterate over that list and call dleteLater() (buttons is a QList<QPushButton*>):

    void Window1::db(){
        for(int i = 0; i < DefN.size(); i++){
             QPushButton *bu = new QPushButton(this);
             if(DefN[i].indexOf(" ") != -1){
                 nom = DefN[i].replace(DefN[i].indexOf(" "),1,"\n");
                 bu->setText(nom+"\n"+DefS[i]);
             }else{
                 bu->setText(DefN[i]+" "+"\n"+DefS[i]);
             }           
             bu->setParent(this);
             bu->setMaximumWidth(121);
             bu->setMinimumWidth(121);
             bu->move(140+120*val,140*val);
             buttons.append(bu);
             bu->show();
        }
    }
    
    void Window1::deleteButtons()
    {
        for (QPushButton *bu : buttons)
            bu->deleteLater();
        bu.clear();
    }
    


  • @meepo1 alternatively to what @jsulm said, you can take the signal of the Deletebutton and connect that to delete slot of the other buttons.

    
    
    //under the assumption your button to delete all buttons is named "deleteButton"
    
    void Window1::db(){
        for(int i = 0; i < DefN.size(); i++){
             QPushButton *bu = new QPushButton(this);
             if(DefN[i].indexOf(" ") != -1){
                 nom = DefN[i].replace(DefN[i].indexOf(" "),1,"\n");
                 bu->setText(nom+"\n"+DefS[i]);
             }else{
                 bu->setText(DefN[i]+" "+"\n"+DefS[i]);
             }           
             bu->setParent(this);
             bu->setMaximumWidth(121);
             bu->setMinimumWidth(121);
             bu->move(140+120*val,140*val);
             bu->show();
    
             connect(deleteButton, &QPushButton::clicked, bu, &QPushButton::deleteLater); 
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.