0

我正在尝试使用 pyqt6 在 QGridLayout 中将一行小部件居中。我在行中有小部件,前几个没有比默认值大,但最后一个我想做得更大,因为它是那里最重要的盒子。我的代码如下所示(我试图只添加重要的部分,因为完整的代码很长)

class runTest(QWidget):
    def __init__(self, mylogs, data):
        super().__init__()

        self.data = data

        self.mylogs = mylogs

        self.mylogs.info("Starting run test window")

        self.setWindowTitle("Running Test")

        layout = QGridLayout()

        self.progEnocean = QLabel("Enocean Program Results")
        layout.addWidget(self.progEnocean, 0, 0)
        self.progEnocean.setStyleSheet("border: 1px solid black;")

        self.enoceanProgFile = QLabel("EnOcean Program File")
        layout.addWidget(self.enoceanProgFile, 0, 1)
        self.enoceanProgFile.setStyleSheet(
            "border: 1px solid black;" "background-color: Red;"
        )
        self.enoceanProgFile.setAlignment(
            Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
        )

        self.seccfgFile = QLabel("Secure Config File")
        layout.addWidget(self.seccfgFile, 0, 2)
        self.seccfgFile.setStyleSheet(
            "border: 1px solid black;" "background-color: Red;"
        )
        self.seccfgFile.setAlignment(
            Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
        )

        self.cpbBit = QLabel("Config File")
        layout.addWidget(self.cpbBit, 0, 3)
        self.cpbBit.setStyleSheet("border: 1px solid black;" "background-color: Red;")
        self.cpbBit.setAlignment(
            Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
        )

        self.enoceanOverallResult = QLabel("EnOcean Overall Results")
        layout.addWidget(self.enoceanOverallResult, 0, 4, 2, 2)
        self.enoceanOverallResult.setStyleSheet(
            "border: 1px solid black;" "background-color: Red;"
        )
        self.enoceanOverallResult.setAlignment(
            Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter
        ) 

        self.setLayout(layout)

目前,小部件看起来像这样,

当前小部件

我希望前四个位于行的中心,而不是与最后一个框的顶部或底部对齐,有没有办法做到这一点?

4

0 回答 0