Editing PyQt Tips and Tricks

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 91: Line 91:
</source>
</source>
[http://talk.maemo.org/showpost.php?p=714507&postcount=1]
[http://talk.maemo.org/showpost.php?p=714507&postcount=1]
-
 
-
=== Instant startup screen ===
 
-
 
-
As most of us already know most PyQt4 apps are taking quite sometimes at start up and most of the time users are getting initial black screen while it's loading, this is just not only the case with Python apps but all the apps on [[Nokia N900|N900]]. Nokia has used a trick to snapshot of apps start up screen and load image instantly when the app icon is pressed, which give users an impression that apps was started up instantly but in reality they were not. (modest, note, sketch, control panel, application manager, pdfviewer, worldclock, xterm, and calculator are using this trick). So why don't we apply the same trick on our apps to make it look fast at startup? Well, Most of us don't even know the trick is there and some debates whether this is a good idea or not, but I for one rather see a false startup image than a black screen. So below is the instruction to make your Python Qt app fly at startup on N900 device.
 
-
 
-
'''There are three basic things to make it work:'''
 
-
 
-
<ol>
 
-
<li>
 
-
A D-Bus service file (name whatever e.g. yourapp.service) in "<code>/usr/share/dbus-1/services/</code>" folder even though yourapp is not using D-Bus related service at all and it should look like this format in <code>yourapp.service</code>.
 
-
<pre>
 
-
[D-BUS Service]
 
-
Name=what.ever.yourapp
 
-
Exec=/opt/yourapp/main.py
 
-
</pre>
 
-
</li>
 
-
<li>
 
-
Adding "<code>X-Osso-Service=what.ever.yourapp</code>" in your yourapp.desktop file in "<code>/usr/share/applications/hildon/</code>" folder, something like this.
 
-
<pre>
 
-
[Desktop Entry]
 
-
Encoding=UTF-8
 
-
Version=1.0
 
-
Type=Application
 
-
Terminal=false
 
-
Name=YourApp
 
-
Icon=YourApp
 
-
Exec=/opt/yourapp/main.py
 
-
X-Osso-Service=what.ever.yourapp
 
-
</pre>
 
-
</li>
 
-
<li>
 
-
Lastly, A function code in your app to take a snapshot of your app startup screen when it starts up the first time or when it exits whichever your prefer. Below is an basic example of PyQt4 app code which take a snapshot of the screen at start up if it's not already exist and save it into '"<code>/home/user/.cache/launch/what.ever.yourapp.pvr</code>" (p.s. the file name has to match with your <code>X-Osso-Service</code>, and D-BUS Service Name plus .pvr extension).
 
-
<source lang="python">
 
-
#!/usr/bin/env python2.5
 
-
# -*- coding: utf-8 -*-
 
-
 
-
from PyQt4.QtGui import *
 
-
from PyQt4.QtCore import *
 
-
 
-
import osso # only needed for osso_initialize
 
-
 
-
class MainWindow(QMainWindow):
 
-
    def __init__(self, parent = None):       
 
-
        self.dbus_service_name = "what.ever.yourapp"  #must be the same as your X-osso-service in yourapp.desktop file
 
-
        """
 
-
        self.osso_c = osso.Context(self.dbus_service_name, "0.0.1", False)
 
-
        is only needed if your app is not launched from run-standalone.sh,
 
-
        Otherwise, your app will be terminated after ~ 2mins running
 
-
        don't ask me why :)
 
-
        """
 
-
        self.osso_c = osso.Context(self.dbus_service_name, "0.0.1", False)
 
-
       
 
-
        QMainWindow.__init__(self, parent)
 
-
 
-
        self.centralwidget = QWidget(self)
 
-
        self.setCentralWidget(self.centralwidget)
 
-
 
-
        """
 
-
        the rest of your code here....
 
-
        """
 
-
 
-
    def takeScreenShot(self):
 
-
        pvr = "/home/user/.cache/launch/%s.pvr" % self.dbus_service_name
 
-
        if not QFile.exists(pvr): # skipped if it's already there
 
-
            QPixmap.grabWidget(self.centralwidget).save(pvr, 'png') # tell it to grab only your self.centralwidget screen, which is just window screen without the menu status bar on top.
 
-
 
-
if __name__ == '__main__':
 
-
    import sys
 
-
    app = QApplication(sys.argv)
 
-
    app.setApplicationName("YourAppName")
 
-
    YourApp = MainWindow()
 
-
    YourApp.show()
 
-
    YourApp.takeScreenShot() # run this after the YourApp.show()
 
-
    sys.exit(app.exec_())
 
-
</source>
 
-
</li>
 
-
</ol>
 
== Example Applications ==
== Example Applications ==

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)