How to remove the margin around a custom PyQt widget?

Viewed 81

I created a custom widget using PyQt:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QVBoxLayout, QTextEdit


class Item(QtWidgets.QWidget):

    def __init__(self):
        super(Item, self).__init__()
        vbox = QVBoxLayout()
        vbox.addWidget(QTextEdit())
        self.setLayout(vbox)

Then I added this widget to a dialog:

import sys

from PyQt5.QtWidgets import QVBoxLayout, QLabel, QApplication, QDialog, \
    QTextEdit

from Item import Item


class Main(QDialog):
    def __init__(self):
        super(Main, self).__init__()
        self.init_ui()

    def init_ui(self):
        vbox = QVBoxLayout()
        vbox.addWidget(QTextEdit())
        vbox.addWidget(Item())
        self.setLayout(vbox)


def main():
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

The result is as follows:

enter image description here

As you can see, the custom widget (below) seems to have some margins to its left and right, how to remove the margin? I want the result to be like this:

enter image description here

1 Answers

Thanks to the comments of @musicamante, I added the following code to Item and the problem was solved:

vbox.setContentsMargins(0, 0, 0, 0)
Related