Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've tried setting setSizePolicy to minimum and it didn't work. Here's my main window widget:

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

How do I avoid this?

enter image description here

Due to the lost soul being in the same column as the path input label, the path input label expands and that's not what I want, I want to block that widget from expanding to the size of the image label. Is there a way to do that using QGridLayout?

EDIT: The height of the self.pathInputLabel at position 0, 0 expands due to the height of the self.lostSoulLabel at position 0, 1. How do I keep its height fixed and prevent it to expand?

question from:https://stackoverflow.com/questions/66055844/how-to-prevent-a-widget-from-expanding-using-qgridlayout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
210 views
Welcome To Ask or Share your Answers For Others

1 Answer

Thanks to Ekhumoro in the comments, the problem has been solved. Needed to add a Qt.AlignHCenter alignment as well.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...