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. Adding widgets dynamically to layout, causes to them to be misplaced
Forum Updated to NodeBB v4.3 + New Features

Adding widgets dynamically to layout, causes to them to be misplaced

Scheduled Pinned Locked Moved Solved General and Desktop
layoutswidgets
28 Posts 4 Posters 17.3k Views 2 Watching
  • 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #15

    Hi

    • my widget is now loaded upon startup

    you mean, after you used promotion, they are constructed with the main window
    or where you promoted them ?
    This is expected and often you can just hide them, and then show if enabled in checkbox.
    If you strictly really need only to construct them on demand, you cant use promotion directly.

    1 Reply Last reply
    1
    • Petross404_Petros SP Offline
      Petross404_Petros SP Offline
      Petross404_Petros S
      wrote on last edited by Petross404_Petros S
      #16

      @mrjj Yes, I think so too. Right now I managed to put the widget on the layout with the use of a vertical spacer (enabled and shown here). After the widget is created and shown, the whole window expands a little so the layout and it's widget can be visible.

      The thing is that I can't "resize" the layout(?) and the whole window to it's original, smaller size. It seems that once the layout is "stretched" to show the widget, it stays that way when the widget is deleted. This leaves an ugly void space where the widget was placed.

      mrjjM 1 Reply Last reply
      0
      • Petross404_Petros SP Petross404_Petros S

        @mrjj Yes, I think so too. Right now I managed to put the widget on the layout with the use of a vertical spacer (enabled and shown here). After the widget is created and shown, the whole window expands a little so the layout and it's widget can be visible.

        The thing is that I can't "resize" the layout(?) and the whole window to it's original, smaller size. It seems that once the layout is "stretched" to show the widget, it stays that way when the widget is deleted. This leaves an ugly void space where the widget was placed.

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

        @Petross404_Petros-S
        Hi
        Normally layouts will "contract". I have yet to experience
        one that leaves a hole if i remove widget. Maybe boxlayout.

        Petross404_Petros SP 1 Reply Last reply
        0
        • mrjjM mrjj

          @Petross404_Petros-S
          Hi
          Normally layouts will "contract". I have yet to experience
          one that leaves a hole if i remove widget. Maybe boxlayout.

          Petross404_Petros SP Offline
          Petross404_Petros SP Offline
          Petross404_Petros S
          wrote on last edited by Petross404_Petros S
          #18

          @mrjj

          Maybe boxlayout.

          You mean I should try boxlayout? Designer doesn't have one though...

          @Global-Moderators Please, correct my title. I meant to write "...causes them to be misplaced".

          mrjjM 1 Reply Last reply
          0
          • Petross404_Petros SP Petross404_Petros S

            @mrjj

            Maybe boxlayout.

            You mean I should try boxlayout? Designer doesn't have one though...

            @Global-Moderators Please, correct my title. I meant to write "...causes them to be misplaced".

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

            @Petross404_Petros-S
            no, no i mean maybe box layout would / might/perhaps leave a hole but all other layouts would
            contract if you remove a widget.

            There is something else wrong.
            Do you use TakeAt to remove from layout ?
            https://doc.qt.io/qt-5.10/qboxlayout.html#takeAt
            Its the only valid way to remove a widget.

            Petross404_Petros SP 1 Reply Last reply
            0
            • mrjjM mrjj

              @Petross404_Petros-S
              no, no i mean maybe box layout would / might/perhaps leave a hole but all other layouts would
              contract if you remove a widget.

              There is something else wrong.
              Do you use TakeAt to remove from layout ?
              https://doc.qt.io/qt-5.10/qboxlayout.html#takeAt
              Its the only valid way to remove a widget.

              Petross404_Petros SP Offline
              Petross404_Petros SP Offline
              Petross404_Petros S
              wrote on last edited by Petross404_Petros S
              #20

              Well, one thing I know is that the custom widget's size constraints had to set to a minimum. It used quite a lot of space so it was centered on top of another layout/widget. Now it's loaded where it should, but after it's deletion there still is a void space.

              That's the code:

              void QtDice::enableUserWidget()
              {
              	userwidget = new UserWidget(this);
              	m_ui->gridLayout_Widget->addWidget(userwidget);
              }
              
              void QtDice::deleteUserWidget()
              {
              	m_ui->gridLayout_Widget->removeWidget(userwidget);
              	userwidget->setParent(nullptr);
              	delete userwidget;
              }
              

              Other suspects could be the main form (i.e. the QtDice qmainwindow) and it's size policies or the vertical spacer. I tried almost every combination and I am out of ideas.

              Do you use TakeAt to remove from layout ?

              Ebook Mastering Qt5, suggested the QLayout::removeWidget, so...

              mrjjM 1 Reply Last reply
              0
              • Petross404_Petros SP Petross404_Petros S

                Well, one thing I know is that the custom widget's size constraints had to set to a minimum. It used quite a lot of space so it was centered on top of another layout/widget. Now it's loaded where it should, but after it's deletion there still is a void space.

                That's the code:

                void QtDice::enableUserWidget()
                {
                	userwidget = new UserWidget(this);
                	m_ui->gridLayout_Widget->addWidget(userwidget);
                }
                
                void QtDice::deleteUserWidget()
                {
                	m_ui->gridLayout_Widget->removeWidget(userwidget);
                	userwidget->setParent(nullptr);
                	delete userwidget;
                }
                

                Other suspects could be the main form (i.e. the QtDice qmainwindow) and it's size policies or the vertical spacer. I tried almost every combination and I am out of ideas.

                Do you use TakeAt to remove from layout ?

                Ebook Mastering Qt5, suggested the QLayout::removeWidget, so...

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

                @Petross404_Petros-S
                Oh, yes, removeWidget seems better in this case and you do change parent.
                Hmm, i would also suspect spacer.
                Sorry i cant spot it. All seems pretty dandy with code shown.

                Petross404_Petros SP 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Petross404_Petros-S
                  Oh, yes, removeWidget seems better in this case and you do change parent.
                  Hmm, i would also suspect spacer.
                  Sorry i cant spot it. All seems pretty dandy with code shown.

                  Petross404_Petros SP Offline
                  Petross404_Petros SP Offline
                  Petross404_Petros S
                  wrote on last edited by
                  #22

                  @mrjj Yeah, it's ok, thank you very much!

                  For the moment I just create unconditionally the userwidget and all that clicking the checkbox does, is to enable or disable it. But it's always visible.

                  It's not what I like, but it gives the functionality I am working on without an ugly ui. Until I find and post what the missing part of this puzzle is, I will leave this as UNSOLVED.

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

                    What about hiding it while the condition to use it are not met ?

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

                    Petross404_Petros SP 1 Reply Last reply
                    0
                    • MCamM Offline
                      MCamM Offline
                      MCam
                      wrote on last edited by
                      #24

                      Why use an empty QVBoxLayout as a placeholder for your userwidget? Why not just simply put your userwidget into the layout of your Mainwindow?

                      I guess you are using another QVBoxLayout as the Layout of your Mainwindow so you could use:

                      m_ui->yourmainlayoutname->insertWidget( 1, userwidget);
                      

                      On a sidenote: You don't need to put your textlabel-placeholder for your dice into a QGridLayout too, just put it into your Mainwindow-Layout and maybe set stretch-factors to your likeing.

                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        What about hiding it while the condition to use it are not met ?

                        Petross404_Petros SP Offline
                        Petross404_Petros SP Offline
                        Petross404_Petros S
                        wrote on last edited by
                        #25

                        @SGaist One reason is that I want to learn about creating custom widgets or working with spacers and layouts.
                        @MCam Aren't the widgets supposed to be -pretty much- always on top of a layout?

                        1 Reply Last reply
                        0
                        • Petross404_Petros SP Offline
                          Petross404_Petros SP Offline
                          Petross404_Petros S
                          wrote on last edited by
                          #26

                          So, at first the window is at it's most minimum size. After enabling the widget it resizes a little, but after closing the widget again, the window doesn't resize back but rather the QLabel that shows the red dice expands.

                          I can't find some way to alter this. I have tried every combination for QLabel about it's SizePolicy but nothing changed.

                          MCamM 1 Reply Last reply
                          0
                          • Petross404_Petros SP Petross404_Petros S

                            So, at first the window is at it's most minimum size. After enabling the widget it resizes a little, but after closing the widget again, the window doesn't resize back but rather the QLabel that shows the red dice expands.

                            I can't find some way to alter this. I have tried every combination for QLabel about it's SizePolicy but nothing changed.

                            MCamM Offline
                            MCamM Offline
                            MCam
                            wrote on last edited by
                            #27

                            @Petross404_Petros-S
                            This is the default behavior of the layout-system here but you can get what you want by readjusting the size of your MainWindow after deleting the userwidget. In your case you have two QWidgets involved: your QtDice-MainWindow and its centralWidget(). So you need to adjust both of them!

                            Try adding

                            centralWidget()->adjustSize();
                            adjustSize();
                            

                            after removing the userwidget.

                            1 Reply Last reply
                            1
                            • Petross404_Petros SP Offline
                              Petross404_Petros SP Offline
                              Petross404_Petros S
                              wrote on last edited by Petross404_Petros S
                              #28

                              @MCam Thank you, I was using plain adjustSize() that's why it didn't resize. Although there is problem that remains even when resizing both layouts :

                              After unmaximising the window, adjustSize gives this result. I will mark the thread as solved because I can make the window to not be able to get maximized, but for completeness shake it would be nice if this result could be avoided.

                              Either way, you saved my day and I thank you very much for that :)

                              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