如何减少 PyQt5 QGridLayout 中两个小部件之间的 space?

How to reduce the space between Two Widgets in PyQt5 QGridLayout?

如何在QGridLayout中按自己的意愿排列Widgets?例如,在我的代码中,

  1. 我需要减少“国家”和“州”按钮之间的space

  2. 将小部件放在最左上角(国家/地区按钮位于最左上角 - 无边距)

  3. 减少“国家”和“地区”按钮之间的space

  4. 将“银行”按钮放在“城镇”按钮的中央

  5. need/arrange 布局如附图

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class GridLayout_Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Grid layout Example")
        self.setGeometry(100,100,1600,600)
        self.initUI()
        self.layout()
        self.show()

    def initUI(self):
        self.btn_country_63 = QPushButton()
        self.btn_country_63.setText("Country")
        self.btn_country_63.setObjectName("ob_btn_country_63")
        self.btn_country_63.setProperty('group', "1")
        self.btn_country_63.setFixedSize(100, 25)
        self.btn_country_63.setStyleSheet("background-color:red;color:white")
        self.btn_country_63.installEventFilter(self)
        # self.btn_country_63.clicked.connect(self.btn_country_63_select)

        self.btn_state_63 = QPushButton()
        self.btn_state_63.setText("State")
        self.btn_state_63.setObjectName("ob_btn_state_63")
        self.btn_state_63.setProperty('group', "1")
        self.btn_state_63.setFixedSize(100, 25)
        self.btn_state_63.setStyleSheet("background-color:red;color:white")
        self.btn_state_63.installEventFilter(self)
        # self.btn_state_63.clicked.connect(self.btn_state_63_select)

        self.btn_district_63 = QPushButton()
        self.btn_district_63.setText("District")
        self.btn_district_63.setObjectName("ob_btn_district_63")
        self.btn_district_63.setProperty('group', "1")
        self.btn_district_63.setFixedSize(100, 25)
        self.btn_district_63.setStyleSheet("background-color:red;color:white")
        self.btn_district_63.installEventFilter(self)
        # self.btn_district_63.clicked.connect(self.btn_district_63_select)

        self.btn_town_63 = QPushButton()
        self.btn_town_63.setText("Town")
        self.btn_town_63.setObjectName("ob_btn_town_63")
        self.btn_town_63.setProperty('group', "1")
        self.btn_town_63.installEventFilter(self)
        self.btn_town_63.setFixedSize(60, 80)
        self.btn_town_63.setStyleSheet("background-color:red;color:white")
        # self.btn_town_63.clicked.connect(self.btn_town_63_select)

        self.btn_bank_63 = QPushButton()
        self.btn_bank_63.setText("Bank")
        self.btn_bank_63.setObjectName("ob_btn_bank_63")
        self.btn_bank_63.setProperty('group', "1")
        self.btn_bank_63.installEventFilter(self)
        self.btn_bank_63.setFixedSize(100, 25)
        # self.btn_bank_63.clicked.connect(self.btn_bank_63_select)

    def layout(self):

        self.layout_main_toppannel_2_right = QGridLayout()
        self.frame_main_toppannel_2_right = QFrame()
        self.frame_main_toppannel_2_right.setProperty('color', "2")
        self.frame_main_toppannel_2_right.setFixedHeight(100)
        self.frame_main_toppannel_2_right.setStyleSheet("background-color: lightgreen")
        self.layout_main_toppannel_2_right = QGridLayout(self.frame_main_toppannel_2_right)

        self.container63 = QWidget()
        self.container63_box = QGridLayout(self.container63)
        self.container63_box.addWidget(self.btn_country_63, 0, 0, Qt.AlignTop)
        self.container63_box.addWidget(self.btn_district_63, 0, 1, Qt.AlignTop)
        self.container63_box.addWidget(self.btn_state_63, 1, 0, Qt.AlignTop)
        self.container63_box.addWidget(self.btn_town_63, 0, 3)
        self.container63_box.addWidget(self.btn_bank_63,0,4)
        self.container63_box.setColumnStretch(0,4)
        self.container63_box.setRowStretch(0,2)

      
        self.stackitem = QStackedWidget()
        self.stackitem.addWidget(self.container63)
        self.layout_main_toppannel_2_right.addWidget(self.stackitem)

        self.main_layout = QHBoxLayout()
        self.main_layout.addWidget(self.frame_main_toppannel_2_right)
        self.setLayout(self.main_layout)

def main():
    app = QApplication(sys.argv)
    mainwindow = GridLayout_Example()
    mainwindow.show()
    sys.exit(app.exec_())
if __name__ == "__main__":
    main()

中我详细解释了QGridLayout的操作,所以在这个post中我只详细介绍最优秀的。

  • 要删除项目之间的 space,您必须使用 setSpacing(0)(您也可以使用 setHorizo​​ntalSpacing(0) 和 setVerticalSpacing(0))。

  • 要使元素向左推,您可以将拉伸设置为假想的第五列。

  • 我还修改了“Town”的高度,因为它与 3 个元素的总高度不匹配,迫使 spaces 出现。

  • 我还计算了绿色容器的高度,在设置项目以匹配所需的 space 并且没有 spaces.

  • 最后请注意,某些边距无法通过代码更改,因为它们是由 OS 的本机 QStyles 实现的,以避免我强制使用样式融合。

import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class GridLayout_Example(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.build_widgets()
        self.build_layout()

        self.resize(640, 480)

    def build_widgets(self):
        self.btn_country_63 = QPushButton(
            text="Country", styleSheet="background-color:red;color:white"
        )
        self.btn_country_63.setFixedSize(100, 25)

        self.btn_state_63 = QPushButton(
            text="State", styleSheet="background-color:red;color:white"
        )
        self.btn_state_63.setFixedSize(100, 25)

        self.btn_district_63 = QPushButton(
            text="District", styleSheet="background-color:red;color:white"
        )
        self.btn_district_63.setFixedSize(100, 25)

        self.btn_town_63 = QPushButton(
            text="Town", styleSheet="background-color:red;color:white"
        )
        self.btn_town_63.setFixedSize(60, 75)

        self.btn_bank_63 = QPushButton(
            text="Bank", styleSheet="background-color:red;color:white"
        )
        self.btn_bank_63.setFixedSize(100, 25)

        self.btn_zip_63 = QPushButton(
            text="Zip Code", styleSheet="background-color:red;color:white"
        )
        self.btn_zip_63.setFixedSize(100, 25)

    def build_layout(self):
        self.container = QFrame()
        self.container.setStyleSheet("background-color: lightgreen")

        grid_layout = QGridLayout(self.container)
        grid_layout.addWidget(self.btn_country_63, 0, 0, 1, 1)
        grid_layout.addWidget(self.btn_state_63, 1, 0, 1, 1)
        grid_layout.addWidget(self.btn_district_63, 0, 1, 1, 1)
        grid_layout.addWidget(self.btn_town_63, 0, 2, 3, 1)
        grid_layout.addWidget(self.btn_bank_63, 1, 3, 1, 1)
        grid_layout.addWidget(self.btn_zip_63, 2, 4, 1, 1)
        grid_layout.setColumnStretch(5, 1)
        grid_layout.setSpacing(0)
        grid_layout.setContentsMargins(0, 0, 0, 0)

        lay = QVBoxLayout(self)
        lay.addWidget(self.container)
        self.container.setFixedHeight(self.container.sizeHint().height())


def main():
    app = QApplication(sys.argv)
    print("default-style: ", app.style().metaObject().className())
    app.setStyle("fusion")
    mainwindow = GridLayout_Example()
    mainwindow.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()