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. QHBoxLayout delete QPushButton

QHBoxLayout delete QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 680 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    Delete QPushButton in QHBoxLayout fail.

    QHBoxLayout *hbox = new QHBoxLayout;
    
    void windowAdded(WId wid){
        Dock *dock = new Dock;
        dock->wid = wid;
        QPushButton *pushButton = new QPushButton(icon, NULL);
        pushButton->setUserData(DOCK, dock);
        hbox->addWidget(pushButton);
    }
    
    void windowRemoved(WId wid){
        for (int i=0; i<hbox->count();i++) {
            QLayoutItem *LI = hbox->layout()->itemAt((i));
            QPushButton *pushButton = qobject_cast<QPushButton*>(LI->widget());
            Dock *dock = (Dock*)(pushButton->userData(DOCK));
            if (dock->wid == wid) {
                delete LI;
            }
        }
    }
    

    https://github.com/sonichy

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      The layout owns it so
      you must use
      https://doc.qt.io/qt-5/qlayout.html#takeAt

      and delete that. ( will also delete its widget as far as i remembers )

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SimonSchroeder
        wrote on last edited by
        #3

        I have to disagree with mrjj. It is true that the layout owns the push button. However, IIRC the push button also knows its parent and will delete itself from it when deleted. So, at least in theory your code should work.

        Nevertheless, I am not sure if you correctly use setUserData/userData. I have never encountered it and the reason for this is that it is not part of the public API. So, the best advice is: Don't use it! Quick googling gave me this: https://stackoverflow.com/questions/8260763/about-using-an-undocumented-class-in-qt Their advice is to use QObject::setProperty instead. You are more likely to get help in this forum if you use only documented features.

        sonichyS 1 Reply Last reply
        0
        • S SimonSchroeder

          I have to disagree with mrjj. It is true that the layout owns the push button. However, IIRC the push button also knows its parent and will delete itself from it when deleted. So, at least in theory your code should work.

          Nevertheless, I am not sure if you correctly use setUserData/userData. I have never encountered it and the reason for this is that it is not part of the public API. So, the best advice is: Don't use it! Quick googling gave me this: https://stackoverflow.com/questions/8260763/about-using-an-undocumented-class-in-qt Their advice is to use QObject::setProperty instead. You are more likely to get help in this forum if you use only documented features.

          sonichyS Offline
          sonichyS Offline
          sonichy
          wrote on last edited by
          #4

          @SimonSchroeder Strange solved by these:
          A layout in a big layout. delete cause overlap.
          Change to:
          A QWidget with the layout in a big layout, delete work fine.

          QList<QPushButton *> list_pushButton = widget_app->findChildren<QPushButton *>();    //QWidget
          for (int i=0; i<list_pushButton.count(); i++) {
              Dock *dock = (Dock*)(list_pushButton.at(i)->userData(DOCK));
                      if (dock->wid == wid) {
                          hbox_app->removeWidget(list_pushButton.at(i));    // QHBoxLayout in QWidget
                          list_pushButton.at(i)->deleteLater();
                      }
          }
          

          https://github.com/sonichy

          1 Reply Last reply
          0

          • Login

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