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()
    w.setLayout(l)
    #add default widgets
    #l.addWidget()
    #display the mainwindow on startup
    w.show()
    #launch the app
    app.exec_()
    return app;
#this did not need to be wrapped in a main
#function but it is cleaner to do so
main()

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()
        app.exec_()
        
'''
#usage example
def main():
    w = QtGui.QFrame()
    w.show()
    return w
    
QtStandalone(main)
'''

Leave a Reply

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