PyQt standalone

Useful from testing within eclipse

import sys
from PyQt4 import QtCore, QtGui

def main():
    #construct the application before any other objects
    app = QtGui.QApplication(sys.argv)
    #setup the default state of the application upon launch
    w = QtGui.QFrame() #treating this frame as mainwindow
    #layout for the mainwindow
    l = QtGui.QVBoxLayout()
    #add default widgets
    #display the mainwindow on startup
    #launch the app
    return app;
#this did not need to be wrapped in a main
#function but it is cleaner to do so

Or an even better example, import it into any window class to test it at the bottom, simply change what w contains.

import sys
from PyQt4 import QtGui

class QtStandalone:
    def __init__(self, mainfunction):
        app = QtGui.QApplication(sys.argv)
        alive = mainfunction()
#usage example
def main():
    w = QtGui.QFrame()
    return w

Leave a Reply

Your email address will not be published. Required fields are marked *