Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QWidget modification at runtime
Qt 6.12 LTS is out! See what's new in the release blog

QWidget modification at runtime

Scheduled Pinned Locked Moved Unsolved Qt for Python
19 Posts 5 Posters 1.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.
  • SGaistS SGaist

    Hi,

    Since you have a QMainWindow, is your layout applied to the central widget of your QMainWindow ? Or did you try to change the QMainWindow's own layout which is not possible.

    S Offline
    S Offline
    SamuraiDestroy
    wrote on last edited by
    #3

    @SGaist The layout is applied to the central widget and then all the ui is contained within the layout like this
    c19edefe-ed5c-4e4a-8fbc-de84eeeba52e-image.png

    JonBJ 1 Reply Last reply
    0
    • S SamuraiDestroy

      @SGaist The layout is applied to the central widget and then all the ui is contained within the layout like this
      c19edefe-ed5c-4e4a-8fbc-de84eeeba52e-image.png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @SamuraiDestroy
      The "red no entry signs" (you must ignore the one on centralWidget I think) indicate you have sub-widgets which do not themselves have any layout, and that may be the cause. You need to eliminate any red no entry signs you see in Designer, except I think on centralWidget which it handles weirdly. If those widgets have child widgets you can click on the parent widget and set layout from there.

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @SamuraiDestroy
        The "red no entry signs" (you must ignore the one on centralWidget I think) indicate you have sub-widgets which do not themselves have any layout, and that may be the cause. You need to eliminate any red no entry signs you see in Designer, except I think on centralWidget which it handles weirdly. If those widgets have child widgets you can click on the parent widget and set layout from there.

        S Offline
        S Offline
        SamuraiDestroy
        wrote on last edited by
        #5

        @JonB It looks like almost all my widgets have the red signs, despite the fact that have given them all layout alignments and size policies, so I'm not really sure what to do from here
        3f667850-83fb-423b-9296-0c0fb7fb948b-image.png

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on last edited by
          #6

          You have not given the widgets a layout. Instead, you have placed layouts inside the widgets. You have to right click on the widget in this list view and select the appropriate layout for the widget.

          1 Reply Last reply
          1
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #7

            As I wrote earlier:

            If those widgets have child widgets you can click on the parent widget and set layout from there.

            (I meant right-click on the parent and there is an option like Set Layout >. Irritatingly I find this is only available on a parent as and when it has at least one child widget, something I commented on as being "non-intuitive" a decade ago :) )

            SGaistS 1 Reply Last reply
            1
            • JonBJ JonB

              As I wrote earlier:

              If those widgets have child widgets you can click on the parent widget and set layout from there.

              (I meant right-click on the parent and there is an option like Set Layout >. Irritatingly I find this is only available on a parent as and when it has at least one child widget, something I commented on as being "non-intuitive" a decade ago :) )

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @JonB said in QWidget modification at runtime:

              As I wrote earlier:

              If those widgets have child widgets you can click on the parent widget and set layout from there.

              (I meant right-click on the parent and there is an option like Set Layout >. Irritatingly I find this is only available on a parent as and when it has at least one child widget, something I commented on as being "non-intuitive" a decade ago :) )

              That's why I always found writing everything by hand so much more intuitive ^^ (disclaimer: I have nothing against designer, just not my cup of tea).

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

              S 1 Reply Last reply
              2
              • SGaistS SGaist

                @JonB said in QWidget modification at runtime:

                As I wrote earlier:

                If those widgets have child widgets you can click on the parent widget and set layout from there.

                (I meant right-click on the parent and there is an option like Set Layout >. Irritatingly I find this is only available on a parent as and when it has at least one child widget, something I commented on as being "non-intuitive" a decade ago :) )

                That's why I always found writing everything by hand so much more intuitive ^^ (disclaimer: I have nothing against designer, just not my cup of tea).

                S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #9

                @SGaist said in QWidget modification at runtime:

                That's why I always found writing everything by hand so much more intuitive

                Totally agree. Most annoyingly, the tab order is never right when using the designer. This is especially the case when changes things in the future, like adding new widgets. You have to explicitly set the tab order. When I write the code instead of a .ui file, it is usually correct. Our software has so many dialogs that are unusable without a mouse. (I just don't like jumping between mouse and keyboard.)

                @JonB said in QWidget modification at runtime:

                Irritatingly I find this is only available on a parent as and when it has at least one child widget

                Not just that. Have you ever tried to add a widget into an existing layout? There are a few corner cases where it is really hard to add a widget at the right spot inside an existing layout in the designer. Sometime I just break up the layout first to get new widgets in. However, the other widgets might be screwed up to a point where reapplying the layout still messes things up. And then you have to know to sometime simplify the layout to get rid of empty slots.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SamuraiDestroy
                  wrote on last edited by SamuraiDestroy
                  #10

                  I have now correctly applied layouts to all my widgets, but the ui still does not correctly resize, however it does definitely change.
                  Here is how it looks once the program starts:
                  image.png
                  And here is how it looks when the refresh function is called manually afterwards:
                  image.png
                  (the big box in the middle of this image is the same as the small one in the middle of the first image, for some reason part of it is obstructed)

                  I checked and the alignments and size policies are still set how I think they should be
                  I also realised I wasn't resizing the central widget and that might be part of the issue but now that I am resizing it it's behaviour is unchanged

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

                    I also realised I wasn't resizing the central widget and that might be part of the issue but now that I am resizing it it's behaviour is unchanged

                    The central widget should automatically resize when you resize the QMainWindow, if it does not then it has not been properly set as central widget or something else strange is at play..

                    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
                    • S SamuraiDestroy

                      I have now correctly applied layouts to all my widgets, but the ui still does not correctly resize, however it does definitely change.
                      Here is how it looks once the program starts:
                      image.png
                      And here is how it looks when the refresh function is called manually afterwards:
                      image.png
                      (the big box in the middle of this image is the same as the small one in the middle of the first image, for some reason part of it is obstructed)

                      I checked and the alignments and size policies are still set how I think they should be
                      I also realised I wasn't resizing the central widget and that might be part of the issue but now that I am resizing it it's behaviour is unchanged

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #12

                      @SamuraiDestroy
                      Qt widget layouts is a dark art. Everything you do in Designer is saved in (fairly) readable XML in the generated .ui file, nothing else. You can look at that to try to figure what is going on, and if you are really stuck you can paste it here and people can tell you what you need so long as they understand what you are trying to achieve.

                      S 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @SamuraiDestroy
                        Qt widget layouts is a dark art. Everything you do in Designer is saved in (fairly) readable XML in the generated .ui file, nothing else. You can look at that to try to figure what is going on, and if you are really stuck you can paste it here and people can tell you what you need so long as they understand what you are trying to achieve.

                        S Offline
                        S Offline
                        SamuraiDestroy
                        wrote last edited by
                        #13

                        @JonB Sorry for the late response, I was super busy. Here is the block of XML for the UI:

                        <widget class="QWidget" name="centralwidget">
                           <widget class="QWidget" name="horizontalLayoutWidget">
                            <property name="geometry">
                             <rect>
                              <x>0</x>
                              <y>0</y>
                              <width>670</width>
                              <height>802</height>
                             </rect>
                            </property>
                            <layout class="QHBoxLayout" name="horizontalLayout">
                             <property name="spacing">
                              <number>0</number>
                             </property>
                             <property name="sizeConstraint">
                              <enum>QLayout::SetNoConstraint</enum>
                             </property>
                             <item alignment="Qt::AlignLeft|Qt::AlignVCenter">
                              <widget class="QFrame" name="toolList">
                               <property name="sizePolicy">
                                <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
                                 <horstretch>0</horstretch>
                                 <verstretch>0</verstretch>
                                </sizepolicy>
                               </property>
                               <property name="minimumSize">
                                <size>
                                 <width>250</width>
                                 <height>800</height>
                                </size>
                               </property>
                               <property name="sizeIncrement">
                                <size>
                                 <width>6</width>
                                 <height>16</height>
                                </size>
                               </property>
                               <property name="frameShape">
                                <enum>QFrame::StyledPanel</enum>
                               </property>
                               <property name="frameShadow">
                                <enum>QFrame::Raised</enum>
                               </property>
                               <layout class="QHBoxLayout" name="horizontalLayout_4">
                                <item>
                                 <layout class="QGridLayout" name="gridLayout">
                                  <property name="spacing">
                                   <number>0</number>
                                  </property>
                                 </layout>
                                </item>
                                <item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
                                 <widget class="QScrollArea" name="scrollArea">
                                  <property name="widgetResizable">
                                   <bool>true</bool>
                                  </property>
                                  <widget class="QWidget" name="scrollAreaWidgetContents">
                                   <property name="geometry">
                                    <rect>
                                     <x>0</x>
                                     <y>0</y>
                                     <width>68</width>
                                     <height>68</height>
                                    </rect>
                                   </property>
                                  </widget>
                                 </widget>
                                </item>
                               </layout>
                              </widget>
                             </item>
                             <item alignment="Qt::AlignHCenter|Qt::AlignVCenter">
                              <widget class="QFrame" name="imageViewer">
                               <property name="sizePolicy">
                                <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
                                 <horstretch>1</horstretch>
                                 <verstretch>1</verstretch>
                                </sizepolicy>
                               </property>
                               <property name="minimumSize">
                                <size>
                                 <width>100</width>
                                 <height>100</height>
                                </size>
                               </property>
                               <property name="frameShape">
                                <enum>QFrame::StyledPanel</enum>
                               </property>
                               <property name="frameShadow">
                                <enum>QFrame::Plain</enum>
                               </property>
                              </widget>
                             </item>
                             <item alignment="Qt::AlignRight|Qt::AlignVCenter">
                              <widget class="QFrame" name="layerList">
                               <property name="sizePolicy">
                                <sizepolicy hsizetype="Preferred" vsizetype="Expanding">
                                 <horstretch>1</horstretch>
                                 <verstretch>1</verstretch>
                                </sizepolicy>
                               </property>
                               <property name="minimumSize">
                                <size>
                                 <width>200</width>
                                 <height>800</height>
                                </size>
                               </property>
                               <property name="maximumSize">
                                <size>
                                 <width>2000</width>
                                 <height>8000</height>
                                </size>
                               </property>
                               <property name="sizeIncrement">
                                <size>
                                 <width>1</width>
                                 <height>4</height>
                                </size>
                               </property>
                               <property name="toolTip">
                                <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
                               </property>
                               <property name="frameShape">
                                <enum>QFrame::StyledPanel</enum>
                               </property>
                               <property name="frameShadow">
                                <enum>QFrame::Raised</enum>
                               </property>
                               <property name="lineWidth">
                                <number>3</number>
                               </property>
                               <property name="midLineWidth">
                                <number>1</number>
                               </property>
                               <layout class="QHBoxLayout" name="horizontalLayout_3">
                                <item alignment="Qt::AlignHCenter|Qt::AlignTop">
                                 <widget class="QFrame" name="frame">
                                  <property name="sizePolicy">
                                   <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
                                    <horstretch>0</horstretch>
                                    <verstretch>0</verstretch>
                                   </sizepolicy>
                                  </property>
                                  <property name="frameShape">
                                   <enum>QFrame::StyledPanel</enum>
                                  </property>
                                  <property name="frameShadow">
                                   <enum>QFrame::Raised</enum>
                                  </property>
                                  <layout class="QVBoxLayout" name="verticalLayout">
                                   <item>
                                    <layout class="QHBoxLayout" name="horizontalLayout_2">
                                     <property name="spacing">
                                      <number>0</number>
                                     </property>
                                     <item>
                                      <widget class="QToolButton" name="toolButton_add">
                                       <property name="sizePolicy">
                                        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
                                         <horstretch>0</horstretch>
                                         <verstretch>0</verstretch>
                                        </sizepolicy>
                                       </property>
                                       <property name="sizeIncrement">
                                        <size>
                                         <width>1</width>
                                         <height>1</height>
                                        </size>
                                       </property>
                                       <property name="text">
                                        <string>...</string>
                                       </property>
                                       <property name="icon">
                                        <iconset resource="../../resources.qrc">
                                         <normaloff>:/resources/plus.png</normaloff>:/resources/plus.png</iconset>
                                       </property>
                                       <property name="iconSize">
                                        <size>
                                         <width>32</width>
                                         <height>32</height>
                                        </size>
                                       </property>
                                       <property name="popupMode">
                                        <enum>QToolButton::InstantPopup</enum>
                                       </property>
                                      </widget>
                                     </item>
                                     <item>
                                      <widget class="QToolButton" name="toolButton_dup">
                                       <property name="sizePolicy">
                                        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
                                         <horstretch>0</horstretch>
                                         <verstretch>0</verstretch>
                                        </sizepolicy>
                                       </property>
                                       <property name="sizeIncrement">
                                        <size>
                                         <width>1</width>
                                         <height>1</height>
                                        </size>
                                       </property>
                                       <property name="text">
                                        <string>...</string>
                                       </property>
                                       <property name="icon">
                                        <iconset resource="../../resources.qrc">
                                         <normaloff>:/resources/dup.png</normaloff>:/resources/dup.png</iconset>
                                       </property>
                                       <property name="iconSize">
                                        <size>
                                         <width>32</width>
                                         <height>32</height>
                                        </size>
                                       </property>
                                      </widget>
                                     </item>
                                     <item>
                                      <widget class="QToolButton" name="toolButton_lock">
                                       <property name="sizePolicy">
                                        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
                                         <horstretch>0</horstretch>
                                         <verstretch>0</verstretch>
                                        </sizepolicy>
                                       </property>
                                       <property name="sizeIncrement">
                                        <size>
                                         <width>1</width>
                                         <height>1</height>
                                        </size>
                                       </property>
                                       <property name="text">
                                        <string>...</string>
                                       </property>
                                       <property name="icon">
                                        <iconset resource="../../resources.qrc">
                                         <normaloff>:/resources/lock.png</normaloff>:/resources/lock.png</iconset>
                                       </property>
                                       <property name="iconSize">
                                        <size>
                                         <width>32</width>
                                         <height>32</height>
                                        </size>
                                       </property>
                                       <property name="checkable">
                                        <bool>true</bool>
                                       </property>
                                      </widget>
                                     </item>
                                     <item>
                                      <widget class="QToolButton" name="toolButton_hide">
                                       <property name="sizePolicy">
                                        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
                                         <horstretch>0</horstretch>
                                         <verstretch>0</verstretch>
                                        </sizepolicy>
                                       </property>
                                       <property name="minimumSize">
                                        <size>
                                         <width>20</width>
                                         <height>20</height>
                                        </size>
                                       </property>
                                       <property name="sizeIncrement">
                                        <size>
                                         <width>1</width>
                                         <height>1</height>
                                        </size>
                                       </property>
                                       <property name="text">
                                        <string>...</string>
                                       </property>
                                       <property name="icon">
                                        <iconset resource="../../resources.qrc">
                                         <normaloff>:/resources/hide.png</normaloff>:/resources/hide.png</iconset>
                                       </property>
                                       <property name="iconSize">
                                        <size>
                                         <width>32</width>
                                         <height>32</height>
                                        </size>
                                       </property>
                                       <property name="checkable">
                                        <bool>true</bool>
                                       </property>
                                      </widget>
                                     </item>
                                     <item>
                                      <widget class="QToolButton" name="toolButton_del">
                                       <property name="sizePolicy">
                                        <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
                                         <horstretch>0</horstretch>
                                         <verstretch>0</verstretch>
                                        </sizepolicy>
                                       </property>
                                       <property name="sizeIncrement">
                                        <size>
                                         <width>1</width>
                                         <height>1</height>
                                        </size>
                                       </property>
                                       <property name="text">
                                        <string>...</string>
                                       </property>
                                       <property name="icon">
                                        <iconset resource="../../resources.qrc">
                                         <normaloff>:/resources/x.png</normaloff>:/resources/x.png</iconset>
                                       </property>
                                       <property name="iconSize">
                                        <size>
                                         <width>32</width>
                                         <height>32</height>
                                        </size>
                                       </property>
                                      </widget>
                                     </item>
                                    </layout>
                                   </item>
                                  </layout>
                                 </widget>
                                </item>
                                <item alignment="Qt::AlignHCenter|Qt::AlignBottom">
                                 <widget class="QScrollArea" name="scrollArea_2">
                                  <property name="widgetResizable">
                                   <bool>true</bool>
                                  </property>
                                  <widget class="QWidget" name="scrollAreaWidgetContents_2">
                                   <property name="geometry">
                                    <rect>
                                     <x>0</x>
                                     <y>0</y>
                                     <width>68</width>
                                     <height>68</height>
                                    </rect>
                                   </property>
                                  </widget>
                                 </widget>
                                </item>
                               </layout>
                              </widget>
                             </item>
                            </layout>
                           </widget>
                          </widget>
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote last edited by
                          #14

                          There you go: centralwidget has no layout.

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

                          JonBJ 1 Reply Last reply
                          1
                          • SGaistS SGaist

                            There you go: centralwidget has no layout.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote last edited by JonB
                            #15

                            @SGaist said in QWidget modification at runtime:

                            There you go: centralwidget has no layout.

                            @SamuraiDestroy
                            ...And in order to to set a layout on the centralwidget on a QMainWindow in Designer you have to set it (right-click, select Lay out > item) on the parent MainWindow instead.... Note that this brain -damaged behaviour applies only in this case.

                            S S 2 Replies Last reply
                            0
                            • JonBJ JonB

                              @SGaist said in QWidget modification at runtime:

                              There you go: centralwidget has no layout.

                              @SamuraiDestroy
                              ...And in order to to set a layout on the centralwidget on a QMainWindow in Designer you have to set it (right-click, select Lay out > item) on the parent MainWindow instead.... Note that this brain -damaged behaviour applies only in this case.

                              S Offline
                              S Offline
                              SimonSchroeder
                              wrote last edited by
                              #16

                              @JonB said in QWidget modification at runtime:

                              Note that this brain -damaged behaviour applies only in this case.

                              That's not true! It also applies for QStackedWidget (to set the layout of the page you need to make that page current and then set the layout of the stacked widget IIRC).

                              SGaistS 1 Reply Last reply
                              0
                              • S SimonSchroeder

                                @JonB said in QWidget modification at runtime:

                                Note that this brain -damaged behaviour applies only in this case.

                                That's not true! It also applies for QStackedWidget (to set the layout of the page you need to make that page current and then set the layout of the stacked widget IIRC).

                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote last edited by
                                #17

                                @SimonSchroeder I would venture that it is almost more intuitive for QStackedWidget 😅

                                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
                                • JonBJ JonB

                                  @SGaist said in QWidget modification at runtime:

                                  There you go: centralwidget has no layout.

                                  @SamuraiDestroy
                                  ...And in order to to set a layout on the centralwidget on a QMainWindow in Designer you have to set it (right-click, select Lay out > item) on the parent MainWindow instead.... Note that this brain -damaged behaviour applies only in this case.

                                  S Offline
                                  S Offline
                                  SamuraiDestroy
                                  wrote last edited by
                                  #18

                                  @JonB After applying a horizontal layout to the centralwidget using this method, the window behaves the same way it did before and changing the size policies to expanding, it still exhibits strange behaviour, but no longer appears to crop the ui elements
                                  Here's how the window looks when created:
                                  3aa69d84-0e13-480e-8cf6-9a2fa17074f9-image.png
                                  And here it is after being refreshed:
                                  9328bcf0-f5d9-455c-bbd9-c7049af40d0d-image.png
                                  The window mostly resizes automatically now so some of the refresh function is redundant, but the size and positioning of some widgets is off

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • S SamuraiDestroy

                                    @JonB After applying a horizontal layout to the centralwidget using this method, the window behaves the same way it did before and changing the size policies to expanding, it still exhibits strange behaviour, but no longer appears to crop the ui elements
                                    Here's how the window looks when created:
                                    3aa69d84-0e13-480e-8cf6-9a2fa17074f9-image.png
                                    And here it is after being refreshed:
                                    9328bcf0-f5d9-455c-bbd9-c7049af40d0d-image.png
                                    The window mostly resizes automatically now so some of the refresh function is redundant, but the size and positioning of some widgets is off

                                    Christian EhrlicherC Online
                                    Christian EhrlicherC Online
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote last edited by
                                    #19

                                    @SamuraiDestroy said in QWidget modification at runtime:

                                    but the size and positioning of some widgets is off

                                    Then you still miss some layouts.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    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
                                    • Unsolved