同步图像显示与屏幕刷新率
Syncing image display with screen refresh rate
程序的作用:使用PyQt4显示图像(简单的jpg/png文件)。
objective: 使屏幕上的图像 displayed/drawn 与屏幕的刷新率同步。
我想要实现的伪代码示例:
pixmap = set_openGL_pixmap(myPixmap)
draw_openGL_pixmap(pixmap)
doSomthingElse()
理想情况下,draw_openGL_pixmap(pixmap)
函数应该只在屏幕刷新并显示图像后 return。比 doSomthingElse()
会在真正绘制图像后立即执行。
到目前为止我尝试了什么:
- 在将像素图设置为 PyQt 标签后使用 PyQt's
QApplication.processEvents()
: 这似乎不是诀窍,因为它不处理与屏幕的刷新率。
- Using QGLFormat.setSwapInterval() : Even though this should work as the documentation suggests, PyQt doesn't draw the image on the screen until
QApplication.processEvents()
被调用,或者直到控件被 returned 到应用程序的事件循环(即,当我调用的所有函数都被 returned 并且 GUI正在等待新活动)。
- 使用 QGraphicsView - Even though the image is rendered using an OpenGL widget, it is only visible once it's parent is displayed, so the actual time of display still depends on PyQt's 事件循环。
- 使用QWidget.repaint() -
repaint()
方法将使图像立即显示。但是,我不认为调用 repaint()
时,它会等到 return 之前的屏幕刷新事件。
总结:
我如何制作 PyQt draw the image on the screen (in a widget) at the exact moment i issue the command, in sync with the screen refresh rate, regardless of PyQt's 事件循环。
感谢 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_())
程序的作用:使用PyQt4显示图像(简单的jpg/png文件)。
objective: 使屏幕上的图像 displayed/drawn 与屏幕的刷新率同步。
我想要实现的伪代码示例:
pixmap = set_openGL_pixmap(myPixmap)
draw_openGL_pixmap(pixmap)
doSomthingElse()
理想情况下,draw_openGL_pixmap(pixmap)
函数应该只在屏幕刷新并显示图像后 return。比 doSomthingElse()
会在真正绘制图像后立即执行。
到目前为止我尝试了什么:
- 在将像素图设置为 PyQt 标签后使用 PyQt's
QApplication.processEvents()
: 这似乎不是诀窍,因为它不处理与屏幕的刷新率。 - Using QGLFormat.setSwapInterval() : Even though this should work as the documentation suggests, PyQt doesn't draw the image on the screen until
QApplication.processEvents()
被调用,或者直到控件被 returned 到应用程序的事件循环(即,当我调用的所有函数都被 returned 并且 GUI正在等待新活动)。 - 使用 QGraphicsView - Even though the image is rendered using an OpenGL widget, it is only visible once it's parent is displayed, so the actual time of display still depends on PyQt's 事件循环。
- 使用QWidget.repaint() -
repaint()
方法将使图像立即显示。但是,我不认为调用repaint()
时,它会等到 return 之前的屏幕刷新事件。
总结: 我如何制作 PyQt draw the image on the screen (in a widget) at the exact moment i issue the command, in sync with the screen refresh rate, regardless of PyQt's 事件循环。
感谢 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_())