[bt-devel] tree keys and Qt4 Model/View architecture

Joachim Ansorg nospam+bt-devel at joachim-ansorg.de
Thu Jun 7 06:18:56 MST 2007


Hi Eeli,
I looked at your code.

I think the slow speed of the program is caused by too many calls to
index()
rowCount()
parent()

If you insert debug("blah()") into the methods you'll see that these are 
called a lot, even while the mouse is hovering some items.
Each of the mentioned methods iterates a part of the tree, which makes it 
quite slow.

The solution would be caching, imho. Using QTreeWidget should do that for you 
(I hope). A simple self-made caching solution would be to cache the results 
of the different method calls. I did that, just for fun :)
I attached the changed files. I think I added a bug, too :) After 
opening/closing the items for several time, the model seems to get mixed up 
with. But it demonstrates how the caching speeds up the model.


The code quality looks fine to me. I have some suggestions, which are not 
related to the problem.

Usually it's good not to have any side effects in an if(...), e.g.
	if (!m_key->firstChild()) {
I you replace that with "if( !m_key->hasChildren() )", which has the same 
meaning in that context, the code's broken (nextSibling() 3 lines below would 
have the wrong parent).
I suggest:
	m_key->setOffset(parentOffset);
	if (!m_key->hasChildren()) {
	        return 0;
	}
	
	m_key->firstChild();
	while (m_key->nextSibling()) {
		count++;
	}

Things like
    int parentOffset;

    if (!parent.isValid())
        parentOffset = 0; //pointer to 0
    else
        parentOffset = parent.internalId();
can be shortened to
    const int parentOffset = parent.isValid() ? parent.internalId() : 0;

But this isn't important, probably just personal style. The compile probably 
is smart enough to generate the same binary code.

Thanks for spending that much time with the model / view stuff. I think it's 
quite difficult, too. That's why labs.trolltech.com offers a model checking 
tool ;)

Joachim



-------------- next part --------------
A non-text attachment was scrubbed...
Name: treemodel.h
Type: text/x-c++hdr
Size: 1387 bytes
Desc: not available
Url : http://www.crosswire.org/pipermail/bt-devel/attachments/20070607/ddf22aaf/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: treemodel.cpp
Type: text/x-c++src
Size: 3342 bytes
Desc: not available
Url : http://www.crosswire.org/pipermail/bt-devel/attachments/20070607/ddf22aaf/attachment-0001.bin 


More information about the bt-devel mailing list