Replace QWidget objects at runtime

Don’t replace the widgets at runtime: promote the widgets in Qt Designer so that the line-edits are automatically replaced by your custom class when the GUI module is generated.

Here’s how to promote a widget to use a custom class:

In Qt Designer, select all the line-edits you want to replace, then right-click them and select “Promote to…”. In the dialog, set “Promoted class name” to “LineEdit”, and set “Header file” to the python import path for the module that contains this class (e.g. myapp.LineEdit). Then click “Add”, and “Promote”, and you will see the class change from “QLineEdit” to “LineEdit” in the Object Inspector pane.

Now when you re-generate your ui module with pyuic, you should see that it uses your custom LineEdit class and there will be an extra line at the bottom of the file like this:

    from myapp.LineEdit import LineEdit

Leave a Comment