0

我一直在设计一个 PySide6 应用程序,但问题是即使我没有在布局中添加任何空间,布局仍然包含很多可见空间。

更新截图

小部件边框为红色的调试屏幕截图: 更新截图

反正有没有删除空间?这是代码:

from PySide6 import QtCore
from PySide6.QtWidgets import (
    QMainWindow,
    QVBoxLayout,
    QFormLayout,
    QWidget,
    QApplication,
    QLabel,
    QCheckBox,
    QHBoxLayout,
    QLineEdit,
    QFileDialog,
    QPushButton
)
import os
import sys


class Styler_Py(QMainWindow):
    def __init__(self, parent=None) -> None:
        super().__init__(parent)

        # Window Setup
        self.setWindowTitle("PyStyler")

        # Layout setup        
        self.container_widget = QWidget()
        self.main_layout = QVBoxLayout()
        self.container_widget.setLayout(self.main_layout)
        self.setCentralWidget(self.container_widget)

        # Irrelevant code here

        # --- New Section ---
        self.main_layout.addSpacing(10)
        self.main_layout.addStretch()
        # --- New Section ---

        # Files
        self.files_label = QLabel("<h3>Files</h3>")  # Note for the future, the closure tag did not have forward slash and have been fixed using an answer below
        self.main_layout.addWidget(self.files_label)
        self.files_layout = QVBoxLayout()
        self.main_layout.addLayout(self.files_layout)

        # Files -> Input File
        self.input_file = FileInput()
        self.input_file_layout = QHBoxLayout()
        self.files_layout.addLayout(self.input_file_layout)
        self.input_file_label = QLabel("Input File")
        self.input_file_layout.addWidget(self.input_file_label)
        self.input_file_layout.addWidget(self.input_file)
        


class FileInput(QWidget):
    def __init__(self, start_path=os.path.expanduser("~")) -> None:
        super().__init__()
        self._main_layout = QHBoxLayout()
        self.setLayout(self._main_layout)

        # Add Text edit for File Input
        self._file_input = QLineEdit(start_path)
        self._main_layout.addWidget(self._file_input)
        self._file_input_browse = QPushButton("Browse")
        self._file_input_browse.clicked.connect(self._browse_file_dialog)
        self._main_layout.addWidget(self._file_input_browse)
        self._start_path = start_path
    
    def _browse_file_dialog(self):
        if os.path.isfile(self._file_input.text()):
            path = os.path.abspath(f"{self._file_input.text()}/..")
        if os.path.isdir(self._file_input.text()):
            path = os.path.abspath(self._file_input.text())
        else:
            path = self._start_path
        file_name = QFileDialog.getOpenFileName(
            self, "Open File", path
        )
        if len(file_name[0]) > 0:
            self._file_input.setText(file_name[0])
    
    def get_path(self):
        return self._file_input.text()


def main():
    app = QApplication(sys.argv)
    app.setStyleSheet("*{ border: 1px solid red; }")
    window = Styler_Py()
    window.show()
    app.exec()

main()

我认为问题出在我的自定义小部件的某个地方,但我找不到它。我尝试添加和删除很多小部件,但没有任何反应。

调整大小时编辑 窗口的屏幕截图更大一点: 屏幕截图但更大的窗口

4

1 回答 1

1

第一个问题来自错误的标签闭包,应该是这样的:

    self.files_label = QLabel("<h3>Files</h3>")

然后,该间距是两个方面的结果:布局间距/边距和富文本边距。

  1. 布局间距由子布局从其父布局继承(因此,input_file_layout具有由 继承的默认间距files_layout,而后者又从main_layout;继承该属性。
  2. 当布局管理器安装在小部件上时,它们使用小部件的样式布局边距和间距 ( QStyle.PM_Layout*Marginand QStyle.PM_Layout*Spacing),除非被layout.setContentsMargins()or覆盖layout.setSpacing();这不仅发生在顶级小部件上,也发生在子小部件上:在子小部件上设置的布局不会继承父布局的边距或间距,它们使用默认的系统(或样式)值;这可以被覆盖
  3. 富文本元素通常会导致标签所需的空间更大sizeHint();不幸的是,没有简单的解决方法,正如布局问题文档中所解释的那样;

为了更好地理解每个小部件的边界,您可以添加一个简单的样式表用于测试目的:

    app.setStyleSheet('*{ border: 1px solid black; }')

请考虑以上将覆盖任何小部件边框(例如包括按钮),因此仅将其用于调试以了解布局的工作原理;不要其用于一般目的,尤其是在应用程序上使用时。

于 2021-07-01T20:24:00.280 回答