2015-02-09 101 views
2

程序的功能:使用PyQt4來顯示圖像(簡單的jpg/png文件)。同步圖像顯示與屏幕刷新率

目標:在屏幕上顯示/繪製圖像,與屏幕刷新率同步。

一個僞代碼樣品我想達到的目標:

pixmap = set_openGL_pixmap(myPixmap) 

draw_openGL_pixmap(pixmap) 

doSomthingElse() 

理想情況下,draw_openGL_pixmap(pixmap)功能只能在屏幕已刷新,顯示的圖像後返回。在真正繪製圖像之後,將立即執行doSomthingElse()

是我到目前爲止已經試過

  • 使用PyQt'sQApplication.processEvents()像素圖設置爲PyQt的標籤後:這似乎並沒有給把戲,因爲它不處理與屏幕刷新率同步。
  • 使用QGLFormat.setSwapInterval()儘管這應該工作的文件表明,PyQt的並不在屏幕上繪製圖像,直至QApplication.processEvents()被調用,或者直到控制權返回給應用程序的事件循環(即當所有的我調用的函數已經返回並且GUI正在等待新事件)。
  • 使用QGraphicsView - 即使使用OpenGL窗口小部件呈現圖像,只有在顯示父窗口時纔會顯示圖像,因此實際顯示時間仍取決於事件循環。
  • 使用QWidget.repaint() - repaint()方法將使圖像立即顯示。但是,我不認爲當調用repaint()時,它會等到屏幕刷新事件返回之前。

彙總: 我怎樣才能使PyQt在精確的時刻,我發出的指令在屏幕上繪製的圖像(在小部件),與屏幕刷新率同步,無論PyQt's事件循環。

+1

也看到http://stackoverflow.com/questions/17167194/how-to-make-updategl -realtime-in-qt – Trilarion 2015-02-09 14:00:02

+0

@Trilarion謝謝!那就是訣竅 – 2015-02-11 14:40:26

回答

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_())