[英]how can I make QGridLayout() resizable in both horizontal and vertical directions?
I can successfully get the layout correctly with the code below, how can I resize this QGridLayout horizontally and vertically?我可以使用下面的代码成功获得正确的布局,如何水平和垂直调整此 QGridLayout 的大小?
self._s_q0= QLabel(u'what is your favorite color?')
self._e_q0 = QLineEdit(self)
self._e_q0.setText("yellow is the best color I can think of")
self._s_q1= QLabel(u'what is your favorite animal?')
self._e_q1 = QLineEdit(self)
self._e_q1.setText("cat or dog")
self._s_q2= QLabel(u'do you like swimming?')
self._e_q2 = QLineEdit(self)
self._e_q2.setText("not at all")
self._s_q3= QLabel(u'what date is it today?')
self._e_q3 = QLineEdit(self)
self._e_q3.setText("i dont know, you can ask Tom, he has a cellphone with him right now")
self._groupData = QGroupBox("100 questions list", self)
self._groupData_layout = QGridLayout()
self._groupData.setLayout(self._groupData_layout)
self._groupData_layout.addWidget(self._s_q0, 0, 0)
self._groupData_layout.addWidget(self._e_q0, 0, 1)
self._groupData_layout.addWidget(self._s_q1, 1, 0)
self._groupData_layout.addWidget(self._e_q1, 1, 1)
self._groupData_layout.addWidget(self._s_q2, 2, 0)
self._groupData_layout.addWidget(self._e_q2, 2, 1)
self._groupData_layout.addWidget(self._s_q3, 3, 0)
self._groupData_layout.addWidget(self._e_q3, 3, 1)
Add more description.添加更多说明。 I can get the layout correctly, the layout generated by code now is like below .
我可以正确获得布局,现在代码生成的布局如下所示。
The width of this layout is too wide, how can I resize it smaller but not change the font size, how can I add a scorll bar to this QGridLayout?这个布局的宽度太宽了,我怎样才能把它缩小但不改变字体大小,我怎样才能给这个 QGridLayout 添加一个滚动条?
Same for height, there are 100 questions, QGridLayout will be too long to show them all.高度相同,有100个问题,QGridLayout太长而无法全部显示。 How can I resize the height of QGridLayout, scroll bar?
如何调整 QGridLayout、滚动条的高度?
Below image is the final result I want下图是我想要的最终结果
Only show partial of the layout to save space for UI.仅显示部分布局以节省 UI 空间。 Drag scroll bar to show other part of this layout.
拖动滚动条以显示此布局的其他部分。 I don't know how to do it in code, just edit picture with paint.
我不知道如何在代码中做到这一点,只需用油漆编辑图片。
The first task that one must do is to create a diagram that shows the structure of the widgets, in this case we can use the following diagram:必须做的第一个任务是创建一个显示小部件结构的图表,在这种情况下,我们可以使用下图:
.
└── groupbox
└── scrollarea
└── contents
├── form
└── spaceritem
Then you must handle certain policies to handle the sizes, the next part shows the resulting code然后你必须处理某些策略来处理大小,下一部分显示结果代码
Code:代码:
class GroupBox(QGroupBox):
def __init__(self, *args, **kwargs):
QGroupBox.__init__(self, *args, **kwargs)
vlayout = QVBoxLayout(self)
scrollArea = QScrollArea(self)
scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget()
scrollArea.setWidget(self.scrollAreaWidgetContents)
vlayout.addWidget(scrollArea)
hlayout = QHBoxLayout(self.scrollAreaWidgetContents)
flayout = QFormLayout()
hlayout.addLayout(flayout)
hlayout.addItem(QSpacerItem(170, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
for i in range(100):
le = QLineEdit(self.scrollAreaWidgetContents)
le.setText("i dont know, you can ask Tom, he has a cellphone with him right now")
le.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
flayout.addRow("what date is it {}".format(i), le)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setStyle("fusion")
w = QWidget()
w.setLayout(QVBoxLayout())
g = GroupBox("100 questions list")
w.layout().addWidget(g)
w.show()
sys.exit(app.exec_())
Screenshot:截屏:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.