Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't remove widget from layout



  • Hey,

    so I have this piece of code:

    void VideoSettings::togglePlaylist(int playlist)
    {
        if(playlist == 1)
        {
            if(pSettings == nullptr)
                pSettings = new PlaylistSettings(this);
            ui->playlistLayout->addWidget(pSettings);
        }
        else
        {
            ui->playlistLayout->removeWidget(pSettings);
        }
    }
    

    So, I do manage to make it add the new widget. However, for some reason it doesn't remove it from the layout if playlist is not equal to one (I've made a test with QDebug and it does arrive at that part of the code, so that's not the problem). Anyone knows what could be wrong?



  • This is a highly unusual way to manage this. usually you just insert the widget in the layout and then call setVisible with true or false to show/hide it


  • Moderators

    @El3ctroGh0st said in Can't remove widget from layout:

    for some reason it doesn't remove it from the layout

    on what test is your assumption based?
    I guess you are confused that you still see it in the parent widget?
    After removing the widget from the layout it is still a child of the parent widget (as also stated in the docs)
    So you either need to hide it and/or give it a new parent widget.

    If you want to show the widget again, you do not need to remove it from the layout, but just toggle it's visibility (show()/hide()).


Log in to reply