Unable to delete the widgets in sub-layout of a layout in Qt

前端 未结 3 1086
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 04:45

I am using Qt 5.5.0 for Windows. In a dialog using for login as well as register, I use a QVBoxLayout as the main layout of the dialog and add a QGridLayout to the mainLayout. W

相关标签:
3条回答
  • 2021-01-26 05:17

    You should use one of the setHidden( bool ), setVisible( bool ) functions. If you just use the removeWidget function which you did then you only remove it from the layout.

    0 讨论(0)
  • 2021-01-26 05:37

    You are removing just from layout, not the parent widget, so they are shown in the widget not "layouted".

    Try simply hiding them with setVisible(false), and setVisible(true) to show them again.

    void LoginDialog::showRegister()
    {
        ...
        useremailLabel->setVisible(true);
        ...
    }
    
    void LoginDialog::hideRegister()
    {
        ...
        useremailLabel->setVisible(false);
        ...
    }
    
    0 讨论(0)
  • 2021-01-26 05:38

    If you truly want to get rid of the widgets, you should destruct them. If they were allocated on the heap, you should simply delete them: this deallocates their memory after destructing them.

    Qt keeps track of widget lifetime and a widget being destructed automatically removes itself from its layout, and removes itself from its parent.

    0 讨论(0)
提交回复
热议问题