Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Replace widget inside Layouts
QtWS25 Last Chance

[SOLVED] Replace widget inside Layouts

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 14.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • freddy311082F Offline
    freddy311082F Offline
    freddy311082
    wrote on last edited by SGaist
    #1

    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

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • freddy311082F Offline
        freddy311082F Offline
        freddy311082
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • freddy311082F Offline
            freddy311082F Offline
            freddy311082
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • V Offline
              V Offline
              veiovis
              wrote on last edited by
              #6

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

              mrjjM 1 Reply Last reply
              0
              • V veiovis

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @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.

                1 Reply Last reply
                1
                • V Offline
                  V Offline
                  veiovis
                  wrote on last edited by veiovis
                  #8

                  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.

                  mrjjM 1 Reply Last reply
                  0
                  • V veiovis

                    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.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #9

                    @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 :)

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      veiovis
                      wrote on last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved