移除QLabel中不必要的空格。

huangapple go评论78阅读模式
英文:

remove unnecessary blank space in Qlabel

问题

以下是您要翻译的代码部分:

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_())

希望这可以帮助您理解和解决您的问题。

英文:

移除QLabel中不必要的空格。

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.

移除QLabel中不必要的空格。

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.

答案1

得分: 3

有2个错误:

  • 多行字符串添加了空格,因此您有两个选项:使用strip()将其删除或手动删除它们,在这种情况下,我将使用第二个选项。
  • 不要在QVBoxLayout内使用嵌套的QHBoxLayout,因为它们不会保持对齐,而是使用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()

移除QLabel中不必要的空格。

英文:

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()

移除QLabel中不必要的空格。

huangapple
  • 本文由 发表于 2020年1月3日 19:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定