Size of verticalLayout is different in Qt Designer and PyQt program












1















In Qt Designer 5.9 the verticalLayout in test.ui had a certain distance to the edge of the window, but after loading test.ui with PyQt 5.11.3 in main.py the verticalLayout extend to the edge of the window.



main.py:



    #!/usr/bin/python3
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi

class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__()
loadUi("test.ui", self)

def main():
app = QApplication(sys.argv)
main_window = MainWindow(app)
main_window.show()
sys.exit(app.exec_())


if __name__ == "__main__":
main()


test.ui:



<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>350</width>
<height>257</height>
</rect>
</property>
<property name="windowTitle">
<string>Test</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QSpinBox" name="spinBox">
<property name="maximum">
<number>100000</number>
</property>
<property name="value">
<number>1000</number>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Test</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBox">
<property name="currentText">
<string/>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label1">
<property name="text">
<string>Test</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>350</width>
<height>28</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>




Screenshot of test.ui in Qt Designer 5.9:



enter image description here



Screenshot of main.py loading test.ui:



enter image description here



What's the reason for this behavior?










share|improve this question





























    1















    In Qt Designer 5.9 the verticalLayout in test.ui had a certain distance to the edge of the window, but after loading test.ui with PyQt 5.11.3 in main.py the verticalLayout extend to the edge of the window.



    main.py:



        #!/usr/bin/python3
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
    from PyQt5.uic import loadUi

    class MainWindow(QMainWindow):
    def __init__(self, parent=None):
    super().__init__()
    loadUi("test.ui", self)

    def main():
    app = QApplication(sys.argv)
    main_window = MainWindow(app)
    main_window.show()
    sys.exit(app.exec_())


    if __name__ == "__main__":
    main()


    test.ui:



    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>350</width>
    <height>257</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>Test</string>
    </property>
    <widget class="QWidget" name="centralwidget">
    <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
    <layout class="QVBoxLayout" name="verticalLayout">
    <property name="leftMargin">
    <number>0</number>
    </property>
    <property name="topMargin">
    <number>0</number>
    </property>
    <item>
    <spacer name="verticalSpacer_2">
    <property name="orientation">
    <enum>Qt::Vertical</enum>
    </property>
    <property name="sizeHint" stdset="0">
    <size>
    <width>20</width>
    <height>40</height>
    </size>
    </property>
    </spacer>
    </item>
    <item>
    <widget class="QGroupBox" name="groupBox">
    <layout class="QGridLayout" name="gridLayout">
    <item row="1" column="1">
    <widget class="QSpinBox" name="spinBox">
    <property name="maximum">
    <number>100000</number>
    </property>
    <property name="value">
    <number>1000</number>
    </property>
    </widget>
    </item>
    <item row="1" column="0">
    <widget class="QLabel" name="label_2">
    <property name="text">
    <string>Test</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
    </widget>
    </item>
    <item row="0" column="1">
    <widget class="QComboBox" name="comboBox">
    <property name="currentText">
    <string/>
    </property>
    </widget>
    </item>
    <item row="0" column="0">
    <widget class="QLabel" name="label1">
    <property name="text">
    <string>Test</string>
    </property>
    <property name="alignment">
    <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
    </property>
    </widget>
    </item>
    </layout>
    </widget>
    </item>
    <item>
    <spacer name="verticalSpacer">
    <property name="orientation">
    <enum>Qt::Vertical</enum>
    </property>
    <property name="sizeHint" stdset="0">
    <size>
    <width>20</width>
    <height>40</height>
    </size>
    </property>
    </spacer>
    </item>
    <item>
    <widget class="QPushButton" name="pushButton">
    <property name="layoutDirection">
    <enum>Qt::LeftToRight</enum>
    </property>
    <property name="text">
    <string>Test</string>
    </property>
    </widget>
    </item>
    </layout>
    </item>
    </layout>
    </widget>
    <widget class="QMenuBar" name="menubar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>350</width>
    <height>28</height>
    </rect>
    </property>
    </widget>
    <widget class="QStatusBar" name="statusbar"/>
    </widget>
    <resources/>
    <connections/>
    </ui>




    Screenshot of test.ui in Qt Designer 5.9:



    enter image description here



    Screenshot of main.py loading test.ui:



    enter image description here



    What's the reason for this behavior?










    share|improve this question



























      1












      1








      1








      In Qt Designer 5.9 the verticalLayout in test.ui had a certain distance to the edge of the window, but after loading test.ui with PyQt 5.11.3 in main.py the verticalLayout extend to the edge of the window.



      main.py:



          #!/usr/bin/python3
      import sys
      from PyQt5.QtWidgets import QApplication, QMainWindow
      from PyQt5.uic import loadUi

      class MainWindow(QMainWindow):
      def __init__(self, parent=None):
      super().__init__()
      loadUi("test.ui", self)

      def main():
      app = QApplication(sys.argv)
      main_window = MainWindow(app)
      main_window.show()
      sys.exit(app.exec_())


      if __name__ == "__main__":
      main()


      test.ui:



      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
      <class>MainWindow</class>
      <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
      <rect>
      <x>0</x>
      <y>0</y>
      <width>350</width>
      <height>257</height>
      </rect>
      </property>
      <property name="windowTitle">
      <string>Test</string>
      </property>
      <widget class="QWidget" name="centralwidget">
      <layout class="QGridLayout" name="gridLayout_2">
      <item row="0" column="0">
      <layout class="QVBoxLayout" name="verticalLayout">
      <property name="leftMargin">
      <number>0</number>
      </property>
      <property name="topMargin">
      <number>0</number>
      </property>
      <item>
      <spacer name="verticalSpacer_2">
      <property name="orientation">
      <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
      <size>
      <width>20</width>
      <height>40</height>
      </size>
      </property>
      </spacer>
      </item>
      <item>
      <widget class="QGroupBox" name="groupBox">
      <layout class="QGridLayout" name="gridLayout">
      <item row="1" column="1">
      <widget class="QSpinBox" name="spinBox">
      <property name="maximum">
      <number>100000</number>
      </property>
      <property name="value">
      <number>1000</number>
      </property>
      </widget>
      </item>
      <item row="1" column="0">
      <widget class="QLabel" name="label_2">
      <property name="text">
      <string>Test</string>
      </property>
      <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
      </widget>
      </item>
      <item row="0" column="1">
      <widget class="QComboBox" name="comboBox">
      <property name="currentText">
      <string/>
      </property>
      </widget>
      </item>
      <item row="0" column="0">
      <widget class="QLabel" name="label1">
      <property name="text">
      <string>Test</string>
      </property>
      <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
      </widget>
      </item>
      </layout>
      </widget>
      </item>
      <item>
      <spacer name="verticalSpacer">
      <property name="orientation">
      <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
      <size>
      <width>20</width>
      <height>40</height>
      </size>
      </property>
      </spacer>
      </item>
      <item>
      <widget class="QPushButton" name="pushButton">
      <property name="layoutDirection">
      <enum>Qt::LeftToRight</enum>
      </property>
      <property name="text">
      <string>Test</string>
      </property>
      </widget>
      </item>
      </layout>
      </item>
      </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
      <property name="geometry">
      <rect>
      <x>0</x>
      <y>0</y>
      <width>350</width>
      <height>28</height>
      </rect>
      </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
      </widget>
      <resources/>
      <connections/>
      </ui>




      Screenshot of test.ui in Qt Designer 5.9:



      enter image description here



      Screenshot of main.py loading test.ui:



      enter image description here



      What's the reason for this behavior?










      share|improve this question
















      In Qt Designer 5.9 the verticalLayout in test.ui had a certain distance to the edge of the window, but after loading test.ui with PyQt 5.11.3 in main.py the verticalLayout extend to the edge of the window.



      main.py:



          #!/usr/bin/python3
      import sys
      from PyQt5.QtWidgets import QApplication, QMainWindow
      from PyQt5.uic import loadUi

      class MainWindow(QMainWindow):
      def __init__(self, parent=None):
      super().__init__()
      loadUi("test.ui", self)

      def main():
      app = QApplication(sys.argv)
      main_window = MainWindow(app)
      main_window.show()
      sys.exit(app.exec_())


      if __name__ == "__main__":
      main()


      test.ui:



      <?xml version="1.0" encoding="UTF-8"?>
      <ui version="4.0">
      <class>MainWindow</class>
      <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
      <rect>
      <x>0</x>
      <y>0</y>
      <width>350</width>
      <height>257</height>
      </rect>
      </property>
      <property name="windowTitle">
      <string>Test</string>
      </property>
      <widget class="QWidget" name="centralwidget">
      <layout class="QGridLayout" name="gridLayout_2">
      <item row="0" column="0">
      <layout class="QVBoxLayout" name="verticalLayout">
      <property name="leftMargin">
      <number>0</number>
      </property>
      <property name="topMargin">
      <number>0</number>
      </property>
      <item>
      <spacer name="verticalSpacer_2">
      <property name="orientation">
      <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
      <size>
      <width>20</width>
      <height>40</height>
      </size>
      </property>
      </spacer>
      </item>
      <item>
      <widget class="QGroupBox" name="groupBox">
      <layout class="QGridLayout" name="gridLayout">
      <item row="1" column="1">
      <widget class="QSpinBox" name="spinBox">
      <property name="maximum">
      <number>100000</number>
      </property>
      <property name="value">
      <number>1000</number>
      </property>
      </widget>
      </item>
      <item row="1" column="0">
      <widget class="QLabel" name="label_2">
      <property name="text">
      <string>Test</string>
      </property>
      <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
      </widget>
      </item>
      <item row="0" column="1">
      <widget class="QComboBox" name="comboBox">
      <property name="currentText">
      <string/>
      </property>
      </widget>
      </item>
      <item row="0" column="0">
      <widget class="QLabel" name="label1">
      <property name="text">
      <string>Test</string>
      </property>
      <property name="alignment">
      <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
      </property>
      </widget>
      </item>
      </layout>
      </widget>
      </item>
      <item>
      <spacer name="verticalSpacer">
      <property name="orientation">
      <enum>Qt::Vertical</enum>
      </property>
      <property name="sizeHint" stdset="0">
      <size>
      <width>20</width>
      <height>40</height>
      </size>
      </property>
      </spacer>
      </item>
      <item>
      <widget class="QPushButton" name="pushButton">
      <property name="layoutDirection">
      <enum>Qt::LeftToRight</enum>
      </property>
      <property name="text">
      <string>Test</string>
      </property>
      </widget>
      </item>
      </layout>
      </item>
      </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
      <property name="geometry">
      <rect>
      <x>0</x>
      <y>0</y>
      <width>350</width>
      <height>28</height>
      </rect>
      </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
      </widget>
      <resources/>
      <connections/>
      </ui>




      Screenshot of test.ui in Qt Designer 5.9:



      enter image description here



      Screenshot of main.py loading test.ui:



      enter image description here



      What's the reason for this behavior?







      python pyqt5 qt-designer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 16:34









      eyllanesc

      80.6k103258




      80.6k103258










      asked Nov 25 '18 at 16:32









      AtalanttoreAtalanttore

      286




      286
























          1 Answer
          1






          active

          oldest

          votes


















          1














          If you check the code of uic.loadUi() you will find the following code:



          uiparser.py



          class UIParser(object):  
          # ...
          def createLayout(self, elem):
          # ...
          margin = -1 if self.stack.topIsLayout() else self.defaults['margin']
          margin = self.wprops.getProperty(elem, 'margin', margin)
          left = self.wprops.getProperty(elem, 'leftMargin', margin)
          top = self.wprops.getProperty(elem, 'topMargin', margin)
          right = self.wprops.getProperty(elem, 'rightMargin', margin)
          bottom = self.wprops.getProperty(elem, 'bottomMargin', margin)
          # A layout widget should, by default, have no margins.
          if self.stack.topIsLayoutWidget():
          if left < 0: left = 0
          if top < 0: top = 0
          if right < 0: right = 0
          if bottom < 0: bottom = 0

          def topIsLayoutWidget(self):
          # A plain QWidget is a layout widget unless it's parent is a
          # QMainWindow or a container widget. Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.

          if type(self[-1]) is not QtWidgets.QWidget:
          return False

          if len(self) < 2:
          return False

          parent = self[-2]

          return isinstance(parent, QtWidgets.QWidget) and type(parent) not in (
          QtWidgets.QMainWindow,
          QtWidgets.QStackedWidget,
          QtWidgets.QToolBox,
          QtWidgets.QTabWidget,
          QtWidgets.QScrollArea,
          QtWidgets.QMdiArea,
          QtWidgets.QWizard,
          QtWidgets.QDockWidget)


          The problem is caused by the topIsLayoutWidget() function since the parent will refer to the widget that is used as a base, in this case MainWindow complies with isinstance(parent, QtWidgets.QWidget) and type (parent) not in (QtWidgets.QMainWindow, ...) so topIsLayoutWidget() will return True, so to be left, top, right, bottom be -1 since those properties do not exist will be updated to 0 so the apply to contentsMargins will be established by eliminating the default value (9, 9, 9, 9), but in the case of Qt Designer the contentsMargins have not been updated maintaining their default value.



          So in conclusion is a pyqt bug that also points in the comments:



          # ... Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.*


          So there are several solutions:





          • Remove:



            if self.stack.topIsLayoutWidget():
            if left < 0: left = 0
            if top < 0: top = 0
            if right < 0: right = 0
            if bottom < 0: bottom = 0



          • Use uic.loadUiType():



            #!/usr/bin/python3
            import sys
            from PyQt5 import QtCore, QtWidgets, uic

            Ui_Interface, _ = uic.loadUiType('test.ui')

            class MainWindow(QtWidgets.QMainWindow, Ui_Interface):
            def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)

            def main():
            app = QtWidgets.QApplication(sys.argv)
            main_window = MainWindow()
            main_window.show()
            sys.exit(app.exec_())

            if __name__ == "__main__":
            main()



          I prefer the second solution since the source code should not be modified.






          share|improve this answer
























          • @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

            – S. Nick
            Nov 25 '18 at 19:52













          • @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

            – eyllanesc
            Nov 25 '18 at 19:54











          • @eyllanesc: Thank you for your detailed answer and suggested solutions.

            – Atalanttore
            Nov 27 '18 at 22:06











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469563%2fsize-of-verticallayout-is-different-in-qt-designer-and-pyqt-program%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          If you check the code of uic.loadUi() you will find the following code:



          uiparser.py



          class UIParser(object):  
          # ...
          def createLayout(self, elem):
          # ...
          margin = -1 if self.stack.topIsLayout() else self.defaults['margin']
          margin = self.wprops.getProperty(elem, 'margin', margin)
          left = self.wprops.getProperty(elem, 'leftMargin', margin)
          top = self.wprops.getProperty(elem, 'topMargin', margin)
          right = self.wprops.getProperty(elem, 'rightMargin', margin)
          bottom = self.wprops.getProperty(elem, 'bottomMargin', margin)
          # A layout widget should, by default, have no margins.
          if self.stack.topIsLayoutWidget():
          if left < 0: left = 0
          if top < 0: top = 0
          if right < 0: right = 0
          if bottom < 0: bottom = 0

          def topIsLayoutWidget(self):
          # A plain QWidget is a layout widget unless it's parent is a
          # QMainWindow or a container widget. Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.

          if type(self[-1]) is not QtWidgets.QWidget:
          return False

          if len(self) < 2:
          return False

          parent = self[-2]

          return isinstance(parent, QtWidgets.QWidget) and type(parent) not in (
          QtWidgets.QMainWindow,
          QtWidgets.QStackedWidget,
          QtWidgets.QToolBox,
          QtWidgets.QTabWidget,
          QtWidgets.QScrollArea,
          QtWidgets.QMdiArea,
          QtWidgets.QWizard,
          QtWidgets.QDockWidget)


          The problem is caused by the topIsLayoutWidget() function since the parent will refer to the widget that is used as a base, in this case MainWindow complies with isinstance(parent, QtWidgets.QWidget) and type (parent) not in (QtWidgets.QMainWindow, ...) so topIsLayoutWidget() will return True, so to be left, top, right, bottom be -1 since those properties do not exist will be updated to 0 so the apply to contentsMargins will be established by eliminating the default value (9, 9, 9, 9), but in the case of Qt Designer the contentsMargins have not been updated maintaining their default value.



          So in conclusion is a pyqt bug that also points in the comments:



          # ... Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.*


          So there are several solutions:





          • Remove:



            if self.stack.topIsLayoutWidget():
            if left < 0: left = 0
            if top < 0: top = 0
            if right < 0: right = 0
            if bottom < 0: bottom = 0



          • Use uic.loadUiType():



            #!/usr/bin/python3
            import sys
            from PyQt5 import QtCore, QtWidgets, uic

            Ui_Interface, _ = uic.loadUiType('test.ui')

            class MainWindow(QtWidgets.QMainWindow, Ui_Interface):
            def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)

            def main():
            app = QtWidgets.QApplication(sys.argv)
            main_window = MainWindow()
            main_window.show()
            sys.exit(app.exec_())

            if __name__ == "__main__":
            main()



          I prefer the second solution since the source code should not be modified.






          share|improve this answer
























          • @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

            – S. Nick
            Nov 25 '18 at 19:52













          • @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

            – eyllanesc
            Nov 25 '18 at 19:54











          • @eyllanesc: Thank you for your detailed answer and suggested solutions.

            – Atalanttore
            Nov 27 '18 at 22:06
















          1














          If you check the code of uic.loadUi() you will find the following code:



          uiparser.py



          class UIParser(object):  
          # ...
          def createLayout(self, elem):
          # ...
          margin = -1 if self.stack.topIsLayout() else self.defaults['margin']
          margin = self.wprops.getProperty(elem, 'margin', margin)
          left = self.wprops.getProperty(elem, 'leftMargin', margin)
          top = self.wprops.getProperty(elem, 'topMargin', margin)
          right = self.wprops.getProperty(elem, 'rightMargin', margin)
          bottom = self.wprops.getProperty(elem, 'bottomMargin', margin)
          # A layout widget should, by default, have no margins.
          if self.stack.topIsLayoutWidget():
          if left < 0: left = 0
          if top < 0: top = 0
          if right < 0: right = 0
          if bottom < 0: bottom = 0

          def topIsLayoutWidget(self):
          # A plain QWidget is a layout widget unless it's parent is a
          # QMainWindow or a container widget. Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.

          if type(self[-1]) is not QtWidgets.QWidget:
          return False

          if len(self) < 2:
          return False

          parent = self[-2]

          return isinstance(parent, QtWidgets.QWidget) and type(parent) not in (
          QtWidgets.QMainWindow,
          QtWidgets.QStackedWidget,
          QtWidgets.QToolBox,
          QtWidgets.QTabWidget,
          QtWidgets.QScrollArea,
          QtWidgets.QMdiArea,
          QtWidgets.QWizard,
          QtWidgets.QDockWidget)


          The problem is caused by the topIsLayoutWidget() function since the parent will refer to the widget that is used as a base, in this case MainWindow complies with isinstance(parent, QtWidgets.QWidget) and type (parent) not in (QtWidgets.QMainWindow, ...) so topIsLayoutWidget() will return True, so to be left, top, right, bottom be -1 since those properties do not exist will be updated to 0 so the apply to contentsMargins will be established by eliminating the default value (9, 9, 9, 9), but in the case of Qt Designer the contentsMargins have not been updated maintaining their default value.



          So in conclusion is a pyqt bug that also points in the comments:



          # ... Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.*


          So there are several solutions:





          • Remove:



            if self.stack.topIsLayoutWidget():
            if left < 0: left = 0
            if top < 0: top = 0
            if right < 0: right = 0
            if bottom < 0: bottom = 0



          • Use uic.loadUiType():



            #!/usr/bin/python3
            import sys
            from PyQt5 import QtCore, QtWidgets, uic

            Ui_Interface, _ = uic.loadUiType('test.ui')

            class MainWindow(QtWidgets.QMainWindow, Ui_Interface):
            def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)

            def main():
            app = QtWidgets.QApplication(sys.argv)
            main_window = MainWindow()
            main_window.show()
            sys.exit(app.exec_())

            if __name__ == "__main__":
            main()



          I prefer the second solution since the source code should not be modified.






          share|improve this answer
























          • @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

            – S. Nick
            Nov 25 '18 at 19:52













          • @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

            – eyllanesc
            Nov 25 '18 at 19:54











          • @eyllanesc: Thank you for your detailed answer and suggested solutions.

            – Atalanttore
            Nov 27 '18 at 22:06














          1












          1








          1







          If you check the code of uic.loadUi() you will find the following code:



          uiparser.py



          class UIParser(object):  
          # ...
          def createLayout(self, elem):
          # ...
          margin = -1 if self.stack.topIsLayout() else self.defaults['margin']
          margin = self.wprops.getProperty(elem, 'margin', margin)
          left = self.wprops.getProperty(elem, 'leftMargin', margin)
          top = self.wprops.getProperty(elem, 'topMargin', margin)
          right = self.wprops.getProperty(elem, 'rightMargin', margin)
          bottom = self.wprops.getProperty(elem, 'bottomMargin', margin)
          # A layout widget should, by default, have no margins.
          if self.stack.topIsLayoutWidget():
          if left < 0: left = 0
          if top < 0: top = 0
          if right < 0: right = 0
          if bottom < 0: bottom = 0

          def topIsLayoutWidget(self):
          # A plain QWidget is a layout widget unless it's parent is a
          # QMainWindow or a container widget. Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.

          if type(self[-1]) is not QtWidgets.QWidget:
          return False

          if len(self) < 2:
          return False

          parent = self[-2]

          return isinstance(parent, QtWidgets.QWidget) and type(parent) not in (
          QtWidgets.QMainWindow,
          QtWidgets.QStackedWidget,
          QtWidgets.QToolBox,
          QtWidgets.QTabWidget,
          QtWidgets.QScrollArea,
          QtWidgets.QMdiArea,
          QtWidgets.QWizard,
          QtWidgets.QDockWidget)


          The problem is caused by the topIsLayoutWidget() function since the parent will refer to the widget that is used as a base, in this case MainWindow complies with isinstance(parent, QtWidgets.QWidget) and type (parent) not in (QtWidgets.QMainWindow, ...) so topIsLayoutWidget() will return True, so to be left, top, right, bottom be -1 since those properties do not exist will be updated to 0 so the apply to contentsMargins will be established by eliminating the default value (9, 9, 9, 9), but in the case of Qt Designer the contentsMargins have not been updated maintaining their default value.



          So in conclusion is a pyqt bug that also points in the comments:



          # ... Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.*


          So there are several solutions:





          • Remove:



            if self.stack.topIsLayoutWidget():
            if left < 0: left = 0
            if top < 0: top = 0
            if right < 0: right = 0
            if bottom < 0: bottom = 0



          • Use uic.loadUiType():



            #!/usr/bin/python3
            import sys
            from PyQt5 import QtCore, QtWidgets, uic

            Ui_Interface, _ = uic.loadUiType('test.ui')

            class MainWindow(QtWidgets.QMainWindow, Ui_Interface):
            def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)

            def main():
            app = QtWidgets.QApplication(sys.argv)
            main_window = MainWindow()
            main_window.show()
            sys.exit(app.exec_())

            if __name__ == "__main__":
            main()



          I prefer the second solution since the source code should not be modified.






          share|improve this answer













          If you check the code of uic.loadUi() you will find the following code:



          uiparser.py



          class UIParser(object):  
          # ...
          def createLayout(self, elem):
          # ...
          margin = -1 if self.stack.topIsLayout() else self.defaults['margin']
          margin = self.wprops.getProperty(elem, 'margin', margin)
          left = self.wprops.getProperty(elem, 'leftMargin', margin)
          top = self.wprops.getProperty(elem, 'topMargin', margin)
          right = self.wprops.getProperty(elem, 'rightMargin', margin)
          bottom = self.wprops.getProperty(elem, 'bottomMargin', margin)
          # A layout widget should, by default, have no margins.
          if self.stack.topIsLayoutWidget():
          if left < 0: left = 0
          if top < 0: top = 0
          if right < 0: right = 0
          if bottom < 0: bottom = 0

          def topIsLayoutWidget(self):
          # A plain QWidget is a layout widget unless it's parent is a
          # QMainWindow or a container widget. Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.

          if type(self[-1]) is not QtWidgets.QWidget:
          return False

          if len(self) < 2:
          return False

          parent = self[-2]

          return isinstance(parent, QtWidgets.QWidget) and type(parent) not in (
          QtWidgets.QMainWindow,
          QtWidgets.QStackedWidget,
          QtWidgets.QToolBox,
          QtWidgets.QTabWidget,
          QtWidgets.QScrollArea,
          QtWidgets.QMdiArea,
          QtWidgets.QWizard,
          QtWidgets.QDockWidget)


          The problem is caused by the topIsLayoutWidget() function since the parent will refer to the widget that is used as a base, in this case MainWindow complies with isinstance(parent, QtWidgets.QWidget) and type (parent) not in (QtWidgets.QMainWindow, ...) so topIsLayoutWidget() will return True, so to be left, top, right, bottom be -1 since those properties do not exist will be updated to 0 so the apply to contentsMargins will be established by eliminating the default value (9, 9, 9, 9), but in the case of Qt Designer the contentsMargins have not been updated maintaining their default value.



          So in conclusion is a pyqt bug that also points in the comments:



          # ... Note that the corresponding uic
          # test is a little more complicated as it involves features not
          # supported by pyuic.*


          So there are several solutions:





          • Remove:



            if self.stack.topIsLayoutWidget():
            if left < 0: left = 0
            if top < 0: top = 0
            if right < 0: right = 0
            if bottom < 0: bottom = 0



          • Use uic.loadUiType():



            #!/usr/bin/python3
            import sys
            from PyQt5 import QtCore, QtWidgets, uic

            Ui_Interface, _ = uic.loadUiType('test.ui')

            class MainWindow(QtWidgets.QMainWindow, Ui_Interface):
            def __init__(self, parent=None):
            super().__init__(parent)
            self.setupUi(self)

            def main():
            app = QtWidgets.QApplication(sys.argv)
            main_window = MainWindow()
            main_window.show()
            sys.exit(app.exec_())

            if __name__ == "__main__":
            main()



          I prefer the second solution since the source code should not be modified.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 18:19









          eyllanesceyllanesc

          80.6k103258




          80.6k103258













          • @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

            – S. Nick
            Nov 25 '18 at 19:52













          • @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

            – eyllanesc
            Nov 25 '18 at 19:54











          • @eyllanesc: Thank you for your detailed answer and suggested solutions.

            – Atalanttore
            Nov 27 '18 at 22:06



















          • @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

            – S. Nick
            Nov 25 '18 at 19:52













          • @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

            – eyllanesc
            Nov 25 '18 at 19:54











          • @eyllanesc: Thank you for your detailed answer and suggested solutions.

            – Atalanttore
            Nov 27 '18 at 22:06

















          @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

          – S. Nick
          Nov 25 '18 at 19:52







          @ eyllanesc sorry, isn't it easier to convert test.ui to test_ui.py?

          – S. Nick
          Nov 25 '18 at 19:52















          @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

          – eyllanesc
          Nov 25 '18 at 19:54





          @S.Nick It is also a valid option, but according to what the OP points out, I think he wants to keep the .ui, so for me the proper solution is to use loadUiType: you do not need to convert to .py and it does not generate the problem of margins.

          – eyllanesc
          Nov 25 '18 at 19:54













          @eyllanesc: Thank you for your detailed answer and suggested solutions.

          – Atalanttore
          Nov 27 '18 at 22:06





          @eyllanesc: Thank you for your detailed answer and suggested solutions.

          – Atalanttore
          Nov 27 '18 at 22:06




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469563%2fsize-of-verticallayout-is-different-in-qt-designer-and-pyqt-program%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'