English 中文(简体)
如何防止QScrollArea的放大 变得如此冷酷无情?
原标题:How to prevent zooming in QScrollArea from being so rickety-flickery?

我正用 PyQt4 和 python-popplerqt4 制作一个 PDF 阅读器。 PDF 页面( QPixmaps) 显示在QLable 中, 垂直显示在 QFrame 中。 QFrame 放置在 QScrollArea 中 。

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

文档的大小由 QLabels 的固定大小决定。 QPixmap 设置为相应的伸展, 而 QFrame 周围的页面会自然调整其大小 。

当我要求缩放时, 页面( Qlabels) 会用 < codeLabel. setFixedSize () 重新缩放。 效果是 < em> dispecting : 重新缩放文档时, 看起来像是摇摆和闪烁。 在 Evince 或 Mendeley 的缩放非常顺利 。

我在其他地方读到过,在缩放前拨打,在帮助后拨打。事实上,它适用于一个小文件。但是,例如,适用于大约700页的PDF, 意味着“em>blank

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.

最佳回答

经过一些调查和来自 QtForums 的帮助后, 似乎有< 坚固>, 没有简单的解决办法可以实现QScrollArea < / strong > 想要的缩放效果。 请证明我错了 。

更好的工具是 < a href=" http://qt- project.org/doc/qt/ qt-4.8/qgraphicsview.html" rel="nofollow"\"codeGraphicsView 。它有一个 < a href=""http://qt-project.org/doc/qt-4.8/qgraphicsview.html#scale" rel="nofollow"\codeGraphicsView.sale(float, fload) 的方法,它完美地使缩放效果变得完美!它也更容易使用,因为这一比例方法甚至不需要维护内容(如果你从一开始不加载 hi-res,也许图像的分辨率除外)。

,然而,QGraphicsView的内容有点懒惰,需要做更多的工作来设置,特别是在布局方面。就我而言,由于我正在制作一个PDF查看器,我需要垂直对齐的图像,而可以做。

为了子孙,这里有一个示例代码, 显示如何在布局、 场景和视图中执行像素映射。 当然, 它也显示如何缩放内容 。

#!/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_())
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签