<div dir="ltr">I am trying to apply styles stored in a PostGIS DB.<div><br></div><div>In one method I'm trying to make an inventory, in the other method I try to load the styles of a particular set for all layers.</div><div><br></div><div>I tried to find a solution using Google and this is the result of assembling code using bits and pieces from other posts, but unfortunately it still doesn't work.</div><div><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt"><span style="color:rgb(0,0,128);font-weight:bold">def </span>populate_layer_styles(<span style="color:rgb(148,85,141)">self</span>, layer):<br>    <span style="color:rgb(0,0,128);font-weight:bold">for </span>style <span style="color:rgb(0,0,128);font-weight:bold">in </span>layer.listStylesInDatabase()[<span style="color:rgb(0,0,255)">2</span>]:<br>        style_set = style[<span style="color:rgb(0,0,255)">3</span>]<br>        <span style="color:rgb(0,0,128);font-weight:bold">if </span>style_set <span style="color:rgb(0,0,128);font-weight:bold">not in </span><span style="color:rgb(148,85,141)">self</span>.layer_styles:<br>            <span style="color:rgb(148,85,141)">self</span>.layer_styles[style_set] = {}<br>        style_manager = layer.styleManager()<br>        <span style="color:rgb(0,0,128);font-weight:bold">from </span>PyQt5.QtXml <span style="color:rgb(0,0,128);font-weight:bold">import </span>QDomDocument<br>        <span style="color:rgb(148,85,141)">self</span>.query = <span style="color:rgb(0,128,128);font-weight:bold">"SELECT styleqml FROM public.layer_styles WHERE stylename='{}'"</span>.format(style)<br>        <span style="color:rgb(0,0,128)">print</span>(<span style="color:rgb(148,85,141)">self</span>.query)<br>        <span style="color:rgb(148,85,141)">self</span>.perform_query()<br>        styleqml = <span style="color:rgb(148,85,141)">self</span>.result[<span style="color:rgb(0,0,255)">0</span>]<br>        style_doc = QDomDocument()<br>        style_doc.setContent(styleqml)<br>        <span style="color:rgb(0,0,128)">print</span>(layer.importNamedStyle(style_doc))<br>        <span style="color:rgb(0,0,128)">print</span>(layer.loadNamedStyle(style))<br>        style_manager.addStyleFromLayer(style)<br><br>        <span style="color:rgb(148,85,141)">self</span>.layer_styles[style_set][style] = style_manager<br>    <span style="color:rgb(0,0,128);font-weight:bold">for </span>style_set <span style="color:rgb(0,0,128);font-weight:bold">in </span><span style="color:rgb(148,85,141)">self</span>.layer_styles:<br>        <span style="color:rgb(0,0,128);font-weight:bold">if </span>style_set <span style="color:rgb(0,0,128);font-weight:bold">not in </span><span style="color:rgb(148,85,141)">self</span>.dockwidget.rb_styles.button:<br>            <span style="color:rgb(148,85,141)">self</span>.dockwidget.rb_styles.add_button(<span style="color:rgb(0,0,128)">str</span>(style_set), <span style="color:rgb(148,85,141)">self</span>.activate_style_set)<br><br><span style="color:rgb(0,0,128);font-weight:bold">def </span>activate_style_set(<span style="color:rgb(148,85,141)">self</span>, style_set):</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">    '''style_set is '1' or '2', etc'''<br>    <span style="color:rgb(0,0,128)">print</span>(style_set.text())<br>    <span style="color:rgb(0,0,128);font-weight:bold">for </span>layer <span style="color:rgb(0,0,128);font-weight:bold">in </span>QgsProject.instance().mapLayers().values():<br>        <span style="color:rgb(128,128,128);font-style:italic"># only for vector layers<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span><span style="color:rgb(0,0,128);font-weight:bold">if </span>layer.type() != <span style="color:rgb(0,0,255)">0</span>:<br>            <span style="color:rgb(0,0,128);font-weight:bold">continue<br></span><span style="color:rgb(0,0,128);font-weight:bold">        </span><span style="color:rgb(128,128,128);font-style:italic"># Extract table name using light regex<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span>table_re = re.compile(<span style="color:rgb(0,128,128);font-weight:bold">r'table="main"."(mow_.+?)"'</span>)<br>        table = table_re.search(layer.source())<br>        <span style="color:rgb(0,0,128);font-weight:bold">if </span>table:<br>            table = table.group(<span style="color:rgb(0,0,255)">1</span>)<br>        <span style="color:rgb(0,0,128);font-weight:bold">else</span>:<br>            <span style="color:rgb(0,0,128);font-weight:bold">continue<br></span><span style="color:rgb(0,0,128);font-weight:bold">        </span><span style="color:rgb(128,128,128);font-style:italic"># And compare it with layer names used for reloading the layers<br></span><span style="color:rgb(128,128,128);font-style:italic">        </span><span style="color:rgb(0,0,128);font-weight:bold">if </span>table <span style="color:rgb(0,0,128);font-weight:bold">in </span><span style="color:rgb(148,85,141)">self</span>.tablenames:<br>            style = <span style="color:rgb(0,128,128);font-weight:bold">'set' </span>+ style_set.text() + <span style="color:rgb(0,128,128);font-weight:bold">'-' </span>+ table<br>            <span style="color:rgb(0,0,128)">print</span>(style)<br>            <span style="color:rgb(0,0,128)">print</span>(layer.loadNamedStyle(style))<br>            style_manager = layer.styleManager()<br>            <span style="color:rgb(128,128,128);font-style:italic"># style_manager.addStyleFromLayer(style)<br></span><span style="color:rgb(128,128,128);font-style:italic">            </span><span style="color:rgb(0,0,128)">print</span>(style_manager.mapLayerStyles())<br>            <span style="color:rgb(128,128,128);font-style:italic"># print(style_manager.layer().name())<br></span><span style="color:rgb(128,128,128);font-style:italic">            </span><span style="color:rgb(0,0,128)">print</span>(style_manager.currentStyle())<br>            style_manager.setCurrentStyle(style)<br>            <span style="color:rgb(0,0,128)">print</span>(style_manager.currentStyle())<br>            <span style="color:rgb(0,0,128)">print</span>(<a href="http://layer.id">layer.id</a>())<br>            <span style="color:rgb(148,85,141)">self</span>.iface.layerTreeView().refreshLayerSymbology(<a href="http://layer.id">layer.id</a>())<br>    <span style="color:rgb(148,85,141)">self</span>.iface.mapCanvas().refreshAllLayers()</pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt"><pre style="font-family:"Courier New";font-size:10.5pt"><span style="color:rgb(0,0,128);font-weight:bold">def </span>reload_layers(<span style="color:rgb(148,85,141)">self</span>):<br>    <span style="color:rgb(148,85,141)">self</span>.iface.mapCanvas().layers()<br>    geometrycol = <span style="color:rgb(0,128,128);font-weight:bold">"geom"<br></span><span style="color:rgb(0,128,128);font-weight:bold">    </span><span style="color:rgb(148,85,141)">self</span>.layer_styles = {}<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">for </span>l <span style="color:rgb(0,0,128);font-weight:bold">in </span>QgsProject.instance().mapLayers():<br>        <span style="color:rgb(0,0,128);font-weight:bold">for </span>table <span style="color:rgb(0,0,128);font-weight:bold">in </span><span style="color:rgb(148,85,141)">self</span>.tablenames:<br>            <span style="color:rgb(0,0,128);font-weight:bold">if </span>l.title().lower().startswith(table):<br>                QgsProject.instance().removeMapLayer(l)<br>    <span style="color:rgb(0,0,128);font-weight:bold">for </span>table <span style="color:rgb(0,0,128);font-weight:bold">in </span><span style="color:rgb(148,85,141)">self</span>.tablenames:<br>        uri = QgsDataSourceUri()<br>        uri.setConnection(DB_ADDRESS, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD)<br>        uri.setDataSource(<span style="color:rgb(0,128,128);font-weight:bold">"main"</span>, table, geometrycol)<br>        layer = QgsVectorLayer(uri.uri(<span style="color:rgb(0,0,128);font-weight:bold">False</span>), table, <span style="color:rgb(0,128,128);font-weight:bold">"postgres"</span>)<br>        <span style="color:rgb(0,0,128);font-weight:bold">if not </span><span style="color:rgb(148,85,141)">self</span>.layer_styles:<br>            <span style="color:rgb(148,85,141)">self</span>.populate_layer_styles(layer)<br>        <span style="color:rgb(0,0,128);font-weight:bold">if not </span>layer.isValid():<br>            QgsMessageLog.logMessage(<span style="color:rgb(0,128,128);font-weight:bold">"Layer failed to load"</span>, <span style="color:rgb(102,0,153)">level</span>=Qgis.Critical)<br>        <span style="color:rgb(0,0,128);font-weight:bold">else</span>:<br>            QgsProject.instance().addMapLayer(layer)<br>            l = QgsProject.instance().layerTreeRoot().findLayer(layer)<br>            l.setExpanded(<span style="color:rgb(0,0,128);font-weight:bold">False</span>)<br>            l.visibilityChanged.connect(<span style="color:rgb(148,85,141)">self</span>.update_layout_widgets)<br>            layer.geometryChanged.connect(<span style="color:rgb(148,85,141)">self</span>.update_layout_widgets)<br>            <span style="color:rgb(0,0,128);font-weight:bold">if </span><a href="http://layer.name">layer.name</a>().lower().startswith(<span style="color:rgb(0,128,128);font-weight:bold">'mow_segment'</span>):<br>                QgsProject.instance().layerTreeRoot().findLayer(layer).setItemVisibilityChecked(<span style="color:rgb(0,0,128);font-weight:bold">False</span>)<br>            layer.triggerRepaint()</pre></pre><pre style="color:rgb(0,0,0);font-family:"Courier New";font-size:10.5pt">Jo</pre></div></div>