3

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?

3
  • If you have 2 different questions then post 2 different posts
    – eyllanesc
    Feb 26, 2020 at 14:42
  • I think the problem is that you are using PySide, which is older version. Try PySide2 as that works with PyQt5 for which your tutorial is for.
    – SajanGohil
    Feb 26, 2020 at 14:47
  • PySide2 is not compatible with python2 so you will not be able to install it if you continue using that version, I recommend installing python3
    – eyllanesc
    Feb 26, 2020 at 15:09

1 Answer 1

5

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)
1
  • "it existed in initial versions of Qt5 but is currently deprecated" yes. It is in Obsolete Members for QLayout of Qt 5 and does not exist in Qt 6.
    – li ki
    Jun 15, 2022 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.