Pergunta

I'm working on a PDF reader with PyQt4 and python-popplerqt4. PDF pages (QPixmaps) are displayed into QLables, laid out vertically in a QFrame. The QFrame is placed into QScrollArea.

QMainWindow
   |_ QScrollArea
        |_ QFrame (Document: stretch)
            |_ QLabel (Page: fixed size)
            |   |_ QPixmap (stretch)
            |_ QLabel
            |   |_ QPixmap
            |_ etc.

The size of the document is determined by the fixed size of the QLabels. The QPixmap is set to stretch accordingly and the QFrame around pages naturally adjusts its size.

When I call for a zoom, pages (QLabels) are resized one by one with QLabel.setFixedSize(). The effect however is disappointing: resizing the document looks rickety and flickery. Zooming in Evince or Mendeley is really smooth in comparison.

I have read elsewhere that calling QFrame.hide() before scaling and QFrame.show() after helps. Indeed it does for a small document. However, applied to a PDF of about 700 pages for instance, means a blank QScrollArea for more than a second. Not good.

How can I scale the document in the QScrollArea and produce a smooth zoom effect?

PS: The images of the Poppler.Pages are drawn for the visible QLabel recipients only. i.e. resizing a document of 700 pages does not involve resizing as many images. It will in fact resize 2 to 4 images at the maximum (and the more, the less the resolution). Resizing the Document object then consists merely in resizing QLabels, for the most part empty.

Foi útil?

Solução

After some investigation and some help from QtForums it appears there is no easy solution to do achieve the desired zoom effect with QScrollArea. Please prove me wrong otherwise.

A better tool, though less easy to implement, is a QGraphicsView. It has a QGraphicsView.scale(float, float) method that does the zoom perfectly! It's also much easier to use, since this scale method does not even require maintaining the content (except perhaps the resolution of images if you don't load up hi-res from beginning onwards).

QGraphicsScene however, the content of the QGraphicsView, is a bit more lazy and needs more work to set up, especially with regard to the layout. In my case, since I am working on a PDF viewer, I require images aligned vertically, which the QGraphicsLinearLayout can do.

For the sake of posterity, here is an example code that shows how to implement pixmaps into a layout, into a scene into a view. It also shows, of course, how to scale the content.

#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore
from popplerqt4 import Poppler

class Application(QtGui.QApplication):

    def __init__(self):
        QtGui.QApplication.__init__(self, sys.argv)

        scene = QtGui.QGraphicsScene()
        scene.setBackgroundBrush(QtGui.QColor('darkGray'))
        layout = QtGui.QGraphicsLinearLayout(QtCore.Qt.Vertical)
        document = Poppler.Document.load('/home/test.pdf')
        document.setRenderHint(Poppler.Document.Antialiasing)
        document.setRenderHint(Poppler.Document.TextAntialiasing)
        for number in range(document.numPages()):
            page = document.page(number)
            image = page.renderToImage(100, 100)
            pixmap = QtGui.QPixmap.fromImage(image)
            container = QtGui.QLabel()
            container.setFixedSize(page.pageSize())
            container.setStyleSheet("Page { background-color : white}")
            container.setContentsMargins(0, 0, 0, 0)
            container.setScaledContents(True)
            container.setPixmap(pixmap)
            label = scene.addWidget(container)
            layout.addItem(label)

        graphicsWidget = QtGui.QGraphicsWidget()
        graphicsWidget.setLayout(layout)
        scene.addItem(graphicsWidget)
        self.view = View(scene)
        self.view.show()


class View(QtGui.QGraphicsView):

    def __init__(self, parent = None):
        QtGui.QGraphicsView.__init__(self, parent)

    def wheelEvent(self, event):

        if event.delta() > 0:
            self.scale(1.1, 1.1)
        else:
            self.scale(0.9, 0.9)

if __name__ == "__main__":
        application = Application()
        sys.exit(application.exec_())
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top