Shrink a QPushButton width to the minimum

16,919

Solution 1

setMaximumWidth works for me

from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        layout = QtGui.QHBoxLayout()
        texts = [":)",
                 "&Short",
                 "&Longer",
                 "&Different && text",
                 "More && text",
                 "Even longer button text", ]
        for text in texts:
            btn = QtGui.QPushButton(text)
            double = text.count('&&')
            text = text.replace('&', '') + ('&' * double)
            width = btn.fontMetrics().boundingRect(text).width() + 7
            btn.setMaximumWidth(width)
            layout.addWidget(btn)
        self.setLayout(layout)

if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)
    mainWin = Window()
    mainWin.show()
    sys.exit(app.exec_())

Solution 2

See http://qt-project.org/doc/qt-4.8/qwidget.html#sizePolicy-prop and http://qt-project.org/doc/qt-4.8/qsizepolicy.html#Policy-enum to learn on how to control widget sizing in a dynamic layout.

If you don't get satisfactory results by changing the SizePolicy alone (you should), you could also look into these nice guys: http://qt-project.org/doc/qt-4.8/qspaceritem.html

Share:
16,919
noisygecko
Author by

noisygecko

Updated on August 16, 2022

Comments

  • noisygecko
    noisygecko almost 2 years

    This seems like such a simple thing, but I can't seem to figure it out. How do I make the button the minimum width. It keeps expanding to the width of the layout I put it in. In the following example, the QPushButton width ends up the same as the QLabel:

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import sys
    
    class MyWindow(QWidget):
    
        def __init__(self,parent = None):
    
            QWidget.__init__(self,parent)
    
            layout = QVBoxLayout()
            layout.addWidget(QLabel('this is a really, really long label that goes on and on'))
            layout.addWidget(QPushButton('short button'))
    
            self.setLayout(layout)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = MyWindow()
        window.show()
        sys.exit(app.exec_())
    
  • chacham15
    chacham15 about 11 years
    IIRC QPushButton has a fixed minimum width (of 80 i think), trying to force it to be smaller with a policy will cause unpredictable results since that means that the policy will ignore the minimum width.
  • ypnos
    ypnos about 11 years
    In my application I have a QPushButton with both minimum and maximum width set to 16, displaying an icon of width 8. It works as intended. However, in OS X the button has not the same OS X Aqua style as all other buttons but seems to be reverted to a plain button to fulfill its size properties. So this is a side effect that needs to be considered.
  • Mad Physicist
    Mad Physicist about 7 years
    This is a very nice demo. By the way, you can transform text without any intermediate steps by just doing width = btn.fontMetrics().boundingRect(re.sub('&(?=(?:&&)*[^&])', '', text)).width() + 7. You can just replace all & that are followed by an even number of &.
  • Mad Physicist
    Mad Physicist about 7 years
    This has the advantage that the & get processed properly in-place instead of being added back to the end of the string.