2

enter image description here

I want labels on the left to all have the same horizontal length while text aligned to the left. Their vertical size equals vertical size of the respective right widget.

Labels on the right to take as little space as possible. Basically remove indents around text.

Something like below.

enter image description here

I have this code.

import sys
from PyQt5.QtCore import Qt
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout


class Window2(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("About")

        vbox = QVBoxLayout()

        hboxes = list()
        disclaimer = {
            'Text': """
                some text
            """,
            'Longer text': """
                longer text longer text text longer text longer    
            """
        }

        for label, text in disclaimer.items():
            hbox = QHBoxLayout()

            for t in (label, text):
                l = QLabel(t)
                l.setAlignment(Qt.AlignLeft)
                l.setStyleSheet('border-style: solid; border-width: 1px; border-color: black;')
                hbox.addWidget(l)

            vbox.addLayout(hbox)

        self.setLayout(vbox)
        self.show()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main_window = Window2()
    sys.exit(app.exec_())

I can't seem to figure out how it works/what is margin, indent, padding, spacing, stretch etc. Please help me understand and solve this problem.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Vitamin C
  • 135
  • 11

1 Answers1

3

It has 2 errors:

  • The multiline string adds spaces so you have 2 options: use strip() to remove them or remove them manually, in this case I will use the second option.
  • Do not use nested QHBoxLayout inside a QVBoxLayout since they will not maintain alignment, instead use a QGridLayout.
class Window2(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("About")

        disclaimer = {
            "Text": """some text""",
            "Longer text": """longer text longer text text longer text longer""",
        }

        gridlay = QGridLayout(self)

        for i, (label, text) in enumerate(disclaimer.items()):

            for j, t in enumerate((label, text)):
                l = QLabel(t.strip())
                l.setAlignment(Qt.AlignLeft)
                l.setStyleSheet(
                    "border-style: solid; border-width: 1px; border-color: black;"
                )
                gridlay.addWidget(l, i, j)

        self.show()

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241