Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setMargin() method not worked for QVBoxLayout in Qt Python

Tags:

python

pyside

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?

like image 896
Thang Ha Avatar asked Sep 16 '25 17:09

Thang Ha


1 Answers

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)
like image 181
eyllanesc Avatar answered Sep 19 '25 06:09

eyllanesc