如何防止小部件使用 QGridLayout 展开?

时间:2021-02-05 00:22:39

标签: python qt pyqt pyqt5

我尝试将 setSizePolicy 设置为最小值,但没有奏效。这是我的主窗口小部件:

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUi()

    def initUi(self):
        self.resize(const.SCREEN_WIDTH, const.SCREEN_HEIGHT)
        self.center()
        self.setWindowTitle(const.MAIN_WINDOW_TITLE)
        self.centralWidget = QWidget()
        self.grid = QGridLayout()
        self.centralWidget.setLayout(self.grid)
        self.setCentralWidget(self.centralWidget)
        self.error_dialog = QErrorMessage()

        self.createMenu()
        self.addWidgets()

        self.show()

    def addWidgets(self):
        self.wadListLabel = QLabel("Wad List:")
        self.wadList = WadList()
        self.pathInputLabel = QLabel("GZDoom Path:")
        self.pathInput = PathInput()
        self.lostSoulLabel = QLabel()
        self.lostSoulPixmap = QPixmap("assets/lost_soul_sprite.png")
        self.lostSoulLabel.setPixmap(self.lostSoulPixmap)
        self.launchButton = QPushButton("Launch")
        self.grid.addWidget(self.pathInputLabel, 0, 0)
        self.grid.addWidget(self.pathInput, 1, 0)
        self.grid.addWidget(self.wadListLabel, 2, 0)
        self.grid.addWidget(self.wadList, 3, 0)
        self.grid.addWidget(self.lostSoulLabel, 0, 1, Qt.AlignHCenter)
        self.grid.addWidget(self.launchButton, 2, 1, Qt.AlignBottom)
        self.grid.addWidget(self.launchButton, 3, 1, Qt.AlignBottom)
        
        self.grid.setRowStretch(0, 3)
        self.grid.setColumnStretch(0, 3)
        self.grid.setColumnStretch(1, 1)
        
    def createMenu(self):
        self.openAction = OpenAction(self, self.addWads)
        self.exitAction = ExitAction(self)

        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('&File')
        fileMenu.addAction(self.openAction)
        fileMenu.addAction(self.exitAction)

        helpMenu = menuBar.addMenu('&Help')

    def addWads(self, wads):
        existent = False
        for wad in wads:
            foundItems = self.wadList.findItems(wad, Qt.MatchExactly)
            if len(foundItems) > 0:
                existent = True
                self.error_dialog.showMessage(f"The wad {wad} has already been added to the wad list.")
        if not existent:
            self.wadList.addItems(wads)

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

如何避免这种情况?

enter image description here

由于丢失的灵魂与路径输入标签在同一列中,路径输入标签扩展而这不是我想要的,我想阻止该小部件扩展到图像标签的大小。有没有办法使用 QGridLayout 做到这一点?

编辑:位置 0, 0 处的 self.pathInputLabel 的高度由于位置 0, 1 处的 self.lostSoulLabel 的高度而扩展。如何保持其高度固定并防止其扩展?

1 个答案:

答案 0 :(得分:0)

感谢评论中的Ekhumoro,问题已经解决。还需要添加 Qt.AlignHCenter 对齐。

enter image description here

def addWidgets(self):
    self.wadListLabel = QLabel("Wad List:")
    self.wadList = WadList()
    self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Expanding, QSizePolicy.Expanding)
    self.pathInputLabel = QLabel("GZDoom Path:")
    self.pathInputLabel.setMaximumHeight(20)
    self.pathInput = PathInput()
    self.lostSoulLabel = QLabel()
    self.lostSoulPixmap = QPixmap("assets/lost_soul_sprite.png")
    self.lostSoulLabel.setPixmap(self.lostSoulPixmap)
    self.lostSoulLabel.setAlignment(Qt.AlignHCenter)
    self.launchButton = QPushButton("Launch")
    self.grid.addWidget(self.pathInputLabel, 0, 0)
    self.grid.addWidget(self.pathInput, 1, 0)
    self.grid.addWidget(self.wadListLabel, 2, 0)
    self.grid.addWidget(self.wadList, 3, 0)
    self.grid.addWidget(self.lostSoulLabel, 0, 1, 4, 1, Qt.AlignTop)
    self.grid.addWidget(self.launchButton, 2, 1, Qt.AlignBottom)
    self.grid.addWidget(self.launchButton, 3, 1, Qt.AlignBottom)
    self.grid.setColumnStretch(0, 3)
    self.grid.setColumnStretch(1, 1)
相关问题