[Interest] PySide2 : Adding widget to a QVBoxLayout in a QScrollArea

Tony Rietwyk tony at rightsoft.com.au
Tue Jul 13 06:44:53 CEST 2021


Hi Nicholas,

QScrollArea manages a single widget.  It provides a default, or you can 
override it - look at the docs.

So in this case, you could apply the layout and pushbutton to 
scroll.widget, instead of scroll itself.

Also take out the self.setLayout(vbox) - the layout can only belong to 
one widget.

Hope that helps,

Tony


On 13/07/2021 2:30 pm, Nicholas Yue wrote:
> Hi,
>
>   The QPushButton I added does not stay inside of a scroll area, where 
> am I calling the API wrongly ?
>
> import sys
> from PySide2 import QtWidgets, QtCore
> class Example(QtWidgets.QWidget):
> def __init__(self):
> super(Example, self).__init__()
> self.initUI()
> def initUI(self):
> scroll = QtWidgets.QScrollArea()
> scroll.setWidgetResizable(True)
> vbox = QtWidgets.QVBoxLayout()
> vbox.setAlignment(QtCore.Qt.AlignTop)
> scroll.setLayout(vbox)
> vbox.addStretch(1)
> for index in range(20):
> vbox.addWidget(QtWidgets.QPushButton('button {}'.format(index)))
> self.setLayout(vbox)
> self.setGeometry(300, 300, 300, 150)
> self.setWindowTitle('Buttons')
> self.show()
> def main():
> app = QtWidgets.QApplication(sys.argv)
> ex = Example()
> ex.show()
> sys.exit(app.exec_())
> if __name__ == '__main__':
> main()
>
> Cheers
> -- 
> Nicholas Yue
> https://www.linkedin.com/in/nicholasyue/ 
> <https://www.linkedin.com/in/nicholasyue/>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210713/9a191710/attachment.html>


More information about the Interest mailing list