[Qgis-developer] Need help with LayerTreeModel (legend node)

Sandro Santilli strk at keybit.net
Fri Dec 19 03:55:11 PST 2014


On Fri, Dec 19, 2014 at 10:34:34AM +0100, Sandro Santilli wrote:
> On Fri, Dec 19, 2014 at 03:35:39PM +0700, Martin Dobias wrote:
> > On Fri, Dec 19, 2014 at 12:37 AM, Sandro Santilli <strk at keybit.net> wrote:
> > >
> > > On Thu, Dec 18, 2014 at 01:46:21PM +0100, Sandro Santilli wrote:
> > >
> > > > The weird looking aspect seems to be due to a discrepancy between
> > > > the actual legend image size and the space reserved for it.
> > > > In practice I have 2 layers and the layer node for second one gets
> > > > rendered right below the first one, with the legend of the first one
> > > > being printed _over_ the layer node of the second, which moves down
> > > > to the correct place on next redraw.
> > > >
> > > > Could this be a bug in the QgsLayerTreeView ?
> > 
> > Most likely you just do not emit dataChanged() where it should be (I have
> > seen this kind of errors in various models and this was a common problem).
> > You can try using the model with plain QTreeView - it will most likely have
> > the same issue.
> >
> > Maybe a missing emit dataChanged() in
> > QgsWMSLegendNode::invalidateMapBasedData() ? Just guessing...
> 
> I've added logging calls and I do see the dataChanged being emitted.
> Also, the _legend_ does get refreshed, so I see the new image.
> Only, the position of the following layer did not move.
> As if the View got the new content (decoration) but not the new size.
> 
> I'm not sure the explanation is clear.
> Did you try building my branch to see ?

On Nyall suggestion I've added support for testing models with
the QT Model Tester: http://qt-project.org/wiki/Model_Test

The test fail this assertion in ModelTest::nonDestructiveBasicTest():

  Qt::ItemFlags flags = model->flags( QModelIndex() );
  Q_ASSERT( flags == Qt::ItemIsDropEnabled || flags == 0 );

The test checks that the flag for an invalid index can only
be 0 or Qt::ItemIsDropEnabled. Instead QgsLayerTreeModel includes
ItemIsSelectable and ItemIsEnabled in the flags:

  if ( !index.isValid() )
  {
    Qt::ItemFlags rootFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    if ( testFlag( AllowNodeReorder ) )
      rootFlags |= Qt::ItemIsDropEnabled;
    return rootFlags;
  }

What's the rationale for adding those two flags ?
I've tested that initializing ti to 0 and adding the dropEnabled
based on AllowNodeReorder passes the test.

Also reverting the change that switched to recursivelyEmitDataChanged()
did not trigger an error report from ModelTester, which leaves the
view as the more likely culprit for the placement issue. How would
you suggest to go for quickly trying use of a vanilla QTreeView ?

--strk; 

 ()  ASCII ribbon campaign  --  Keep it simple !
 /\  http://strk.keybit.net/rants/ascii_mails.txt  


More information about the Qgis-developer mailing list