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

How can I temporarily store QLayout?



  • 0_1532576676181_cap1.png

    "contentWidget" is a widget where contents are rendered. As you can see, initially it has no layout.

    0_1532576783594_cap2.PNG
    This is one of the contents which is rendered in the contentWidget. And it has a layout. (indicated with a red circle)
    This layout is applied to contentWidget everytime a content is rendered in contentWidget.

    My topic is, Every time a content in contentWidget changes, I want to store the content (technically the layout applied to contentWidget which has all widgets in contentWidget as children) temporarily so that it will be just loaded, rather than being created again if I render that content again.
    To achieve it, I must detach the layout from contentWidget lest I get an error message below.

    QLayout: Attempting to add QLayout "" to QWidget "contentWidget", which already has a layout
    

    However, it doesn't work well like I expected.

    delete this->contentWidget->layout()->setParent(nullptr);
    
    QWidget *temp = new QWidget();
    delete this->contentWidget->layout()->setParent(temp);
    

    Those examples make the program crash.
    Why this happens?


  • Lifetime Qt Champion



  • Thanks. It looks more convenient.


Log in to reply