跳到主要內容

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

假設你已經有一個使用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_()

其實,需要的工作遠比你想像的容易。只要稍作修改,就可以透過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_()



留言

這個網誌中的熱門文章

Portable Python

我常常需要把Python寫的script帶到其他電腦使用,因此,一個免安裝,可攜帶的Python就顯得十分重要。最近看過了幾個可攜式Python的方案,下面這個PortablePython是我覺得最合我意的方案。因為它提供了大部分會用到的Python module及工具,甚至連wxPython及PyGame也有。同時也有好用的Python編輯器PyScripter。所有開發Python所需的開發工具都一應俱全了!把它放到隨身碟中,就不用到處幫人安裝Python了。 PortablePython : http://www.portablepython.com/

一個Python程式可幫檔名加上日期與時間

很多時候,我們希望能夠將檔案或是目錄名稱加上一個時間及日期,以便release。所以,我就寫了一個小小的程式來達到這個目的。我把這個程式貼上來,讓有興趣的人可以拿去使用。 -- #!/usr/bin/env python # -*- coding: ascii -*- """ Usage: cfgfn.py [filename or directory list] """ import sys import os import time import re import glob ro = re.compile(r'(?P<FN> .*)-[0-9]{8}-[0-9]{4}(?P<EXT> .*)') for fnl in sys.argv[1:]: for fn in glob.glob(fnl): mo = ro.match(fn) if mo: pre = mo.group('FN') ext = mo.group('EXT') else: pre, ext = os.path.splitext(fn) newFn = pre + time.strftime('-%Y%m%d-%H%M') + ext os.rename(fn, newFn) print 'Rename %s -> %s' % (fn, newFn)