Search code examples
qtqt5qpushbuttonqlayout

QPushButton of minimal width in QHBoxLayout?


I want to create the QPushButton with text "X" to close the window. I want to place it to the button's bar. To do so, I made QHLayout.

layTop=QHBoxLayout()

self.btnMenu=QPushButton("Menu") #just as sample of button with normal size
layTop.addWidget(self.btnMenu)

btnX=QPushButton("X")  #I need it small
btnX.setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Preferred) #I try this, but it's not work as I expected
layTop.addWidget(btnX)

But, "X" button is as big as other buttons. How to make it narrow?

(screenshot for this code is below)

expectation-vs-reality for SizePolicy


Solution

  • Use a QToolButton instead of QPushButton:

    enter image description here