I want to make a PyQt4 program that supports plugins. Basically I want the user to be able to write QWidget subclasses in PyQt4 and add/remove them from the main application window via a GUI. How would I do that, especially the plugin mechanism?
There are 2 answers
Have a directory for the plugins, define an interface for those plugins, and walk the directory importing them.
I've never done this in Python, but in C the way I did it was to define a number of functions that the plugin needed to implement. The basic key elements that are necessary is the name of the things in the plugin (so that you can display them in your UI so that the user can instantiate them) and a factory to actually create the things. I suppose in Python it'd be pretty trivial to just do those as one dict that you can return from the module.
E.g. declare that all plugins must author a function called GetPluginInfo() which returns a dictionary of name:class mappings.
Actually now that I think about it you could probably just import the file and look for classes that are subclasses of whatever you care about and not require any explicit API be implemented. I haven't done a lot of that but basically you'd walk the module's dir() and test to see if each thing is a subclass of QWidget (for example).
First, use a QFileDialog or more conveniently the static function QFileDialog.getOpenFileName to let the user pick the
.py
file they want to "plug into" your GUI.Next, if you want to allow importing the plugin from anywhere on the disk (as opposed to, from specific directories previously added to your
sys.path
, e.g. via the environment variablePYTHONPATH
), you can "do it right" (a non-negligible amount of work), via the imp module in the standard Python library), or you can "cut corners" as follows (assumingfilepath
is theQString
with the path to the file, and that you're using a filesystem/platform with natively Unicode filenames -- otherwise you'll have to add whatever.encode
your platform and filesystem require)...:This isn't thread-safe because it may have a side effect on
sys.path
, which is why I said it's "cutting corners"... but then, making thread-safe imports (and "clean" imports from "anywhere") is really hard work (probably worth a separate question if you need to, since it really has nothing much to do with the gist of this one, or PyQt, &c).Once you do have the module object (the result from
importfrom
above), I suggest:This returns a list with all the widget classes (if any) defined (at the top level) in module
amodule
.What you want to do next is up to you, of course. Perhaps you want to give some kind of error messages if the list is empty (or maybe also if it has more than one item? or else how do decide which widget class to use?) or else instantiate the widget class (how many times? At what coordinates? And so on -- questions only you can answer) and show the resulting widget(s) in the appropriate spot(s) on your window.