krrr
krrr

Reputation: 609

Why does PyQt's pyuic ignore default margins?

In Qt Designer I can see verticalLayout has a default margin (13px), but pyuic5 always gives me self.verticalLayout.setContentsMargins(0, 0, 0, 0). In uiparser.py:443 I found this comment:

A layout widget should, by default, have no margins.

and it sets the margins to (0,0,0,0). Another comment also says:

A plain QWidget is a layout widget unless it's parent is a QMainWindow. Note that the corresponding uic test is a little more complicated as it involves features not supported by pyuic.

What does this mean? I'm using PyQt-5.7, and here is my ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>configDialog</class>
 <widget class="QDialog" name="configDialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>566</width>
    <height>552</height>
   </rect>
  </property>
  <layout class="QHBoxLayout" name="horizontalLayout">
   <item>
    <widget class="QTabWidget" name="tabWidget">
     <property name="currentIndex">
      <number>0</number>
     </property>
     <widget class="QWidget" name="general">
      <attribute name="title">
       <string>General</string>
      </attribute>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <widget class="QPushButton" name="pushButton">
         <property name="text">
          <string>PushButton</string>
         </property>
        </widget>
       </item>
      </layout>
     </widget>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

Upvotes: 3

Views: 497

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

There is a PyQt mailing list thread on this, which seems to have resulted in the default margins being forced to zero for certain widgets. I don't know whether the behaviour of the C++ uic tool has changed since then, but in Qt-5.7 it certainly doesn't behave the same way as pyuic does.

This can be checked in Qt Designer by selecting View -> View code. When the margins are all set to their default values, the C++ uic tool does not generate a setContentsMargins line at all. And if one margin is set, it will set the other values to minus one. When a margin is not set, or set to minus one, the layout will use whatever value is specified by the current style.

In contrast, pyuic always explicitly resets to zero any margin which is not set, or set to minus one - which effectively overrides the current style. It's hard to see how this can be the correct behaviour - so maybe it's a regression?

Upvotes: 2

Related Questions