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

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.0k 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.
  • P Offline
    P Offline
    Petross404_Petros S
    wrote on 10 Mar 2018, 20:27 last edited by
    #1

    As shown here, when I add my widgets to the main window, they are misplaced.

    void QtDice::enableUserWidget()
    {
    	userwidget = new UserWidget("", this);
    	m_ui->verticalLayout_Widget->addWidget(userwidget);
    	setLayout(m_ui->verticalLayout_Widget);
    }
    

    Can someone give me a hint what should I look for?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Mar 2018, 20:44 last edited by
      #2

      Hi,

      Why are you calling setLayout ? Since you have a designer based UI you likely already have the layout set on something, no ?

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

      P 1 Reply Last reply 10 Mar 2018, 21:19
      1
      • S SGaist
        10 Mar 2018, 20:44

        Hi,

        Why are you calling setLayout ? Since you have a designer based UI you likely already have the layout set on something, no ?

        P Offline
        P Offline
        Petross404_Petros S
        wrote on 10 Mar 2018, 21:19 last edited by
        #3

        @SGaist said in Adding widgets dynamically to layout, causes to them to be misplaced:

        Hi,

        Why are you calling setLayout ? Since you have a designer based UI you likely already have the layout set on something, no ?

        I added it as a last attempt. Nothing changed though, the same misplacement happened before.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Mar 2018, 21:34 last edited by
          #4

          It looks like the label isn't in the vertical layout, is it ?

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

          P 1 Reply Last reply 10 Mar 2018, 21:49
          0
          • S SGaist
            10 Mar 2018, 21:34

            It looks like the label isn't in the vertical layout, is it ?

            P Offline
            P Offline
            Petross404_Petros S
            wrote on 10 Mar 2018, 21:49 last edited by
            #5

            @SGaist It's in a grid layout. Why do you suspect it?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 10 Mar 2018, 22:02 last edited by
              #6

              Since you are adding a widget to a vertical layout and said widgets comes on top of another one then that other one is not in the same layout (or any layout for that matter)

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

              P 1 Reply Last reply 10 Mar 2018, 22:05
              0
              • S SGaist
                10 Mar 2018, 22:02

                Since you are adding a widget to a vertical layout and said widgets comes on top of another one then that other one is not in the same layout (or any layout for that matter)

                P Offline
                P Offline
                Petross404_Petros S
                wrote on 10 Mar 2018, 22:05 last edited by
                #7

                I am thinking that I tried vertical and grid layouts, but not horizontal. Before I try this next morning, is there something I am missing with spacers?

                I also could not solve this way.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 10 Mar 2018, 22:13 last edited by
                  #8

                  Where exactly is that widget supposed to come ? And can there be more than one ?

                  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
                  • P Offline
                    P Offline
                    Petross404_Petros S
                    wrote on 11 Mar 2018, 15:56 last edited by
                    #9

                    In the third picture of the link, there is a layout selected which is called verticalLayout_Widgets. The widget is supposed to be placed there and only once but that is something I took care with some code.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 11 Mar 2018, 21:14 last edited by
                      #10

                      Why not in designer using the promotion feature ?

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

                      P 1 Reply Last reply 12 Mar 2018, 13:27
                      2
                      • S SGaist
                        11 Mar 2018, 21:14

                        Why not in designer using the promotion feature ?

                        P Offline
                        P Offline
                        Petross404_Petros S
                        wrote on 12 Mar 2018, 13:27 last edited by
                        #11

                        @SGaist What is the promotion feature?

                        mrjjM 1 Reply Last reply 12 Mar 2018, 14:27
                        0
                        • P Petross404_Petros S
                          12 Mar 2018, 13:27

                          @SGaist What is the promotion feature?

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 12 Mar 2018, 14:27 last edited by mrjj 3 Dec 2018, 14:28
                          #12

                          @Petross404_Petros-S
                          Hi
                          Promotion feature is a super replace at runtime.
                          During design, you say this QWidget should become this type when run.
                          Designer will then use the actual type so when app is run, your control is
                          used and not the plain widget.
                          http://doc.qt.io/qt-5/designer-using-custom-widgets.html

                          This allows light integration where you can desgin with your custom widget but do not have to make a full
                          plugin for integration.

                          P 1 Reply Last reply 12 Mar 2018, 21:17
                          4
                          • mrjjM mrjj
                            12 Mar 2018, 14:27

                            @Petross404_Petros-S
                            Hi
                            Promotion feature is a super replace at runtime.
                            During design, you say this QWidget should become this type when run.
                            Designer will then use the actual type so when app is run, your control is
                            used and not the plain widget.
                            http://doc.qt.io/qt-5/designer-using-custom-widgets.html

                            This allows light integration where you can desgin with your custom widget but do not have to make a full
                            plugin for integration.

                            P Offline
                            P Offline
                            Petross404_Petros S
                            wrote on 12 Mar 2018, 21:17 last edited by
                            #13

                            @mrjj I will read about this. I didn't know this thing even existed.

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              Petross404_Petros S
                              wrote on 14 Mar 2018, 12:25 last edited by
                              #14

                              What happens it that my widget is now loaded upon startup, while I need it to be loaded if and when the use clicks a checkbox. I guess I wan't clear about the term dynamic loading...

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 14 Mar 2018, 13:54 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
                                • P Offline
                                  P Offline
                                  Petross404_Petros S
                                  wrote on 14 Mar 2018, 16:08 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 14 Mar 2018, 16:18
                                  0
                                  • P Petross404_Petros S
                                    14 Mar 2018, 16:08

                                    @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 14 Mar 2018, 16:18 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.

                                    P 1 Reply Last reply 14 Mar 2018, 16:20
                                    0
                                    • mrjjM mrjj
                                      14 Mar 2018, 16:18

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

                                      P Offline
                                      P Offline
                                      Petross404_Petros S
                                      wrote on 14 Mar 2018, 16:20 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 14 Mar 2018, 17:46
                                      0
                                      • P Petross404_Petros S
                                        14 Mar 2018, 16:20

                                        @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 14 Mar 2018, 17:46 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.

                                        P 1 Reply Last reply 14 Mar 2018, 17:57
                                        0
                                        • mrjjM mrjj
                                          14 Mar 2018, 17:46

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

                                          P Offline
                                          P Offline
                                          Petross404_Petros S
                                          wrote on 14 Mar 2018, 17:57 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 14 Mar 2018, 18:04
                                          0

                                          1/28

                                          10 Mar 2018, 20:27

                                          • Login

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