Question

I have a problem with the declaration of flags in a widget which is used as a custom widget for QtDesigner.

This widget is a QComboBox using a filter proxy model, called QgsMapLayerComboBox

In the filter proxy model (QgsMapLayerProxyModel), I have defined flags:

class GUI_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel
{
    Q_OBJECT
    Q_FLAGS( Filters )
  public:
    enum Filter
    {
      NoFilter = 1,
      RasterLayer = 2,
      NoGeometry = 4,
      PointLayer = 8,
      LineLayer = 16,
      PolygonLayer = 32,
      HasGeometry = PointLayer | LineLayer | PolygonLayer,
      VectorLayer = NoGeometry | HasGeometry
    };
    Q_DECLARE_FLAGS( Filters, Filter )

    ...
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters )

Now I want to be able to define these settings directly in Qt Designer. Hence, I have reused the flags from the proxy model class in the combo box class:

class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
{
    Q_OBJECT

    Q_FLAGS( QgsMapLayerProxyModel::Filters )
    Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )

  public:
    explicit QgsMapLayerComboBox( QWidget *parent = 0 );

    //! setFilters allows fitering according to layer type and/or geometry type.
    void setFilters( QgsMapLayerProxyModel::Filters filters );

    //! currently used filter on list layers
    QgsMapLayerProxyModel::Filters filters(){ return mProxyModel->filters(); }
}

This is working as expected.

But, these widgets are also compiled in a python library using SIP. I have created a module for pyuic (in /usr/lib/python2.7/dist-packages/PyQt4/uic/widget-plugins) so it knows where to look for the widget:

pluginType = MODULE
def moduleInformation():
    return "qgis.gui", ("QgsMapLayerCombobox", )

Now, the problem is that pyuic complains: AttributeError: unknown enum QgsMapLayerProxyModel::RasterLayer because it can't find QgsMapLayerProxyModel.

The only solution that came to my mind was to duplicate the flags in QgsMapLayerComboBox:

class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
{
    Q_OBJECT

    Q_FLAGS( Filters2 )
    Q_PROPERTY( Filters2 filters2 READ filters2 WRITE setFilters2 )

  public:
    typedef QgsMapLayerProxyModel::Filter Filter2;
    typedef QgsMapLayerProxyModel::Filters Filters2;

    explicit QgsMapLayerComboBox( QWidget *parent = 0 );

    //! setFilters allows fitering according to layer type and/or geometry type.
    void setFilters2( Filters2 filters );

    //! currently used filter on list layers
    Filters2 filters2(){ return static_cast<Filters2>( mProxyModel->filters() ); }
}

But this is not working: I don't see the settings in Qt Designer: do you know why?

Would you think of a better way to solve this?

PS: this is made within QGIS code.

Was it helpful?

Solution

Just modify your code, so Qt Designer will know where to find the definition for the required enum:

pluginType = MODULE
def moduleInformation():
     return "qgis.gui", ("QgsMapLayerCombobox", "QgsMapLayerProxyModel" )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top