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

前端 未结 3 1176
悲哀的现实
悲哀的现实 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:11

    Why don't you use a QListView for your rows, it will manage all the issues for you? Just make sure that after you add it you click on the Class (top right window of designer) and assign a layout or it wont expand properly.

    I use a QLIstWidget inside a QScrollArea to make a scrollable image list

    Try this for adding other objects to the list, this is how I add an image to the list.

    QImage& qim = myclass.getQTImage();
    
    QImage iconImage = copyImageToSquareRegion(qim, ui->display_image->palette().color(QWidget::backgroundRole()));
    
    QListWidgetItem* pItem = new QListWidgetItem(QIcon(QPixmap::fromImage(iconImage)), NULL);
    
    pItem->setData(Qt::UserRole, "thumb" + QString::number(ui->ImageThumbList->count()));  // probably not necessary for you
    
    QString strTooltip = "a tooltip"
    
    pItem->setToolTip(strTooltip);
    
    ui->ImageThumbList->addItem(pItem);
    
    0 讨论(0)
  • 2020-12-05 14:20

    The documentation provide an answer :

    widgetResizable : bool
    This property holds whether the scroll area should resize the view widget. If this property is set to false (the default), the scroll area honors the size of its widget.

    Set it to true.

    0 讨论(0)
  • 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_()
    
    0 讨论(0)
提交回复
热议问题