[Qgis-developer] Using the QgsBrowserModel in Python

Ludovic Delauné ludovic.delaune at oslandia.com
Thu Jul 30 05:54:52 PDT 2015


Le 30/07/2015 13:47, Hugo Mercier a écrit :
> Hi,
> 
> On 29/07/2015 17:50, Ludovic Delauné wrote:
>> Hi devs,
>>
>> i'm trying to use the QgsBrowserModel in a Python plugin.
>> I need to get the URI from a selected Item in the QTreeView, i expected to have a QgsLayerItem returned by the dataItem() in order to get the uri() member but a QgsDataItem
>> is returned instead.
>> Given a QgsDataItem, is it possible to get the uri of my data source ?
>>
>> Is this a bug due to the binding (https://github.com/qgis/QGIS/blob/feb3bee85837d707b1b10c14064fcb55364e282d/python/core/qgsbrowsermodel.sip) or i'm not in the right way ?
>>
>> Here are the few steps to reproduce the problem in a console:
>>
>> -------
>> from PyQt4.QtGui import QTreeView
>> tv = QTreeView(None)
>> m = QgsBrowserModel()
>> tv.setModel(m)
>> tv.show()
>> m.dataItem(tv.currentIndex())
>> --------
>> output is :
>> <qgis._core.QgsDataItem object at 0x7f6cb8bcca68>
> 
> Apparently, this is because QgsDataItem has nothing to convert to its
> derived types in the Python bindings. I'll add a %ConvertToSubClassCode.
> 
> Thanks for the report

Thanks for that clarification.

My current workaround is to use the mimeData method and deserialize the item to obtain the URI :

Add to the steps above :

from qgis.core import QgsMimeDataUtils
uri_list = QgsMimeDataUtils.decodeUriList(m.mimeData([tv.currentIndex()]))
uri_list[0].uri

--
Ludovic





More information about the Qgis-developer mailing list