在PySide中QDeclarativeView變成使用OpenGL來渲染

出自GaryLee
跳轉到: 導覽, 搜尋

假設你已經有一個使用QDeclarativeView的程式,你覺得顯示效能始終不夠滿意。


from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *

class MyView(QDeclarativeView):
	def __init__(self, src):
		super(MyView, self).__init__()
		self.setAttribute(Qt.WA_TranslucentBackground)
		self.viewport().setAutoFillBackground(False)
		self.setWindowTitle('My QML Windows')
		self.setSource(src)
		self.setResizeMode(QDeclarativeView.SizeRootObjectToView)

app = QApplication([])
view = MyView(src="MyView.qml")
view.show()
app.exec_()

稍微加入一點code就可以使用OpenGL。

from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtOpenGL import *
from PySide.QtDeclarative import *

class MyView(QDeclarativeView):
	def __init__(self, src, useOpenGL=False):
		super(MyView, self).__init__()
		if useOpenGL:
			format = QGLFormat.defaultFormat()
			format.setSampleBuffers(False)        
			glw = QGLWidget(format)
			glw.setAutoFillBackground(False)
			self.setViewport(glw)
		self.setAttribute(Qt.WA_TranslucentBackground)
		self.viewport().setAutoFillBackground(False)	
		self.setWindowTitle('My QML Windows')
		self.setSource(src)
		self.setResizeMode(QDeclarativeView.SizeRootObjectToView)			
		
app = QApplication([])
view = MyView(src="MyView.qml", useOpenGL=True)
view.show()
app.exec_()

自己比較一下兩個程式的差異吧!

個人工具