Jaime02
Jaime02

Reputation: 347

Aligning QGridLayout rows in QScrollArea

I am trying to create a lot of rows in a PyQt5 grid widget, but they try to expand as much as they can. How can I set a fixed cell height? They are represented like this:

enter image description here

But I would like them to stick at the top, ordered like this:

enter image description here

Code:

name = QtWidgets.QLabel()
name.setText(str(ui.nombre.toPlainText()) + "({}, {}, {})".format(do, cota, alejamiento))
borrar = QtWidgets.QPushButton()
borrar.setText("X")
borrar.clicked.connect(self.borrar)
ui.elementos.addWidget(name, self.num_elementos, 0, 1, 1)
ui.elementos.addWidget(borrar, self.num_elementos, 1, 1, 1)
self.num_elementos += 1
self.update()
print(self.puntos)

And the elementos widget is created in other class:

self.scroll = QtWidgets.QScrollArea(self.gridLayoutWidget_2)
self.scroll_widget = QtWidgets.QWidget()
self.scroll_widget.resize(200, 700)
self.elementos = QtWidgets.QGridLayout()
self.scroll_widget.setLayout(self.elementos)
self.scroll.setWidget(self.scroll_widget)
self.Punto.addWidget(self.scroll, 4, 0, 1, 3)

Upvotes: 1

Views: 406

Answers (1)

ekhumoro
ekhumoro

Reputation: 120608

You need to add a stretchable space beneath the rows of widgets, so that it pushes them all up to the top. One way to do this is to put another widget inside the scroll-widget, and then use a vertical layout to add the spacer. It will also help if you make the scroll-widget resizable, otherwise the rows will start to get squashed if too many are added.

Below is a demo that implements all that. Hopefully it should be clear how you can adapt this to work with your own code:

import sys
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.scroll = QtWidgets.QScrollArea()
        self.scroll.setWidgetResizable(True)
        self.scroll_widget = QtWidgets.QWidget()
        self.scroll_widget.setMaximumWidth(200)
        self.elementos_widget = QtWidgets.QWidget()
        vbox = QtWidgets.QVBoxLayout(self.scroll_widget)
        vbox.setContentsMargins(0, 0, 0, 0)
        vbox.addWidget(self.elementos_widget)
        vbox.addStretch()

        self.elementos = QtWidgets.QGridLayout()
        self.elementos_widget.setLayout(self.elementos)
        self.scroll.setWidget(self.scroll_widget)

        self.button = QtWidgets.QPushButton('Add')
        self.button.clicked.connect(self.crear_punto)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.scroll)
        layout.addWidget(self.button)

    def crear_punto(self):
        num_elementos = self.elementos.rowCount()
        name = QtWidgets.QLabel()
        name.setText('FOO %s' % num_elementos)
        borrar = QtWidgets.QPushButton()
        borrar.setText('X')
        self.elementos.addWidget(name, num_elementos, 0)
        self.elementos.addWidget(borrar, num_elementos, 1)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 100, 300, 500)
    window.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions