我可以使用下面的代码正确地获得布局,如何在水平和垂直方向调整此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)
添加更多说明。 我可以正确地获得布局,现在代码生成的布局如下所示。
此布局的宽度太宽,如何将其调整为较小但不更改字体大小,如何向此QGridLayout添加scorll bar?
相同的高度,有100个问题,QGridLayout将太长,无法显示它们。如何调整QGridLayout的高度,滚动条?
下图是我想要的最终结果
仅显示部分布局以节省UI空间。拖动滚动条以显示此布局的其他部分。我不知道如何在代码中做到这一点,只需用油漆编辑图片。
答案 0 :(得分:1)
首先要做的是创建一个显示小部件结构的图表,在这种情况下我们可以使用下图:
.
└── groupbox
└── scrollarea
└── contents
├── form
└── spaceritem
然后您必须处理某些策略来处理大小,下一部分显示结果代码
代码:
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_())
<强>截图:强>