Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - How to prevent a widget from expanding using QGridLayout?

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...