Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80d0778

Browse files
authored
Merge pull request matplotlib#12885 from timhoffm/formlayout-margin
Improve margins in formlayout
2 parents 73cb5c7 + 4c35cf4 commit 80d0778

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/matplotlib/backends/qt_editor/_formlayout.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,30 @@ def is_edit_valid(edit):
209209
class FormWidget(QtWidgets.QWidget):
210210
update_buttons = QtCore.Signal()
211211

212-
def __init__(self, data, comment="", parent=None):
212+
def __init__(self, data, comment="", with_margin=False, parent=None):
213+
"""
214+
Parameters
215+
----------
216+
data : list of (label, value) pairs
217+
The data to be edited in the form.
218+
comment : str, optional
219+
220+
with_margin : bool, optional, default: False
221+
If False, the form elements reach to the border of the widget.
222+
This is the desired behavior if the FormWidget is used as a widget
223+
alongside with other widgets such as a QComboBox, which also do
224+
not have a margin around them.
225+
However, a margin can be desired if the FormWidget is the only
226+
widget within a container, e.g. a tab in a QTabWidget.
227+
parent : QWidget or None
228+
The parent widget.
229+
"""
213230
QtWidgets.QWidget.__init__(self, parent)
214231
self.data = copy.deepcopy(data)
215232
self.widgets = []
216233
self.formlayout = QtWidgets.QFormLayout(self)
234+
if not with_margin:
235+
self.formlayout.setContentsMargins(0, 0, 0, 0)
217236
if comment:
218237
self.formlayout.addRow(QtWidgets.QLabel(comment))
219238
self.formlayout.addRow(QtWidgets.QLabel(" "))
@@ -370,13 +389,15 @@ def __init__(self, datalist, comment="", parent=None):
370389
layout = QtWidgets.QVBoxLayout()
371390
self.tabwidget = QtWidgets.QTabWidget()
372391
layout.addWidget(self.tabwidget)
392+
layout.setContentsMargins(0, 0, 0, 0)
373393
self.setLayout(layout)
374394
self.widgetlist = []
375395
for data, title, comment in datalist:
376396
if len(data[0]) == 3:
377397
widget = FormComboWidget(data, comment=comment, parent=self)
378398
else:
379-
widget = FormWidget(data, comment=comment, parent=self)
399+
widget = FormWidget(data, with_margin=True, comment=comment,
400+
parent=self)
380401
index = self.tabwidget.addTab(widget, title)
381402
self.tabwidget.setTabToolTip(index, comment)
382403
self.widgetlist.append(widget)

0 commit comments

Comments
 (0)