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. QPushButton->layout() returns 0

QPushButton->layout() returns 0

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.9k 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.
  • C Offline
    C Offline
    ChajusSaib
    wrote on last edited by
    #1

    Hello there,

    so I have a QPushButton in a QHBoxLayout that also contains a QLineEdit. The QHBoxLayout is in a QVBoxLayout. I would like to remove the QHBoxLayout and everything in it(QPushButton and QLineEdit) when I click the QPushButton.

    So I have the button connected to a slot that should do the work that looks like this

    void ClassName::removeLine()
    {
        QPushButton *button = qobject_cast<QPushButton *>(sender()); // get the caller object(QPushButton)
        if (button)
        {
            disconnect(button, SIGNAL(clicked(bool)), this, SLOT(removeLine()));
    
            QLayout *layout = button->layout();
            if (layout)
            {
                QLayoutItem * item;
                while ((item = layout->takeAt(0)) != 0)
                {
                    item->widget()->hide();
                    delete item->widget();
                    delete item;
                }
    
                delete layout;
            }
        }
    }
    

    but button->layout() returns 0. I believe I'm misunderstanding the ::layout() function.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      I believe I'm misunderstanding the ::layout() function

      Yup, unfortunately. layout() returns the layout of the widget, not the layout it is placed in. So since there are no layouts "inside" a button its layout() method returns null. There's no direct way to tell what layout is managing a given widget i.e. there's no way to go "up" in the hierarchy of layouts. For that you need to get a parent widget (via parentWidget() method) and look for the layout you are interested in the parent layout's items.

      For this sort of task it's easier to put the button and line edit in a dummy "row" QWidget parent, instead of nesting layouts directly. This way you could just do button->parentWidget()->deleteLater() and be done with it.

      C 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        I believe I'm misunderstanding the ::layout() function

        Yup, unfortunately. layout() returns the layout of the widget, not the layout it is placed in. So since there are no layouts "inside" a button its layout() method returns null. There's no direct way to tell what layout is managing a given widget i.e. there's no way to go "up" in the hierarchy of layouts. For that you need to get a parent widget (via parentWidget() method) and look for the layout you are interested in the parent layout's items.

        For this sort of task it's easier to put the button and line edit in a dummy "row" QWidget parent, instead of nesting layouts directly. This way you could just do button->parentWidget()->deleteLater() and be done with it.

        C Offline
        C Offline
        ChajusSaib
        wrote on last edited by
        #3

        @Chris-Kawa
        Thank you mate, that did the job.

        ...
        QWidget *row = new QWidget();
        QLineEdit *line = new QLineEdit(row);
        QPushButton *removeButton = new QPushButton("x", row);
        QHBoxLayout *rowLayout = new QHBoxLayout(row);
        ...
        row->setLayout(rowLayout);
        ...
        
        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