1:在qgsmapcanvas.cpp中定义一个全局字符串变量,QString g_sNeedLayerID="";//需要隐藏的图层id
2:找到refreshMap()函数;
3:添加如下代码:
Q_ASSERT( mRefreshScheduled ); QgsDebugMsg( "CANVAS refresh!" ); stopRendering(); // if any... // from now on we can accept refresh requests again mRefreshScheduled = false; //update $map variable to canvas QgsExpression::setSpecialColumn("$map", tr("canvas")); // create the renderer job Q_ASSERT( mJob ==0); mJobCancelled = false; QStringList layerList; if (m_Settings_back.layers().count()==0) { m_Settings_back=m_Settings; } foreach (QString id, m_Settings_back.layers()) { if (id!=g_sNeedLayerID) { layerList.push_back(id); } } m_Settings.setLayers(layerList); mJob = new QgsMapRendererSequentialJob(m_Settings); connect(mJob, SIGNAL(finished()), SLOT( rendererJobFinished())); mJob->setCache( mCache ); QStringList layersForGeometryCache; foreach (QString id, m_Settings.layers()) { if (QgsVectorLayer* vl= qobject_cast<QgsVectorLayer*>(QgsMapLayerRegistry::instance()->mapLayer(id))) { if (vl->isEditable() )//&& id!=m_sLayerID) layersForGeometryCache << id; } } mJob->setRequestedGeometryCacheForLayers(layersForGeometryCache); mJob->start(); emit renderStarting();
4:在其他想隐藏的代码里:
extern QString g_sNeedLayerID; //需要隐藏的图层id
g_sNeedLayerID=m_pPolylineLayer->id(); m_pMapCanvas->refresh();