PyQGIS some GUIs(e.g. QgsMapLayerComboBox,QgsFieldComboBox) don’t work in custom application

I’m trying to make a custom application based on PyQGIS.I have made QGIS plugin and I used QgsMapLayerComboBox,QgsFieldComboBox and some other QGSI custom GUI widget.

I want to use them in my custom application,too.But the same .ui file worked different between QGIS plugin and PyQGIS custom application.It only shows basic PyQT5.ComboBox in custom application.

Here comes the content of the .ui file and the main code of the dialog show.

.ui file

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>SpatialClusterDialogBase</class>
 <widget class="QDialog" name="SpatialClusterDialogBase">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>630</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>空间分区</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_6">
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout" stretch="2,2,8">
     <item>
      <widget class="QLabel" name="label">
       <property name="text">
        <string>输入图层</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QgsMapLayerComboBox" name="in_layer_combo"/>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_3" stretch="2,4,6">
     <item>
      <widget class="QLabel" name="label_8">
       <property name="text">
        <string>标识字段</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_3">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QgsFieldComboBox" name="in_id_combo"/>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="8,6,6">
     <item>
      <widget class="QLabel" name="label_7">
       <property name="text">
        <string>要素间距离确定方法</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_2">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QComboBox" name="in_featureGeom_combo">
       <item>
        <property name="text">
         <string>self</string>
        </property>
       </item>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <widget class="Line" name="line">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
    </widget>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_4" stretch="2,2,8">
     <item>
      <widget class="QLabel" name="label_9">
       <property name="text">
        <string>成本路径图层</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_4">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QgsMapLayerComboBox" name="in_od_combo"/>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QHBoxLayout" name="horizontalLayout_5">
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_7">
       <item>
        <widget class="QLabel" name="label_10">
         <property name="text">
          <string> 起点标识字段</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QgsFieldComboBox" name="in_origin_combo"/>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_8">
       <item>
        <widget class="QLabel" name="label_11">
         <property name="text">
          <string>终点标识字段</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QgsFieldComboBox" name="in_dest_combo"/>
       </item>
      </layout>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_9">
       <item>
        <widget class="QLabel" name="label_12">
         <property name="text">
          <string>成本字段</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QgsFieldComboBox" name="in_cost_combo"/>
       </item>
      </layout>
     </item>
    </layout>
   </item>
   <item>
    <widget class="Line" name="line_2">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
    </widget>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="label_2">
       <property name="text">
        <string>最终距离确定方法</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QComboBox" name="in_method_combo">
       <item>
        <property name="text">
         <string>average</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>single</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>complete</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>centroid</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>ward</string>
        </property>
       </item>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
      <widget class="QLabel" name="label_3">
       <property name="text">
        <string>距离计算方法</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QComboBox" name="in_metric_combo">
       <item>
        <property name="text">
         <string>euclidean</string>
        </property>
       </item>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_3">
     <item>
      <widget class="QLabel" name="label_4">
       <property name="text">
        <string>分区准则</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QComboBox" name="in_criterion_combo">
       <item>
        <property name="text">
         <string>distance</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>maxclust</string>
        </property>
       </item>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_4">
     <item>
      <widget class="QLabel" name="label_5">
       <property name="text">
        <string>准则阈值</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QLineEdit" name="in_t_lineEdit"/>
     </item>
    </layout>
   </item>
   <item>
    <widget class="Line" name="line_3">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
    </widget>
   </item>
   <item>
    <layout class="QVBoxLayout" name="verticalLayout_5">
     <item>
      <widget class="QLabel" name="label_6">
       <property name="text">
        <string>分区结果</string>
       </property>
      </widget>
     </item>
     <item>
      <widget class="QgsFileWidget" name="out_result">
       <property name="dialogTitle">
        <string>结果保存为</string>
       </property>
      </widget>
     </item>
    </layout>
   </item>
   <item>
    <widget class="QDialogButtonBox" name="button_box">
     <property name="orientation">
      <enum>Qt::Horizontal</enum>
     </property>
     <property name="standardButtons">
      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QgsFieldComboBox</class>
   <extends>QComboBox</extends>
   <header>qgsfieldcombobox.h</header>
  </customwidget>
  <customwidget>
   <class>QgsFileWidget</class>
   <extends>QWidget</extends>
   <header>qgsfilewidget.h</header>
  </customwidget>
  <customwidget>
   <class>QgsMapLayerComboBox</class>
   <extends>QComboBox</extends>
   <header>qgsmaplayercombobox.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections>
  <connection>
   <sender>button_box</sender>
   <signal>accepted()</signal>
   <receiver>SpatialClusterDialogBase</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>20</x>
     <y>20</y>
    </hint>
    <hint type="destinationlabel">
     <x>20</x>
     <y>20</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>button_box</sender>
   <signal>rejected()</signal>
   <receiver>SpatialClusterDialogBase</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>20</x>
     <y>20</y>
    </hint>
    <hint type="destinationlabel">
     <x>20</x>
     <y>20</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

main code

dialog.py

import os

from qgis.PyQt import uic
from qgis.PyQt import QtWidgets
from qgis.core import (
QgsMapLayerProxyModel,
QgsFieldProxyModel,
)
from qgis.gui import(
QgsFileWidget,
QgsMapLayerComboBox,
QgsFieldComboBox,
)


# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'forms/SpatialCluster_dialog_base.ui'))

class SpatialClusterDialog(QtWidgets.QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(SpatialClusterDialog, self).__init__(parent)
        # Set up the user interface from Designer through FORM_CLASS.
        # After self.setupUi() you can access any designer object by doing
        # self.<objectname>, and you can use autoconnect slots - see
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
        # #widgets-and-dialogs-with-auto-connect
        self.setupUi(self)
        self.init_ui()

    def init_ui(self):
        # 设置图层下拉框过滤器
        self.in_layer_combo.setFilters(QgsMapLayerProxyModel.PointLayer) # 待分区点图层
        self.in_od_combo.setFilters(QgsMapLayerProxyModel.LineLayer) # 成本矩阵路径图层

        # 允许线图层下拉框为空
        self.in_od_combo.setAllowEmptyLayer(True)

        # 设置属性下拉框过滤器
        self.in_id_combo.setFilters(QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong) # 点ID字段
        self.in_origin_combo.setFilters(QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong) # 起点ID字段
        self.in_dest_combo.setFilters(QgsFieldProxyModel.Int | QgsFieldProxyModel.LongLong) # 终点ID字段
        # 允许id相关字段为空
        self.in_id_combo.setAllowEmptyFieldName(True)
        self.in_origin_combo.setAllowEmptyFieldName(True)
        self.in_dest_combo.setAllowEmptyFieldName(True)
        # 图层下拉框连接属性选择框
        # 初始化
        if self.in_layer_combo.currentLayer():
            self.in_id_combo.setLayer(self.in_layer_combo.currentLayer())
        if self.in_od_combo.currentLayer():
            self.in_origin_combo.setLayer(self.in_od_combo.currentLayer())
            self.in_dest_combo.setLayer(self.in_od_combo.currentLayer())
            self.in_cost_combo.setLayer(self.in_od_combo.currentLayer())
        # 连接图层切换事件
        self.in_layer_combo.layerChanged.connect(self.in_id_combo.setLayer)
        self.in_od_combo.layerChanged.connect(self.in_origin_combo.setLayer)
        self.in_od_combo.layerChanged.connect(self.in_dest_combo.setLayer)
        self.in_od_combo.layerChanged.connect(self.in_cost_combo.setLayer)
        self.in_layer_combo.layerChanged.connect(self.slot_layerCombo_layerChanged)

        # 设置导出文件框模式为保存文件
        self.out_result.setStorageMode(QgsFileWidget.SaveFile)



    def slot_layerCombo_layerChanged(self, layer):
        # 判断图层类型是点、线、面
        # 如果图层是点图层
        pass

dialog show

def show_calculate_zone_dialog(main, name, title):
    bg_color = 'background: white' if main.theme == 'light' else 'background: #2B2D30'
    if main.calulate_zone_dialog is None:
        # main.calulate_zone_dialog = CalculateDialog(main)
        # setup_dialog_ui(main.calulate_zone_dialog)
        # main.calulate_zone_dialog.set_title(title)
        #
        # main.calulate_zone_dialog.set_theme(main.theme, bg_color)
        #
        # main.calulate_zone_dialog.resize(820, 700)
        main.calulate_zone_dialog = SpatialClusterDialog(main)
    else:
        return

main code

    QgsApplication.setPrefixPath('G:/QGIS 3.38.0/apps/qgis', False) # I have tried to set to True or False,they both don't work. 

    # 第二个参数为是否启用 GUI
    qgs = QgsApplication([], False) # I have tried to set to True or False,they both don't work. 
    # 初始化 QGIS
    qgs.initQgis()
    app = PyQgisSEApp(qgs)
    app.show()

    qgs.exec_()
    qgs.exitQgis()

I tried to check the lib fils of qgis installed by conda and found they were the same as the QGIS application.And I found that in PyQGIS Api document there wern’t method that connect QgsMapLayerComboBox to the MapCanvas or the LayerTree.So I wonder how they worked in QGIS plugin.

New contributor

cywwyc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật