python - 在QGraphicsView的wheelEvent中,self.scale不允许self.scrollContentsBy工作
问题描述
a 的内容的缩放和滚动QGraphicsView
都应用于wheelEvent
. 问题是滚动
只有在. 在缩放和滚动调用之间
应用没有成功。wheelEvent
self.update()
额外的点和猜测
如果注释掉滚动部分,比例在它的位置上准确地工作,没有移动多次上下滚动但滚动部分没有注释掉,多次滚动(多次放大和缩小)有一点位移观点已被观察到。
调用 ui 元素的触发方法似乎存在冲突。
感谢您分享的知识
# importing libraries
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *
import sys
class Scene(QGraphicsScene):
def __init__(self, _scene_width, _scene_height):
super(Scene, self).__init__()
self.setSceneRect(0, 0, _scene_width, _scene_height)
map_center = _scene_width / 2
te = QTextEdit()
te.insertPlainText("Test Text")
te.setGeometry(map_center, map_center, 100, 100)
self.addWidget(te)
class View(QGraphicsView):
def __init__(self, _scene_width, _scene_height):
super(View, self).__init__()
self.scale_factor = 1
self.scene_width = _scene_width
self.scene_height = _scene_height
scene = Scene(_scene_width, _scene_height)
self.setScene(scene)
self.showMaximized()
def wheelEvent(self, event):
if self.underMouse():
# scale part which doesn't let the scroll part to work
if event.angleDelta().y() > 0:
self.scale_factor = self.scale_factor * 1.2
self.scale(1.2, 1.2)
else:
self.scale_factor = self.scale_factor / 1.2
self.scale(1 / 1.2, 1 / 1.2)
# scroll part which works fine if the scale part is commented out
self.scrollContentsBy(10, 0) # values are just for testing
def main():
app = QApplication(sys.argv)
view = View(1000000, 1000000)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
解决方案
推荐阅读
- reactjs - 按钮 onclick 在 reactJS 中不起作用,代码如下
- java - 由于拦截器异常,Spring 写入中断响应
- raspberry-pi - 拔下局域网电缆时,Raspberry Wifi 失去连接
- android - 从 android studio 的内部存储中删除列表
- android - PendingIntent 存储在哪里?使用的大陷阱
- css - Firefox CSS 渲染问题
- python - discord_slash:如何实际添加斜线命令/为什么我的不起作用?
- python - Python moviepy 正在生成一个奇怪的视频
- css - 如何将 bulma navbar burger 对齐到页面的右端?
- wordpress - 在通过 hook/php 加载 Woocommerce 感谢页面的内容后,有什么方法可以运行代码?