Important: Please read the Qt Code of Conduct -

How to restore QFrame to its initial/default state

  • I am subclassing a QFrame and I have some data members of my own there.
    Initially, I am customizing the QFrame with some parameters such as:


    And later I want that QFrame to be customized differently, but I want to do that on a clean slate.

    Is there a way to restore a QFrame back to its default state (shape/shadow/background color/line width/etc...)?
    Something along the lines of

    myFrame->setToDefualt(); // sets MyFrame back to its defaults as if it was just now created with the 'new' operator

    of course I could do:

    delete myFrame;
    myFrame = new MyFrame();

    but I'd rather avoid that.

  • Lifetime Qt Champion

    Move the lines where you set your default behavior into an own function and call it later on again when you want to reset to default.

  • @Christian-Ehrlicher those lines are in a class ("FormatterA") that customizes the QFrame, and I have another class ("FormatterB") that will customize it differently (and in the future there might be more "Formatters"), but I don't want FormatterB to assume knowledge on FormatterA, I want FormatterB to just do myFrame->setToDefault() to cancel everything that's been done by FormatterA.

    I can write my own myFrame->setToDefault() and have the Formatters invoke it, but that's prone to misses since another formatter in the future might change an attribute that wasn't initialized in myFrame->setToDefault().
    So I thought maybe there's something like that that's provided by the object that I was missing...

  • @Absurd
    There is no "restore object to default/as it was before FormatterA" call.

    Only FormatterA knows exactly what it did to your object. Either export a method from FormatterA which knows how to reset what it did, or, just possibly, have it save all the original settings somehow in a subclassed QFrame and use those values to restore.

Log in to reply