PyQt4: set size of QGridLayout depending on size of QMainWindow

有些话、适合烂在心里 提交于 2020-01-06 13:25:28

问题


I'm writing a little Qt application with Python. I've created QMainWindow, which have a QGridLayout. In each grid I'm adding QTextBrowser Widget. I want left side of my grid to be not bigger than 25% of window. So I'll have two QTextBrowsers: one is 25% of window's width, and another is 75% of window's width. How can I do it? Thanks!


回答1:


You can specify relative width by giving each cell a stretch with setStretch(). They will get sizes proportional to the given stretches. Here is a simple example that makes the right widget 3 times greater than the left widget.

import sys
from PyQt4 import QtGui

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    Q = QtGui.QWidget()

    H = QtGui.QHBoxLayout()
    H.addWidget(QtGui.QTextBrowser())
    H.setStretch(0,1)
    H.addWidget(QtGui.QTextBrowser())
    H.setStretch(1,3)

    Q.setLayout(H)

    Q.show()

    app.exec_()

But, bear in mind that widgets have minimum sizes by default. So they can shrink below that. If you want to change that behavior also, consider setting minimum sizes to your liking.



来源:https://stackoverflow.com/questions/7619121/pyqt4-set-size-of-qgridlayout-depending-on-size-of-qmainwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!