0

For the same PyQt4 code, the layout looks quite equivalent on Linux and Windows. But with MacOSX, we see that :

  • the elements seems to take a little more space (mostly bt_bookmark which doesn't shrink to the expected size)
  • a lot of space is used between the elements (that's the most annoying thing)

How could it be shrink to the minimum? Is there a margin policy that we need to set globally or anything else?

Here is the code used for the test case :

from PyQt4 import QtGui, QtCore
import sys


class GUI(QtGui.QWidget):
    def __init__(self):
        super(GUI, self).__init__()

        vbox_layout = QtGui.QVBoxLayout()
        for i in range(4):
            hbox_layout = QtGui.QHBoxLayout()
            bt_bookmark = QtGui.QPushButton()
            bt_bookmark.setGeometry(0, 0, 15, 15)
            bt_bookmark.setIcon(QtGui.QIcon("./bookmark_on.png"))
            hbox_layout.addWidget(bt_bookmark)
            hbox_layout.addWidget(QtGui.QPushButton("Button1"))
            hbox_layout.addWidget(QtGui.QLabel("Some text here."))
            hbox_layout.addWidget(QtGui.QPushButton("Button2"))
            hbox_layout.addWidget(QtGui.QPushButton("Button3"))
            vbox_layout.addLayout(hbox_layout)

        self.setLayout(vbox_layout)

        self.setWindowTitle("Test Layout")
        self.show()
        self.resize_window_to_minimum()

    def resize_window_to_minimum(self):
        # http://stackoverflow.com/a/28667119/446302
        def _func_to_call():
            self.resize(self.minimumSizeHint())
        QtCore.QTimer.singleShot(500, _func_to_call)


if __name__ == "__main__":
    app = QtGui.QApplication([])
    gui = GUI()
    sys.exit(app.exec_())

And the captures :

On Ubuntu 14.04

On OSX 10.10.3

samb
  • 1,713
  • 4
  • 22
  • 31

1 Answers1

1

To reduce the space between the widgets you can use QBoxLayout.setSpacing().

About the size of the buttons, you could try to adjust it by setting the margin or padding with style sheets.

Personally I would set the stretch of the bt_bookmark to 0 (hbox_layout.addWidget(bt_bookmark, stretch=0)) and the stretch factor of the other buttons to 1. This way the button bt_bookmark doesn't grow when the window is resized.

titusjan
  • 5,376
  • 2
  • 24
  • 43
  • Thanks. I could solve it with : `self.setContentsMargins(2, 2, 2, 2)` , `vbox_layout.setSpacing(0)` and `vbox_layout.setContentsMargins(0, 0, 0, 0)` – samb May 06 '15 at 09:29