[SOLVED] Replace widget inside Layouts



  • Hi Guys...

    I'm trying to change a widget by other at run time.

    imagine that we have a Widget with all widgets posibles and its layouts. When my app start, I use the class QUILoader to load a .ui file and load my widget dynamically. After that, If run over all childs of the main widget looking for a QDateEdir, QDateTimeEdit or QTimeEdit, because if i find someone of them, i need to create a QFrame with this edit and a checkbox too.

    My issue is that I need to replace this edit for the new frame but in the same place inside the layaout and i can have any layout because I'm loading the form dynamically.

    any idea ??

    best regards


  • Lifetime Qt Champion

    Hi,

    You can try with the QLayout's replace function. First your replace the widget with a dummy placeholder, create your new QFrame based widget and replace it back.

    Hope it helps



  • thanks my friend... that's is what i'm looking for !!!!

    but now I need to do something else... because I can't invoke the copy constructor of widgets like QDateEdit (for example)... so, each component has it's own properties and I need to copy each one... let me explain better...

    If i find some QDateEdit, QDateTimeEdit or QTimeEdit (this is my real case), I need to replace this item by a frame with one QDateEdit, QDateTimeEdit or QTimeEdit (depending what I found) and a checkbox in a horizontal layout. For that reason, I need to copy all properties defined in each edit to the new one. Copy constructor is not available for widgets objects , so, I can't copy the metaObject where properties are defined...

    how can I do that ??

    regards


  • Lifetime Qt Champion

    You don't need to, build your QFrame based container widget, swap it with widgets you're interested in, then add that widget to the layout of your container



  • Ok my friend.... thank for the answer... With your first answer I solve my problem.. now trying to something else I have some problems, but first I'll like to looking for the answer by my self...

    best regards and thanks



  • How is this done in Qt4.8? The function QLayout::replaceWidget() does not exist there.


  • Qt Champions 2016

    @veiovis
    You can use TakeAt
    http://doc.qt.io/qt-4.8/qlayout.html#takeAt
    To get widget and ownership back.
    Then delete and insert new.

    You cannot copy a QObject ( QWidget) but you can duplicate the properties.
    http://stackoverflow.com/questions/36518686/dump-all-properties-of-a-qobject-derived-object

    Instead of "dumping" simply assign to new object same property.



  • Thank you, but in an abstract qlayout I cannot add an item a specific position, can I? The insertion would only work with a concrete Layout Implementation.


  • Qt Champions 2016

    @veiovis
    Hi, im not sure how you be able to have a concrete abstract layout instance.

    Do you mean that you access the layout via a base pointer ?
    QLayout * and not say QGridLayout * ?

    In such case, you can just use s cast
    QGridLayout *lay=qobject_cast<QGridLayout *>(the_base_pointer);
    if (lay)
    lay->insert(xxx)

    Of Course the "the_base_pointer" should really point to a QGridLayout for this to work.
    ( hence the if () is impotantant)

    So im purely guessing since im not really sure what code u have :)



  • I wanted to have an implementation independent way, which replaces it in-place.
    But I can also make it work with an upcast.
    Thank you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.