Jack
Jack

Reputation: 27

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?

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.

Upvotes: 2

Views: 879

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

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

Upvotes: 1

Related Questions