Skip to main content
deleted 13 characters in body
Source Link
Jan
  • 171
  • 8

When I load a bunch of layers into QGIS from PostGIS, they seem to load in reverse alphabetical order. I'd like to sort them alphabetically.

In this thread a solution is proposed, but that does not work in QGIS 2.18 or 3.4:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface


mw = iface.mainWindow()
lgd = mw.findChild(QTreeWidget, "theMapLegend")  # get ref to object by type/objectName
lgd.sortItems(0, Qt.AscendingOrder)  # sort first column (Qt.DescendingOrder to reverse)

How can I do this in QGIS 3? I tried:

from qgis.PyQt.QtCore import * 
from qgis.PyQt.QtGui import *
from qgis.utils import iface

mw = iface.mainWindow()
lgd = mw.findChild(QTreeView, 'theLayerTreeView')
lgd.sortItems(0, Qt.AscendingOrder)

It "works" until the last line, then I get the error:

Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in AttributeError: 'QgsLayerTreeView' object has no attribute 'sortItems'

I don't know where to search for a method that will sort the layers as in the first code example.

When I load a bunch of layers into QGIS from PostGIS, they seem to load in reverse alphabetical order. I'd like to sort them alphabetically.

In this thread a solution is proposed, but that does not work in QGIS 2.18 or 3.4:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface


mw = iface.mainWindow()
lgd = mw.findChild(QTreeWidget, "theMapLegend")  # get ref to object by type/objectName
lgd.sortItems(0, Qt.AscendingOrder)  # sort first column (Qt.DescendingOrder to reverse)

How can I do this in QGIS 3? I tried:

from qgis.PyQt.QtCore import * 
from qgis.PyQt.QtGui import *
from qgis.utils import iface

mw = iface.mainWindow()
lgd = mw.findChild(QTreeView, 'theLayerTreeView')
lgd.sortItems(0, Qt.AscendingOrder)

It "works" until the last line, then I get the error:

Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in AttributeError: 'QgsLayerTreeView' object has no attribute 'sortItems'

I don't know where to search for a method that will sort the layers as in the first code example.

When I load a bunch of layers into QGIS, they seem to load in reverse alphabetical order. I'd like to sort them alphabetically.

In this thread a solution is proposed, but that does not work in QGIS 2.18 or 3.4:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface


mw = iface.mainWindow()
lgd = mw.findChild(QTreeWidget, "theMapLegend")  # get ref to object by type/objectName
lgd.sortItems(0, Qt.AscendingOrder)  # sort first column (Qt.DescendingOrder to reverse)

How can I do this in QGIS 3? I tried:

from qgis.PyQt.QtCore import * 
from qgis.PyQt.QtGui import *
from qgis.utils import iface

mw = iface.mainWindow()
lgd = mw.findChild(QTreeView, 'theLayerTreeView')
lgd.sortItems(0, Qt.AscendingOrder)

It "works" until the last line, then I get the error:

Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in AttributeError: 'QgsLayerTreeView' object has no attribute 'sortItems'

I don't know where to search for a method that will sort the layers as in the first code example.

Tweeted twitter.com/StackGIS/status/1118122066392358913
edited tags
Link
Joseph
  • 76.3k
  • 7
  • 172
  • 286
added pyqgis-3 tag, fixed minor typos
Source Link
csk
  • 25.1k
  • 3
  • 33
  • 72

When I load a bunch of layers into QGIS from PostGIS, they seem to load in reverse alphabetical order. I'd like to sort them alphabetically.

In thisthis thread a solution is proposed, but that does not work in QGIS 2.18 or 3.4:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface


mw = iface.mainWindow()
lgd = mw.findChild(QTreeWidget, "theMapLegend")  # get ref to object by type/objectName
lgd.sortItems(0, Qt.AscendingOrder)  # sort first column (Qt.DescendingOrder to reverse)

How can I do this in QGIS 3? I tried:

from qgis.PyQt.QtCore import * 
from qgis.PyQt.QtGui import *
from qgis.utils import iface

mw = iface.mainWindow()
lgd = mw.findChild(QTreeView, 'theLayerTreeView')
lgd.sortItems(0, Qt.AscendingOrder)

It "works" until the last line, then I get the error:

Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in AttributeError: 'QgsLayerTreeView' object has no attribute 'sortItems'

I don't know where to search for a method that will sort the layers as in the first code example.

When I load a bunch of layers into QGIS from PostGIS, they seem to load in reverse alphabetical order. I'd like to sort them alphabetically

In this thread a solution is proposed, but that does not work in QGIS 2.18 or 3.4:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface


mw = iface.mainWindow()
lgd = mw.findChild(QTreeWidget, "theMapLegend")  # get ref to object by type/objectName
lgd.sortItems(0, Qt.AscendingOrder)  # sort first column (Qt.DescendingOrder to reverse)

How can I do this in QGIS 3? I tried:

from qgis.PyQt.QtCore import * 
from qgis.PyQt.QtGui import *
from qgis.utils import iface

mw = iface.mainWindow()
lgd = mw.findChild(QTreeView, 'theLayerTreeView')
lgd.sortItems(0, Qt.AscendingOrder)

It "works" until the last line, then I get the error:

Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in AttributeError: 'QgsLayerTreeView' object has no attribute 'sortItems'

I don't know where to search for a method that will sort the layers as in the first code example.

When I load a bunch of layers into QGIS from PostGIS, they seem to load in reverse alphabetical order. I'd like to sort them alphabetically.

In this thread a solution is proposed, but that does not work in QGIS 2.18 or 3.4:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.utils import iface


mw = iface.mainWindow()
lgd = mw.findChild(QTreeWidget, "theMapLegend")  # get ref to object by type/objectName
lgd.sortItems(0, Qt.AscendingOrder)  # sort first column (Qt.DescendingOrder to reverse)

How can I do this in QGIS 3? I tried:

from qgis.PyQt.QtCore import * 
from qgis.PyQt.QtGui import *
from qgis.utils import iface

mw = iface.mainWindow()
lgd = mw.findChild(QTreeView, 'theLayerTreeView')
lgd.sortItems(0, Qt.AscendingOrder)

It "works" until the last line, then I get the error:

Traceback (most recent call last): File "C:\PROGRA~1\QGIS3~1.4\apps\Python37\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in AttributeError: 'QgsLayerTreeView' object has no attribute 'sortItems'

I don't know where to search for a method that will sort the layers as in the first code example.

Source Link
Jan
  • 171
  • 8
Loading