1.调用
1) QgsMapLayerFactory::createLayer
在QgsMapLayerFactory::createLayer()内判断需创建的图层类型后创建对应的图层对象实例
QgsMapLayerFactory::createLayer(...)
└─new QgsVectorLayer(...)
2) QgsVectorLayer构造函数
当传入的图层路径和providerKey都不为空时,调用父类QgsMapLayer::setDataSource设置数据源
QgsMapLayerFactory::createLayer(...)
└─new QgsVectorLayer(...)
└─QgsMapLayer::setDataSource(...)
3) QgsMapLayer::setDataSource()
QgsMapLayer::setDataSource内部调用了setDataSourcePrivate(),由于setDataSourcePrivate是虚函数且子类QgsVectorLayer对其进行了实现,所有此时的函数实现为QgsVectorLayer::setDataSourcePrivate()
QgsMapLayerFactory::createLayer(...)
└─new QgsVectorLayer(...)
└─QgsMapLayer::setDataSource(...)
└─QgsVectorLayer::setDataSourcePrivate(...)
4) QgsVectorLayer::setDataSourcePrivate
setDataSourcePrivate内调用setDataProvider()设置图层的dataprovider
QgsMapLayerFactory::createLayer(...)
└─new QgsVectorLayer(...)
└─QgsMapLayer::setDataSource(...)
└─QgsVectorLayer::setDataSourcePrivate(...)
└─setDataProvider(...)
2. QgsVectorLayer::setDataProvider()
1) 创建DataProvider对象
通过QgsProviderRegistry::createProvider创建对应的Provider对象
bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
{
......
mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options, flags ) );
......
}
2) 如果可以获取图层元数据
bool QgsVectorLayer::setDataProvider( QString const &provider, const QgsDataProvider::ProviderOptions &options, QgsDataProvider::ReadFlags flags )
{
......
mDataProvider = qobject_cast<QgsVectorDataProvider *>( QgsProviderRegistry::instance()->createProvider( provider, mDataSource, options, flags ) );
......
if ( mDataProvider->capabilities() & QgsVectorDataProvider::ReadLayerMetadata )
{
// we combine the provider metadata with the layer's existing metadata, so as not to reset any user customizations to the metadata
// back to the default if a layer's data source is changed
QgsLayerMetadata newMetadata = mDataProvider->layerMetadata();
// this overwrites the provider metadata with any properties which are non-empty from the existing layer metadata
newMetadata.combine( &mMetadata );
setMetadata( newMetadata );
QgsDebugMsgLevel( QStringLiteral( "Set Data provider QgsLayerMetadata identifier[%1]" ).arg( metadata().identifier() ), 4 );
}
......
}
网友评论