Unsolved Scrollarea scroll not set
-
Im using pyqt4 to create a scrollarea.
When I load the image I dont see the scrollbar being set.
import sys from PyQt4 import QtGui, QtCore, Qt class Canvas(QtGui.QWidget): def __init__(self, *args, **kwargs): super(Canvas, self).__init__() self.pixmap = QtGui.QPixmap() def paintEvent(self, event): print 'paintevent' painter = QtGui.QPainter(self) # painter.scale(3,3) # painter.translate(200, 200) painter.drawPixmap(QtCore.QPoint(), self.pixmap) class Window(QtGui.QMainWindow): def __init__(self): super(Window, self).__init__() self.setGeometry(50, 50, 500, 300) self.setWindowTitle("PyQT tuts!") self.setWindowIcon(QtGui.QIcon('pythonlogo.png')) openFile = QtGui.QAction("Open File", self) openFile.setShortcut("Ctrl+O") openFile.setStatusTip('Open File') openFile.triggered.connect(self.file_open) zoomIn = QtGui.QAction("Zoom", self) zoomIn.setShortcut("Ctrl+z") zoomIn.setStatusTip('Open File') zoomIn.triggered.connect(self.zoom_in) self.scrollArea = QtGui.QScrollArea() # self.scrollBars = { # QtCore.Qt.Vertical: self.scrollArea.verticalScrollBar(), # QtCore.Qt.Horizontal: self.scrollArea.horizontalScrollBar(), # } self.statusBar() mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('&File') fileMenu.addAction(openFile) fileMenu.addAction(zoomIn) self.canvas = Canvas() # self.scrollArea.setWidget(self.canvas) # self.scrollArea.setWidgetResizable(True) # self.scrollArea.setFixedHeight(400) self.home() # self.resize(200, 200) def home(self): self.show() def file_open(self): name = QtGui.QFileDialog.getOpenFileName(self, 'Open File') if name: self.load_pixmap(QtGui.QPixmap(name)) def load_pixmap(self, pixmap): self.canvas.pixmap = pixmap self.scrollArea.setWidget(self.canvas) self.scrollArea.setWidgetResizable(True) self.setCentralWidget(self.scrollArea) # self.canvas.adjustSize() # self.canvas.update() def zoom_in(self): print 'zoom in' print self.scrollArea print self.scrollBars[QtCore.Qt.Horizontal].value() self.scrollBars[QtCore.Qt.Horizontal].setValue(10) print self.scrollBars[QtCore.Qt.Horizontal].value() print self.scrollArea.horizontalScrollBarPolicy() def run(): app = QtGui.QApplication(sys.argv) GUI = Window() sys.exit(app.exec_()) run()
-
Hi and welcome to devnet,
Please take a look at the Image Viewer example to see how you can use a QScrollArea properly.
-
@SGaist Thanks, its not working once I load the image. Also, im not very familiar with C++
-
Hi, you mean Image viewer is not working when you load and image ?
Try to make window smaller. Picture has to be bigger than window before scrollbars will come. -
@mrjj The widget containing the pixmap is loading properly but the scroll bar is not visible.
-
@prem1980
Hi
Even if u make window really small ?
-
@mrjj thats correct..Did you use the python code that was given?
-
@prem1980
Hi
Hmm are we talking the Image sample or the your code here ?
Image sample works fine for me so i wonder why you said it did not for you.
Anyway, its set up this wayimageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setScaledContents(true); scrollArea->setWidget(imageLabel);
sorry cant type it in python.