setColumnStretch 和 setRowStretch 如何工作 [英] How does setColumnStretch and setRowStretch works

查看:441
本文介绍了setColumnStretch 和 setRowStretch 如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 PySide2 构建的应用程序,它使用 setColumnStretch 进行列拉伸,使用 setRowStretch 进行行拉伸.它运行良好,但我无法理解它是如何工作的.我参考了 qt 文档,但它对我没有帮助.我被困在那些括号内的两个值上.

例如:

glay = QtWidgets.QGridLayout(right_container)glay.addWidget(lineedit, 0, 0)glay.addWidget(button2, 0, 2)glay.addWidget(widget, 2, 0, 1, 3)glay.addWidget(button, 4, 0)glay.addWidget(button1, 4, 2)glay.setColumnStretch(1, 1) # setColumnStretchglay.setRowStretch(1, 1) # setRowStretchglay.setRowStretch(2, 2) # setRowStretchglay.setRowStretch(3, 1) # setRowStretch

这会产生如下图所示的输出:

但是怎么样?glay.addWidget(widget, 2, 0, 1, 3) 中的这四个值有什么作用?请用例子向我解释这一切.

解决方案

简短回答: 阅读 Qt 文档:

  • setColumnStretch()setRowStretch():

    默认情况下,如果 QGridLayout 填充相同的小部件并且 rowSpan 和 columnSpan 为 1,则所有小部件的大小都相同,但很多时候您希望小部件占用更多空间,或者大小成比例.为了理解逻辑,我将使用以下代码:

    随机导入导入系统从 PyQt5 导入 QtGui、QtWidgets如果 __name__ == "__main__":app = QtWidgets.QApplication(sys.argv)w = QtWidgets.QWidget()glay = QtWidgets.QGridLayout(w)对于范围内的 i (3):对于范围内的 j(3):label = QtWidgets.QLabel("{}x{}".format(i, j))颜色 = QtGui.QColor(*random.sample(range(255), 3))label.setStyleSheet("background-color: {}".format(color.name()))glay.addWidget(label, i, j)glay.setRowStretch(0, 1)glay.setRowStretch(1, 2)glay.setRowStretch(2, 3)w.resize(640, 480)w.show()sys.exit(app.exec_())

    确定第一列的拉伸为1,第二列为2,第三列为3,即行的大小比例为1:2:3

    如果将拉伸设置为 0 会发生什么?好吧,它将占据最小尺寸,那些拉伸 > 0 的将保持比例:

    glay.setRowStretch(0, 0)glay.setRowStretch(1, 1)glay.setRowStretch(2, 2)

    同样的概念适用于 setColumnStretch().

  • I have an application built using PySide2 which uses setColumnStretch for column stretching and setRowStretch for row stretching. It works well and good, but i am unable to understand how it is working. I refer to the qt docs, but it doesn't helped me. I am stuck on the two values inside those parenthesis.

    For example :

    glay = QtWidgets.QGridLayout(right_container)
    glay.addWidget(lineedit, 0, 0)
    glay.addWidget(button2, 0, 2)
    
    glay.addWidget(widget, 2, 0, 1, 3)  
    
    glay.addWidget(button, 4, 0)                                    
    glay.addWidget(button1, 4, 2)
    
    glay.setColumnStretch(1, 1)                                     # setColumnStretch
    glay.setRowStretch(1, 1)                                        # setRowStretch
    glay.setRowStretch(2, 2)                                        # setRowStretch
    glay.setRowStretch(3, 1)                                        # setRowStretch
    
    

    This produces the output as shown in the image below :

    But how? What does these four values inside glay.addWidget(widget, 2, 0, 1, 3)do? Please explain me all this with examples.

    解决方案

    Short Answer: Read the Qt docs: https://doc.qt.io/qt-5/qgridlayout.html as it is clear and precise.

    Long Answer:

    • addWidget():

      The addWidget method is overload (that concept exists natively in C++ but can be built in python but does not exist by default) which implies that a method (or function) has a different behavior depending on the arguments, in this case:

      void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment = Qt::Alignment())
      void addWidget(QWidget *widget, int fromRow, int fromColumn, int rowSpan, int columnSpan, Qt::Alignment alignment = Qt::Alignment())
      

      The first method indicates that the widget will be placed in the position "row" and "column" that an item occupies, and in the second method the number of rows or columns that it occupies can be indicated, and they are equivalent to:

      def addWidget(self, row, column, rowSpan = 1, colSpan = 1, alignment = Qt.Alignment()):
          pass 
      

      So in lay.addWidget(widget, 2, 0, 1, 3) it means that "widget" will be placed at position 2x0 and will occupy 1 row and 3 columns.

      import random
      import sys
      
      from PyQt5 import QtGui, QtWidgets
      
      if __name__ == "__main__":
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QWidget()
          glay = QtWidgets.QGridLayout(w)
          elements = (
              (0, 0, 1, 1),  # Position: 0x0 1 rowspan 1 colspan
              (1, 0, 1, 1),  # Position: 1x0 1 rowspan 1 colspan
              (0, 1, 2, 1),  # Position: 0x1 2 rowspan 1 colspan
              (2, 0, 1, 2),  # Position: 2x0 1 rowspan 2 colspan
          )
          for i, (row, col, row_span, col_span) in enumerate(elements):
              label = QtWidgets.QLabel("{}".format(i))
              color = QtGui.QColor(*random.sample(range(255), 3))
              label.setStyleSheet("background-color: {}".format(color.name()))
              glay.addWidget(label, row, col, row_span, col_span)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())
      

    • setColumnStretch() and setRowStretch():

      By default if a QGridLayout is filled with the same widgets and the rowSpan and columnSpan are 1 then all the widgets will be the same size, but many times you want a widget to take up more space, or the sizes are proportional. To understand the logic I will use the following code:

      import random
      import sys
      
      from PyQt5 import QtGui, QtWidgets
      
      if __name__ == "__main__":
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QWidget()
          glay = QtWidgets.QGridLayout(w)
          for i in range(3):
              for j in range(3):
                  label = QtWidgets.QLabel("{}x{}".format(i, j))
                  color = QtGui.QColor(*random.sample(range(255), 3))
                  label.setStyleSheet("background-color: {}".format(color.name()))
                  glay.addWidget(label, i, j)
          glay.setRowStretch(0, 1)
          glay.setRowStretch(1, 2)
          glay.setRowStretch(2, 3)
          w.resize(640, 480)
          w.show()
          sys.exit(app.exec_())
      

      It was established that the stretch of the first column is 1, the second is 2 and the third is 3, which is the ratio of the size of the rows 1:2:3

      What happens if you set stretch to 0? Well, it will occupy the minimum size and those with a stretch > 0 will keep the proportion:

      glay.setRowStretch(0, 0)
      glay.setRowStretch(1, 1)
      glay.setRowStretch(2, 2)
      

      The same concept applies to setColumnStretch().

    这篇关于setColumnStretch 和 setRowStretch 如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆