父级中的 Python 加载模块以防止覆盖问题



我正在用Python构建一个QGIS插件,并为它设计了一个GUI。我可以用 pyuic4 编译它,但在加载时它会出错。我发现我可以通过在编译的 Python 代码中添加下面的行来防止该错误。只有在某些时候我必须重新编译,因此文件被覆盖并且该行丢失。

form.py

from qgis.gui import QgsMapLayerComboBox

我有一个"父"文件,它导入编译版本,如下所示:

dialog.py

from form import Ui_Dialog

有没有办法在 dialog.py 中导入QgsMapLayerComboBox,这样我就不必每次都将其添加到重新编译GUI后 form.py?

编辑:

<widget class="QgsMapLayerComboBox" name="mMapLayerComboBox">
 <property name="geometry">
  <rect>
   <x>100</x>
   <y>18</y>
   <width>160</width>
   <height>22</height>
  </rect>
 </property>
 <property name="filters">
  <set>QgsMapLayerProxyModel::RasterLayer</set>
 </property>
</widget>
</widget>
<customwidgets>
<customwidget>
 <class>QgsMapLayerComboBox</class>
 <extends>QComboBox</extends>
 <header>qgsmaplayercombobox.h</header>
</customwidget>
</customwidgets>

使用一些文本编辑器打开 form.ui 并替换:

<customwidget>
 <class>QgsMapLayerComboBox</class>
 <extends>QComboBox</extends>
 <header>qgsmaplayercombobox.h</header>
</customwidget>

<customwidget>
 <class>QgsMapLayerComboBox</class>
 <extends>QComboBox</extends>
 <header>qgis.gui</header>
</customwidget>

并再次编译。

最新更新