同步图像显示与屏幕刷新率
问题描述:
程序的功能:使用PyQt4来显示图像(简单的jpg/png文件)。同步图像显示与屏幕刷新率
目标:在屏幕上显示/绘制图像,与屏幕刷新率同步。
一个伪代码样品我想达到的目标:
pixmap = set_openGL_pixmap(myPixmap)
draw_openGL_pixmap(pixmap)
doSomthingElse()
理想情况下,draw_openGL_pixmap(pixmap)
功能只能在屏幕已刷新,显示的图像后返回。在真正绘制图像之后,将立即执行doSomthingElse()
。
是我到目前为止已经试过:
- 使用PyQt's
QApplication.processEvents()
像素图设置为PyQt的标签后:这似乎并没有给把戏,因为它不处理与屏幕刷新率同步。 - 使用QGLFormat.setSwapInterval():尽管这应该工作的文件表明,PyQt的并不在屏幕上绘制图像,直至
QApplication.processEvents()
被调用,或者直到控制权返回给应用程序的事件循环(即当所有的我调用的函数已经返回并且GUI正在等待新事件)。 - 使用QGraphicsView - 即使使用OpenGL窗口小部件呈现图像,只有在显示父窗口时才会显示图像,因此实际显示时间仍取决于事件循环。
- 使用QWidget.repaint() -
repaint()
方法将使图像立即显示。但是,我不认为当调用repaint()
时,它会等到屏幕刷新事件返回之前。
汇总: 我怎样才能使PyQt在精确的时刻,我发出的指令在屏幕上绘制的图像(在小部件),与屏幕刷新率同步,无论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_())
也看到http://stackoverflow.com/questions/17167194/how-to-make-updategl -realtime-in-qt – Trilarion 2015-02-09 14:00:02
@Trilarion谢谢!那就是诀窍 – 2015-02-11 14:40:26