On 02/21/2018 11:16 PM, Stefan Giese wrote:

yes, you can. Save the style in the same directory as foo.qml.

Am 22.02.2018 um 02:25 schrieb David J. Bakeman:

Can I make it so that every time I load a shapefile named foo.shp it gets a particular style?

Yes but I don't want to write a script to populate my directories with foo.qml...  What I want is to have foo.qml applied to foo.shp whenever I open a foo.shp.  If I was just opening a few shape files occasionally I would just apply the style the way I do now which is to load Style.

I get lots of shapefiles that come in a deep directory tree. I load the whole directory tree using the excellent plug-in load them all but then I have to go in and manually apply the styles.   I occasionally use another excellent plugin multiqml to apply Styles in a somewhat automated fashion but it's still difficult.

I thought I had heard of some way to do this but couldn't google the right thing.  So I discovered by browsing the qgis source code that whenever it loads a new map layer, in addition to checking for a foo.qml file, it checks a sqlite db qgis.qmldb (actually it checks for qgis.qmldb in the settings path and resources/qgis.qmldb and the project path projectbasename.qmldb) and I thought I could use this.  I couldn't find any instructions on how to create the sqlite database but I did find an example by Googling and I created my own.

Initially I tried using foo.shp as the key but that didn't work.  Once I discovered how to build a debug qgis version I discovered that unfortunately this looks for the full path instead of just foo.shp, actually it is the full path/foo.qml.   So I don't have to put foo.qml files all throughout my directory tree but I still have to have a separate entry for each foo file in this qml database.

So I added one more test where it takes just the file name so foo.qml and searches for that in the qml database in the application settings directory.  For me this works great I can load a whole bunch of shape files using load them all and they all get the style that I want!

This is what I changed the code to:

QFileInfo  fname(theURI);
if ( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) ||
   ( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml ) ) ||
   loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml ) ||
   loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), fname.fileName(), qml ))

Am I doing something the wrong way?

Qgis-user mailing list
List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user

Reply via email to