2

I can successfully get the layout correctly with the code below, how can I resize this QGridLayout horizontally and vertically?

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?

Same for height, there are 100 questions, QGridLayout will be too long to show them all. How can I resize the height of QGridLayout, scroll bar?

Below image is the final result I want

Only show partial of the layout to save space for 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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jack
  • 27
  • 5
  • You could show an image of what you get and what you want to get, it would also be great to share your project and the images via github or similar – eyllanesc Oct 24 '17 at 04:13
  • Your description is very vague, that's why I ask for the images – eyllanesc Oct 24 '17 at 17:13
  • Totally edited this question to make it more clearly, image link is also added – Jack Oct 24 '17 at 18:09
  • The image seems correct, what do you want to change? Or is it the image of what you want to get ?, if so it shows an image of what you get with your current code – eyllanesc Oct 24 '17 at 18:29
  • I have tried to prove your code and I do not see any inconveniences: https://imgur.com/a/zebwc – eyllanesc Oct 24 '17 at 18:40
  • It is not exactly what I want in terms of the layout width and height. For layout width, it is too wide, it's better if I can resize the width, not sure if adding scroll bar is the best idea. For layout height, same problem, I may have 100 questions item to list, which is super long show them all, it's better if I can use a scroll bar. – Jack Oct 24 '17 at 18:46
  • Show an image of what you want to get, please do it I have asked many times and do not do it, it is uncomfortable to ask the same thing every time. – eyllanesc Oct 24 '17 at 18:49
  • Not sure if it's clear now, do you know how to solve it? – Jack Oct 24 '17 at 21:01
  • Here's what you want: https://imgur.com/a/54174 – eyllanesc Oct 24 '17 at 21:03
  • Thanks a lot, do you mind sharing the code you added? – Jack Oct 24 '17 at 21:11

1 Answers1

1

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:

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241