Replace QWidget objects at runtime

前端 未结 2 1264
耶瑟儿~
耶瑟儿~ 2020-11-30 14:56

In my application I have to replace all QLineEdit elements with customized QLineEdit. To do that there are different solutions:

  1. Modify the generated py file fr
相关标签:
2条回答
  • 2020-11-30 15:16

    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
    
    0 讨论(0)
  • 2020-11-30 15:33

    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.

    0 讨论(0)
提交回复
热议问题