How do i resize the contents of a QScrollArea as more widgets are placed inside

前端 未结 3 1184
悲哀的现实
悲哀的现实 2020-12-05 13:58

I have a QScrollArea Widget, which starts empty;

\"empty

It has a vertical layout, with a

3条回答
  •  臣服心动
    2020-12-05 14:26

    If you're coming here from Google and not having luck with the accepted answer, that's because you're missing the other secret invocation: QScrollArea::setWidget. You must create and explicitly identify a single widget which is to be scrolled. It's not enough to just add the item as a child! Adding multiple items directly to the ScrollArea will also not work.

    This script demonstrates a simple working example of QScrollArea:

    from PySide.QtGui import *
    
    app = QApplication([])
    
    scroll = QScrollArea()
    scroll.setWidgetResizable(True) # CRITICAL
    
    inner = QFrame(scroll)
    inner.setLayout(QVBoxLayout())
    
    scroll.setWidget(inner) # CRITICAL
    
    for i in range(40):
        b = QPushButton(inner)
        b.setText(str(i))
        inner.layout().addWidget(b)
    
    scroll.show()
    app.exec_()
    

提交回复
热议问题