Replace QWidget objects at runtime

前端 未结 2 1274
耶瑟儿~
耶瑟儿~ 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
    

提交回复
热议问题