0

I am trying to centre a line of widgets in a QGridLayout using pyqt6. I have the widgets in the line and the first few are not made any bigger than the default, but the last one I would like to have made bigger as it's the most important box that is there. My code looks like below (I have tried to add only the important bits as the full code is quite long)

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)

Currently, the widgets look like this,

Current widgets

I would like the first four to be in the centre of the line, not justified with the top or bottom of the final box, is there a way to do this?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Use the *alignment* argument of [addWidget](https://doc.qt.io/qt-5/qgridlayout.html#addWidget-1): e.g. `layout.addWidget(self.progEnocean, 0, 0, Qt.AlignVCenter)`. – ekhumoro Sep 14 '21 at 10:25
  • Thanks, this has helped, I still have some work to do to get it exactly how I want, but will play around with the Qt.Align options to see if I can get to where I want. I did need to change it to Qt.AlignmentFlag.AlignVCenter, as I am using PyQt6, but managed to work that one out myself. – ProgramMasher Sep 14 '21 at 10:58
  • @ProgramMasher I suggest you to play around with layouts in Designer too: while you don't need to use it and creating UI from code is perfectly file, it really helps in directly understanding how layout work by changing the properties of their widgets on the fly, instead of continuously modifying the code to see the result (the widget alignment inside a layout is accessible using the context menu). – musicamante Sep 14 '21 at 13:51
  • Thanks, I had looked at Qt Designer but had not delved into it. I have spent some time with it now and it is making a lot more sense. – ProgramMasher Sep 29 '21 at 14:12

0 Answers0