python - 以编程方式滚动 QGraphicsView

标签 python qt

我想在我的 (Py)Qt 应用程序中的 QGraphicsView 上实现滚动/平移功能。它应该是这样工作的:用户按下鼠标中键, View 随着用户移动鼠标而滚动(这是一个非常常见的功能)。
我尝试使用从 QWidget 继承的 scroll() 方法。但是,这会以某种方式移动 View - 滚动条和所有内容。见图片。
因此,鉴于这不是我应该这样做的方式,我该怎么办?或者这是正确的方法,但我做错了什么?我使用的代码:

   def __init__(self):
       ...

       self.ui.imageArea.mousePressEvent=self.evImagePress
       self.ui.imageArea.mouseMoveEvent=self.evMouseMove

       self.scrollOnMove=False
       self.scrollOrigin=[]

       ...

   def evImagePress(self, event):
        if event.button() == Qt.LeftButton:
            self.evImageLeftClick(event)
        if event.button() == Qt.MidButton:
            self.scrollOnMove=not self.scrollOnMove
            if self.scrollOnMove:
                self.scrollOrigin=[event.x(), event.y()]
   ...

   def evMouseMove(self, event):
        if self.scrollOnMove:
            self.ui.imageArea.scroll(event.x()-self.scrollOrigin[0],
                                     event.y()-self.scrollOrigin[1])

它按我的预期工作,除了整个移动小部件业务。

Fails to scroll http://img55.imageshack.us/img55/3222/scrollfail.jpg

最佳答案

我自己没有这样做过,但这是来自 QGraphicsView 文件

... When the scene is larger than the scroll bars' values, you can choose to use translate() to navigate the scene instead.



通过使用 scroll您正在移动小部件, translate 应该实现您正在寻找的内容,移动 QGraphicsScene 的内容在 View 下方

关于python - 以编程方式滚动 QGraphicsView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1108477/

相关文章:

python - cx_Oracle.DatabaseError : DPI-1047: 64-bit Oracle Client library cannot be loaded: "dlopen(libclntsh.dylib, 1): image not found"

c++ - 不同来源的QT调用函数

c++ - 在 QT 中使用 LTI-Lib

python - 逻辑回归 : Unknown label type: 'continuous' using sklearn in python

python - OpenSSL s_client 和 Python SSL 模块在主机名证书上存在分歧

python - Google App Engine 套接字权限被拒绝

python - 谁能解释如何使用OpenCV在Raspberry Pi上从kinect保存RGB图像?

javascript - QT QWebEngine 滚动后渲染?

c++ - 如何正确地将带有 vtable 的类实例写入和读取到 QSharedMemory 中?

c++ - QMainWindow 从 main() 函数打开,而不是从任何其他函数打开