PyQt6:删除小部件周围的所有填充

问题描述 投票:0回答:1

我正在尝试将两个 QLineEdit-widgets 一直推到彼此,但无论我如何以及何时调用

setContentsMargins(0,0,0,0)
,它似乎并没有消除所有边距。我试过谷歌搜索,但所有评论都只说“使用
layout.setContentsMargins(0,0,0,0)
”,仅此而已。有人可以向我解释为什么这在我的示例中不起作用以及如何解决它吗?

代码:

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QLineEdit
enter image description hereimport sys

class CustomWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setContentsMargins(0,0,0,0)

        self.qlineedit1 = QLineEdit()
        self.qlineedit1.setContentsMargins(0,0,0,0)

        self.qlineedit2 = QLineEdit()
        self.qlineedit2.setContentsMargins(0,0,0,0)

        self.general_layout = QHBoxLayout()
        self.general_layout.setContentsMargins(0,0,0,0)

        self.general_layout.addWidget(self.qlineedit1)
        self.general_layout.addWidget(self.qlineedit2)

        self.setLayout(self.general_layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = TimeWidget1()
    w.show()
    sys.exit(app.exec())

出现的窗口:

There is clearly a gap here

python pyqt qwidget pyqt6 qlayout
1个回答
0
投票

解决办法是打电话

layout.setSpacing(0)

代码:

from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QLineEdit
enter image description hereimport sys

class CustomWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setContentsMargins(0,0,0,0)

        self.qlineedit1 = QLineEdit()
        self.qlineedit1.setContentsMargins(0,0,0,0)

        self.qlineedit2 = QLineEdit()
        self.qlineedit2.setContentsMargins(0,0,0,0)

        self.general_layout = QHBoxLayout()
        self.general_layout.setContentsMargins(0,0,0,0)

        self.general_layout.addWidget(self.qlineedit1)
        self.general_layout.addWidget(self.qlineedit2)

        self.setLayout(self.general_layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = TimeWidget1()
    w.show()
    sys.exit(app.exec())

结果: No padding to speak of

© www.soinside.com 2019 - 2024. All rights reserved.