Skip to content Skip to sidebar Skip to footer

Replace Qwidget Objects At Runtime

In my application I have to replace all QLineEdit elements with customized QLineEdit. To do that there are different solutions: Modify the generated py file from pyuic4 and replac

Solution 1:

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.LineEditimportLineEdit

Solution 2:

I didn't go through all of your code...

My guess though is, that even though you've replaced the widget in the layout, window.txt_line_1 still points to the deleted object. So, in your replacement-procedure you'll also have to update this attribute.

So, adding

setattr(parent, obj_name, qt_obj);

to _replace_qlineedit_from_gridlayout might do the trick.

Post a Comment for "Replace Qwidget Objects At Runtime"