Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1032283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:03:21+00:00 2026-05-16T14:03:21+00:00

I want to make a PyQt4 program that supports plugins. Basically I want the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:03:21+00:00Added an answer on May 16, 2026 at 2:03 pm

    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 variable PYTHONPATH), 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 (assuming filepath is the QString 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)…:

    import sys, os
    
    def importfrom(filepath):
        ufp = unicode(filepath)
        thedir, thefile = os.path.split(ufp)
        if sys.path[0] != thedir:
          sys.path.insert(0, thedir)
        themodule, theext = os.path.splitext(thefile)
        return __import__(themodule)
    

    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:

    from PyQt4 import QtGui 
    import inspect
    
    def is_widget(w):
        return inspect.isclass(w) and issubclass(w, QtGui.QWidget)
    
    def all_widget_classes(amodule):
        return [v for n, v in inspect.getmembers(amodule, is_widget)]
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.