【问题标题】:Syncing image display with screen refresh rate图像显示与屏幕刷新率同步
【发布时间】:2015-04-08 22:10:40
【问题描述】:

程序的作用:使用PyQt4 显示图像(简单的jpg/png 文件)。

目标:在屏幕上显示/绘制与屏幕刷新率同步的图像。

我想要实现的伪代码示例:

pixmap = set_openGL_pixmap(myPixmap) 

draw_openGL_pixmap(pixmap) 

doSomthingElse()

理想情况下,draw_openGL_pixmap(pixmap) 函数应仅在屏幕刷新并显示图像后返回。比doSomthingElse() 将在图像真正绘制后立即执行。

到目前为止我所做的尝试

  • 在将像素图设置为 PyQt 标签后使用 PyQt's QApplication.processEvents() : 这似乎不是诀窍,因为它不处理与屏幕刷新率同步的问题。
  • 使用QGLFormat.setSwapInterval() : 尽管这应该像文档建议的那样工作,PyQt 不会在屏幕上绘制图像,直到调用QApplication.processEvents(),或者直到控件返回到应用程序的事件循环(即,当我调用的所有函数都返回并且 GUI 正在等待新事件时)。
  • 使用QGraphicsView - 尽管图像是使用 OpenGL 小部件渲染的,但它只有在显示其父级时才可见,因此实际显示时间仍取决于 PyQt's 事件循环。
  • 使用QWidget.repaint() - repaint() 方法将导致图像立即显示。但是,我不认为当repaint() 被调用时,它会等到屏幕刷新事件才返回。

总结: 无论PyQt's 事件循环如何,我如何让PyQt 在我发出命令的确切时刻在屏幕上(在小部件中)绘制图像,与屏幕刷新率同步。

【问题讨论】:

标签: opengl pyqt pyqt4 pyopengl


【解决方案1】:

感谢 Trialarion 对我的问题的评论,我找到了解决方案 here

对于任何感兴趣的人,这里是显示与屏幕刷新率同步的图像的 python 代码:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtOpenGL import *

app = QApplication(sys.argv)

# Use a QGLFormat with the swap interval set to 1
qgl_format = QGLFormat()
qgl_format.setSwapInterval(1)

# Construct a QGLWidget using the above format
qgl_widget = QGLWidget(qgl_format)

# Set up a timer to call updateGL() every 0 ms
update_gl_timer = QTimer()
update_gl_timer.setInterval(0)
update_gl_timer.start()
update_gl_timer.timeout.connect(qgl_widget.updateGL)

# Set up a graphics view and a scene
grview = QGraphicsView()
grview.setViewport(qgl_widget)
scene = QGraphicsScene()
scene.addPixmap(QPixmap('pic.png'))
grview.setScene(scene)

grview.show()

sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多