PyQt QScrollArea no scrollarea

情到浓时终转凉″ 提交于 2020-01-07 09:24:48

问题


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 ?


回答1:


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

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


来源:https://stackoverflow.com/questions/45515445/pyqt-qscrollarea-no-scrollarea

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