diff --git a/src/qgisapp.cpp b/src/qgisapp.cpp index e5c4e660a24..6372e2a2ee0 100644 --- a/src/qgisapp.cpp +++ b/src/qgisapp.cpp @@ -499,16 +499,22 @@ void QgisApp::addDatabaseLayer() // create the layer //qWarning("creating lyr"); QgsVectorLayer *lyr = new QgsVectorLayer(connInfo + " table=" + *it, *it, "postgres"); - - // init the context menu so it can connect to slots in main app - lyr->initContextMenu(this); - - // give it a random color - QgsSingleSymRenderer* renderer=new QgsSingleSymRenderer();//add single symbol renderer as default - lyr->setRenderer(renderer); - renderer->initializeSymbology(lyr); - // add it to the mapcanvas collection - mapCanvas->addLayer(lyr); + if(lyr->isValid()){ + // init the context menu so it can connect to slots in main app + lyr->initContextMenu(this); + + // give it a random color + QgsSingleSymRenderer* renderer=new QgsSingleSymRenderer();//add single symbol renderer as default + lyr->setRenderer(renderer); + renderer->initializeSymbology(lyr); + // add it to the mapcanvas collection + mapCanvas->addLayer(lyr); + }else{ + std::cerr << *it << " is an invalid layer - not loaded" << std::endl; + QMessageBox::critical(this, tr("Invalid Layer"), + tr("%1 is an invalid layer and cannot be loaded.").arg(*it)); + delete lyr; + } //qWarning("incrementing iterator"); ++it; } diff --git a/src/qgsdataprovider.h b/src/qgsdataprovider.h index b1a59617bd4..a065516047b 100644 --- a/src/qgsdataprovider.h +++ b/src/qgsdataprovider.h @@ -115,7 +115,12 @@ public: /**Returns the maximum value of an attributs @param position the number of the attribute*/ virtual QString maxValue(int position)=0; + +/** +* Returns true if this is a valid layer. It is up to individual providers +* to determine what constitutes a valid layer +*/ +virtual bool isValid()=0; }; - - #endif +