简体   繁体   English

如何根据比例调整QLayout的大小?

[英]How do I size QLayout based on proportionality?

I'm creating a simple dialog box for my PySide application. 我正在为我的PySide应用程序创建一个简单的对话框。 Within this dialog, there are going to be multiple inputs that the user will have to fill out. 在此对话框中,用户将必须填写多个输入。 Associated with those inputs are labels that go alongside the left of the labels. 与这些输入相关联的是标签左侧的标签。 Right now I create the label, input pair using a separate class: 现在,我使用一个单独的类创建标签,输入对:

class inputLayout(PyGui.QHBoxLayout):
    def __init__(self, Label, parent):
        super(inputLayout, self).__init__()

        label = PyGui.QLabel()
        label.setText(Label)
        self.addWidget(label)

        self.__input = PyGui.QTextEdit()
        self.addWidget(self.__input)

        parent.addLayout(self)

and then add it to the master layout like so: 然后将其添加到主布局中,如下所示:

layout = PyGui.QVBoxLayout()

self.amp = inputLayout('Amplitude', layout)
self.test = inputLayout('test', layout)
self.test2 = inputLayout('test2', layout)

The problem is that when PySide does its automagic, it get something like the following: 问题在于,当PySide进行自动处理时,它会得到如下所示的内容:

哦,不!

Like my image suggests, I'd rather have the Label take up 1/3 (or some other proportional rate of my choice) to make it look more unified. 就像我的图像所暗示的那样,我希望Label占用1/3(或我选择的其他比例比率)以使其看起来更加统一。 How do I size the layout using this proportionality, or ratio? 如何使用此比例或比例来调整布局大小?


I am aware of this question , however I'm not looking to statically set the size of the label, but rather do it dynamically using a ratio. 知道这个问题 ,但是我并不是要静态设置标签的大小,而是要使用比率来动态地设置标签的大小。

Here's a small example that should solve your problem. 这是一个应该解决您问题的小例子。 The key is in two parts: 关键分为两部分:

  1. Use a QGridLayout , which sets the column width to the width of the widest widget in its column (unless defined otherwise). 使用QGridLayout ,它将列宽设置为其列中最宽控件的宽度(除非另有定义)。 This ensures that everything is aligned nicely along the vertical axis. 这样可以确保所有内容都沿垂直轴很好地对齐。

  2. Set a stretch factor. 设置拉伸因子。 This determines how an element should resize when their parent is resized. 这确定了在元素的父元素调整大小后应如何调整大小。 By default it's 0, so you don't have to set it, I just added it for illustration purposes. 默认情况下它是0,所以您不必设置它,我只是出于说明目的添加了它。 By setting the text_edit's column to 1, it will start stretching. 通过将text_edit的列设置为1,它将开始拉伸。 By playing with the factors, you can make one column grow faster than the other. 通过考虑因素,可以使一列的增长快于另一列。


from PySide import QtGui
import sys

class MainWindow(QtGui.QMainWindow):
  def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.input_widget = InputWidget(self)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.input_widget)
        self.setLayout(self.layout)
        self.setCentralWidget(self.input_widget)

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

        self.grid_layout = QtGui.QGridLayout()
        self.labels = ["amp", "more text", "blabla"]

        self.text_edits = []
        self.qlabels = []

        for row, label in enumerate(self.labels):
            label = QtGui.QLabel(label)
            self.qlabels.append(label)
            self.grid_layout.addWidget(label, row, 0)
            text_edit = QtGui.QTextEdit()
            self.text_edits.append(text_edit)
            self.grid_layout.addWidget(text_edit, row, 1)

        self.grid_layout.setColumnStretch(0, 0)
        self.grid_layout.setColumnStretch(1, 2)

        self.setLayout(self.grid_layout)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.exec_() 

If you want to access the text from the text_edit , you could search for the label's index in self.labels and then use that label in self.text_edits to retrieve the corresponding text_edit . 如果要从text_edit访问文本,则可以在self.labels搜索标签的索引,然后在self.text_edits使用该标签来检索相应的text_edit Alternatively, once you close the dialog, you could loop through both self.labels and self.text_edits and create a dictionary that maps the label to the text from the text_edit . 另外,关闭对话框后,可以遍历self.labelsself.text_edits并创建一个字典,该字典将标签映射到text_edit的文本。

results = {}
for label, text_edit in zip(self.labels, self.text_edits):
    results[label] = text_edit.text()

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

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