Thang Ha
Thang Ha

Reputation: 41

setMargin() method not worked for QVBoxLayout in Qt Python

I use PySide instead, therefore in my program, instead

from PySide.QtGui import QVBoxLayout

Normally it works for importing but then when I called method .setMargin() as the tutorial : https://doc.qt.io/qtforpython/tutorials/expenses/expenses.html#right-side-layout, I got an error:

self.right = QVBoxLayout()
self.right.setMargin(10)

AttributeError: 'PySide.QtGui.QVBoxLayout' object has no attribute 'setMargin'

I tried to find the setMargin() in the library and it appear here: https://doc.qt.io/qtforpython/PySide2/QtWidgets/QLayout.html#PySide2.QtWidgets.PySide2.QtWidgets.QLayout.setMargin. That means I can call it by importing QLayout from Pyside.QtGui but that doesn't work. The cmd say:

NotImplementedError: 'QLayout' represents a C++ abstract class and cannot be instantiated

Could you please show me how to use setMargin() by an alternative way in my case?

Upvotes: 4

Views: 5651

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

setMargin() does not exist in Qt4 (PySide), it existed in initial versions of Qt5 but is currently deprecated, instead you must use the setContentsMargins() method:

self.right = QVBoxLayout()
self.right.setContentsMargins(10, 10, 10, 10)

Upvotes: 7

Related Questions