Make the scroll area horizontal to fit the content

I want the dialog to expand horizontally to show all images . I don't want it to expand vertically as there will be a scrollbar. The stuff sizePolicy

that is commented doesn't help. The images are just four images that are side by side wider than the window.

What am I doing wrong?

from PyQt4 import QtGui, QtCore

class Images(QtGui.QDialog):
    def __init__(self, pics, size, imagesPerRow=6, imagePopup=True, parent=None):
        QtGui.QDialog.__init__(self)
        self.scrollArea = QtGui.QScrollArea(self)
        self.scrollArea.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        #sp = QtGui.QSizePolicy()
        #sp.setHorizontalPolicy(QtGui.QSizePolicy.Expanding)
        #self.setSizePolicy(sp)
        #self.scrollAreaWidgetContents.setSizePolicy(sp)
        #self.scrollArea.setSizePolicy(sp)

        self.verticalLayout = QtGui.QVBoxLayout(self)
        self.verticalLayout.addWidget(self.scrollArea)

        self.gLayoutScroll = QtGui.QGridLayout(self.scrollAreaWidgetContents)

        row = col = 0
        for pic in pics:
            thumb = QtGui.QLabel()
            pixmap = QtGui.QPixmap(pic)
            pixmap = pixmap.scaled(size, QtCore.Qt.KeepAspectRatioByExpanding, QtCore.Qt.SmoothTransformation)
            thumb.setPixmap(pixmap)
            self.gLayoutScroll.addWidget(thumb, row, col)

            col +=1
            if col % imagesPerRow == 0:
                row += 1
                col = 0

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myDialog')

    main = Images(['one.png','two.png','three.png','four.png'], size=QtCore.QSize(192,192))
    main.show()

    sys.exit(app.exec_())

      

+3


source to share


1 answer


You can simply set the minimum width of the scroll area to the width of the content. Thus, the scroll area will initially display the full horizontal width of the content widget.

Here's my example (PySide, Python 3):

from PySide import QtGui, QtCore

class Images(QtGui.QScrollArea):
    def __init__(self, images):
        super().__init__()

        self.content = QtGui.QWidget()
        self.layout = QtGui.QGridLayout(self.content)
        self.layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
        col = 0
        for image in images:
            thumb = QtGui.QLabel()
            thumb.setPixmap(QtGui.QPixmap(image))
            self.layout.addWidget(thumb, 0, col)
            col += 1

        self.setWidget(self.content)
        self.setMinimumWidth(self.content.sizeHint().width())
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

app = QtGui.QApplication([])

window = QtGui.QWidget()
layout = QtGui.QVBoxLayout(window)
scroll_area = Images(['test.png','test.png','test.png','test.png'])
layout.addWidget(scroll_area)
window.show()

app.exec_()

      

where the minimum scrollbar width is set to the preferred width of the content after images are added. I also rotate the horizontal scrollbar because then it is not needed.



The result is a window that is large enough horizontally, but not necessarily vertically (manually modified).

expanded scroll area

Two comments:

  • If the content changes width, you need to do it again.
  • The vertical scrollbar will be inside (overlapped) with the content widgets. Unless you want the vertical scrollbar width to grow to the minimum width of the scroll area.
+3


source







All Articles