简体   繁体   English

PyQt4-如何在Mac OSX上缩小布局?

[英]PyQt4 - How to shrink the layout on Mac OSX?

For the same PyQt4 code, the layout looks quite equivalent on Linux and Windows. 对于相同的PyQt4代码,布局在Linux和Windows上看起来相当等效。 But with MacOSX, we see that : 但是使用MacOSX,我们看到:

  • the elements seems to take a little more space (mostly bt_bookmark which doesn't shrink to the expected size) 元素似乎要占用更多空间(主要是bt_bookmark不会缩小到预期的大小)
  • 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 : 和捕获:

在Ubuntu 14.04上

在OSX 10.10.3上

To reduce the space between the widgets you can use QBoxLayout.setSpacing() . 要减少小部件之间的空间,可以使用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. 我个人将bt_bookmark的拉伸设置为0( hbox_layout.addWidget(bt_bookmark, stretch=0) ),将其他按钮的拉伸因子设置为1。这样,调整窗口大小时,按钮bt_bookmark不会增大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM