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?
Share
First, use a QFileDialog or more conveniently the static function QFileDialog.getOpenFileName to let the user pick the
.pyfile 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 (assumingfilepathis theQStringwith the path to the file, and that you’re using a filesystem/platform with natively Unicode filenames — otherwise you’ll have to add whatever.encodeyour 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
importfromabove), 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.