How to adjust the size of a widget within a horizontal layout

Colt :

I want all my widgets to change in length (horizontally) as I enlarge the GUI window. Right now, only the line edit changes size when I make the window larger. More than anything, I want my button and combobox to be longer.

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QGridLayout, QWidget, QListWidget, QLineEdit, QHBoxLayout, QPushButton


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)
        self.Search_Bar = QLineEdit(placeholderText="Search")
        self.Button = QPushButton('button')
        layout = QHBoxLayout(centralWidget)
        layout.addWidget(self.Button)
        layout.addWidget(self.Search_Bar)



if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
eyllanesc :

There are 2 possible solutions:

  • Set the same stretch by adding the widgets to the layout:

    layout.addWidget(self.Button, stretch=1)
    layout.addWidget(self.Search_Bar, stretch=1)
  • Set QSizePolicy::Expanding on the horizontal component of the sizePolicy button:

    sp = self.Button.sizePolicy()
    sp.setHorizontalPolicy(QtWidgets.QSizePolicy.Expanding)
    self.Button.setSizePolicy(sp)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21339&siteId=1