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

孤人 提交于 2019-11-28 23:05:17
UmNyobe

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.

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_()

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!