Search code examples
pythonpyqtpyqt5qscrollarea

PyQt QScrollArea no scrollarea


I have

class View(QtWidgets.QLabel):
    def __init__(self):
        super(View,self).__init__()

        self.cropLabel = QtWidgets.QLabel(self)
        self.label = QtWidgets.QLabel(self)

        self.ogpixmap = QtGui.QPixmap()
        fileName = r'C:/Users/user11.HPO-SAMAT/Pictures/Lake.jpg'
        image = QtGui.QImage(fileName)
        self.pixmap = QtGui.QPixmap.fromImage(image)
        self.label.setPixmap(self.pixmap)
        self.label.adjustSize()

and then I call this class:

class Viewer(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.view = View()
        self.scroller = QtWidgets.QScrollArea()
        self.scroller.setWidget(self.view)
        self.scroller.setWidgetResizable(True)
        self.scroller.adjustSize()

But QScrollArea does not seem to work (noscrollbar though the image is visible and I can expand QMainWindows to see it entirely) What am I doing wrong ?


Solution

  • I do not understand so they put several labels inside View, if we remove the labels that are other we get what you want.

    class View(QtWidgets.QLabel):
        def __init__(self, parent=None):
            super(View,self).__init__(parent)
            fileName = "/home/qhipa/Pictures/1475777628875.jpg"
            self.pixmap = QtGui.QPixmap(fileName)
            self.setPixmap(self.pixmap)
    
    
    class Viewer(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            self.view = View(self)
            self.scroller = QtWidgets.QScrollArea(self)
            self.setCentralWidget(self.scroller)
            self.scroller.setWidget(self.view)
            self.scroller.setWidgetResizable(True)
            self.scroller.adjustSize()
    

    enter image description here

    Instead if you want to get several labels, it is better that the View class inherits from QWidget.

    class View(QtWidgets.QWidget):
        def __init__(self, parent=None):
            super(View,self).__init__(parent)
            self.setLayout(QtWidgets.QVBoxLayout())
    
            self.cropLabel = QtWidgets.QLabel(self)
            self.label = QtWidgets.QLabel(self)
    
            self.layout().addWidget(self.cropLabel)
            self.layout().addWidget(self.label)
            self.pixmap = QtGui.QPixmap("/home/qhipa/Pictures/1475777628875.jpg")
            self.label.setPixmap(self.pixmap)
            self.label.adjustSize()