PDA

View Full Version : expanding tree view problem



joeld42
7th May 2009, 19:34
I've got a custom tree model/view. I've got a slot on the treeView called expandNonDefaultNodes(), which goes through all the nodes, and calls setExpanded( index, true ) on every node that isn't at the default value.

This behaves correctly, except I get a bunch of these warnings on the shell:


QPainter::begin: Cannot paint on a null pixmap
QPainter::translate: Painter not active
QPainter::end: Painter not active, aborted

The debugger indicates that this is coming from the scrollbars.

However, if I call QTreeView's expandAll() instead, it works. What is QTreeView::expandAll() doing that I'm not? I looked at the code for expandAll(), and there's some stuff I don't understand (like d->layout(-1) ) but I am calling update(), updateGeometries() and viewport()->update().

wysota
7th May 2009, 20:54
Can we see your code?

joeld42
7th May 2009, 21:24
Sure, here's the whole function:


void JasperView::expandNonDefaultNodes()
{
// If I uncomment this, I don't get the QPainter warnings
// and ALL the nodes are expanded..
//expandAll();
//return;

QAbstractItemModel *mdl = model();
QModelIndex rootNdx = rootIndex();

for (int i=0; i < mdl->rowCount( rootNdx ); ++i)
{
QModelIndex ctrIndex = mdl->index( i, 0, rootNdx );
BlockTreeItem *item = getItem( ctrIndex );

// For binds, just expand so the fcalls are visible (for now)
BindBlockItem *bindBlock = dynamic_cast<BindBlockItem*>( item );
if (bindBlock)
{
setExpanded( ctrIndex, true );
}

// For functions, expand out any nodes with non-default values
FuncBlockItem *funcBlock = dynamic_cast<FuncBlockItem*>( item );

if (funcBlock)
{
bool anyExpandedNodes = false;
for (int j=0; j < mdl->rowCount( ctrIndex ); ++j )
{
QModelIndex nodeIndex = mdl->index( j, 0, ctrIndex );
ParamNodeItem *nodeItem = dynamic_cast<ParamNodeItem*>( getItem( nodeIndex ) );
ParamNode *node = nodeItem->block();

if (node)
{
bool shouldExpand = node->anyChangesFromDefault();
setExpanded( nodeIndex, shouldExpand );

anyExpandedNodes = anyExpandedNodes | shouldExpand;
}
}

// Expand this container if any of it's nodes
// are expanded
setExpanded( ctrIndex, anyExpandedNodes );

}
}

// these don't seem to make any difference
updateGeometries();
viewport()->update();
update();
}

I've tried replacing the setExpanded() calls with expand() but it does the same thing. I have verified that the setExpandeds actually get called.

One other little detail, I was calling this from my openFile, after I loaded the model data and did a setModel. That resulted in nothing expanding (again, expandAll works). I'm now calling it with this:


QTimer::singleShot( 0, ui.treeView, SLOT(expandNonDefaultNodes()) );

And it works, but i get the warnings. Same thing if I call it from a QAction.

this is qt 4.5.0, btw.

wysota
8th May 2009, 06:44
What happens if you increase the timeout of the timer to 100ms or so?

joeld42
8th May 2009, 20:52
Ok, so I figured it out. I had the "animated" property set, and that was causing problems. I wrapped the function with setAnimated( false ), setAnimated( true) and it works now.

In the process of figuring this out, I modified simpletreeview to exhibit the same behavior, attached if anyone's curious. I'm not sure how to file a bug report, and in any case, I've got a good workaround.

Thanks!
Joel