Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting QPushButtons?
Forum Updated to NodeBB v4.3 + New Features

Deleting QPushButtons?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.2k Views
  • 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
    meepo1
    wrote on last edited by meepo1
    #1

    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();
        }
    }
    
    jsulmJ 1 Reply Last reply
    0
    • M meepo1

      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();
          }
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @meepo1 said in Deleting QPushButtons?:

      I cannot delete all of them

      Why not? How did you try to delete them?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • jsulmJ jsulm

        @meepo1 said in Deleting QPushButtons?:

        I cannot delete all of them

        Why not? How did you try to delete them?

        M Offline
        M Offline
        meepo1
        wrote on last edited by
        #3

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

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • M meepo1

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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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();
          }
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • M meepo1

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

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @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); 
                }
            }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            5

            • Login

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