Congratulations to our 2022 Qt Champions!

[SOLVED] Change the Layout/Content of a QWidget

  • Hey, look at this picture:

    I have two lists with several Elements. Each Element has a .ui File (blue lines) with different content. Each of the .ui files has a QWidget, having a QVBoxLayout, storing the content (e.g. Labels and QImages, etc).

    Now, if I click one of the Elements in the QListView, I would like to load the content of the elements .ui files in to the main widget on the left side (green arrows).

    I can use:
    but this works only one time, the content won't refresh and I always see the content of element0.

    I can use
    but then I can always click only one time each element. After that I get a Segmentation Fault because I have deleted the content of the Element. But it should be possible to click always each element in the two lists and the content should always change.

    Any idea how to achviev that=


  • Hi,

    wouldn't it be easier to just add the widgets on the left side to the layout of the right side widget and alway have only one of the "visible":

    If you don't want top do that for some reason, I would set a fixed QHBoxlayout tot he right side widget. Then I would add an entire elementx widget(s) to that layout of the right side widget. If another elementx widget should be displayed you just "remove": the previously added widget and add the new one.

    Moving layouts between widgets ... I have never done that actually. I don't know whether this is possible...

    Best Soraltan

  • Soraltan you are my hero!

    I was going to get crazy. Almost jumped out of the window while singing "Hit me baby one more time" from Britney Spears.

    At the beginning of my program I load the widget of all elements into the main widget (the widget on the right), hiding all widgets. When clicking on a element I just show it. It works like expected!

    Thanks, thanks, THANKS!

  • Your welcome. Glad I could save your from such a cruel fate ;-)

    Best Soraltan

  • Hi,
    The show/hide is a perfect option! Would have done the same. What is also possible is not to set/clear the layout, but to add/remove the entire widget of the Mainwindow. You get/set it with the setCentralWidget(). That should work as well.

Log in to reply