Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, December 2006
Problems with QTreeView / QStandardItemModel


Message 1 in thread

Hi,

I'm new to Qt and QtJambi, and so far I'm impressed. But I'm having a
problem trying to code a tree view with QTreeView and QStandardItemModel.

When I construct a large, hierarchical QStandardItemModel (8000+ items), the
JVM is crashing when trying to display it in a QTreeView. I'm not sure if
there's a problem with how I'm constructing the model or not.

One area I can use help is trying to debug what's in the QStandardItemModel.
I've written the following bit of code to print a hierarchical view of
what's in the model, but it doesn't seem to print everything that should be
in the tree. Am I doing it correctly?

        printModel(myFilesModel.invisibleRootItem(), 0);

        ...
        
        private void printModel(QStandardItem item, int level) {
                for (int i=0; i<level; ++i)
                        System.out.print("  ");
                System.out.println(item.text());
                
                for (int j=0; j<item.rowCount(); ++j) {
                        if (item.child(j) != null) {
                                printModel(item.child(j),level+1);
                        }
                }
        }

Thanks for any help you can give!

-- Steve


Message 2 in thread

Steve Rosen wrote:
> Hi,
> 
> I'm new to Qt and QtJambi, and so far I'm impressed. But I'm having a
> problem trying to code a tree view with QTreeView and QStandardItemModel.
> 
> When I construct a large, hierarchical QStandardItemModel (8000+ items), the
> JVM is crashing when trying to display it in a QTreeView. I'm not sure if
> there's a problem with how I'm constructing the model or not.
> 
> One area I can use help is trying to debug what's in the QStandardItemModel.
> I've written the following bit of code to print a hierarchical view of
> what's in the model, but it doesn't seem to print everything that should be
> in the tree. Am I doing it correctly?
> 
>         printModel(myFilesModel.invisibleRootItem(), 0);
> 
>         ...
>         
>         private void printModel(QStandardItem item, int level) {
>                 for (int i=0; i<level; ++i)
>                         System.out.print("  ");
>                 System.out.println(item.text());
>                 
>                 for (int j=0; j<item.rowCount(); ++j) {
>                         if (item.child(j) != null) {
>                                 printModel(item.child(j),level+1);
>                         }
>                 }
>         }
> 
> Thanks for any help you can give!

Problem is probably that your QStandardItem's are being garbage 
collected. This is a bug that we'll fix for the next release. In the 
meantime you can fix this by manually disabling gc for these objects. 
See the attached example.

best regards,
Gunnar


import com.trolltech.qt.*;
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

public class Test
{
    public static void fill(QStandardItem parent, int level) {
        if (level > 13)
            return;

        QStandardItem c1 = new QStandardItem(level + " : " + 1);
        c1.disableGarbageCollection();

        QStandardItem c2 = new QStandardItem(level + " : " + 2);
        c2.disableGarbageCollection();

        parent.insertRow(0, c1);
        parent.insertRow(1, c2);

        fill(c1, level+1);
        fill(c2, level+1);
    }

    private static void printModel(QStandardItem item, int level) {
        for (int i=0; i<level; ++i)
            System.out.print("  ");
        System.out.println(item.text());

        for (int j=0; j<item.rowCount(); ++j) {
            if (item.child(j) != null) {
                printModel(item.child(j), level+1);
            }
        }
    }

    public static void main(String args[]) {

	QApplication.initialize(args);

        QStandardItemModel model = new QStandardItemModel();

        fill(model.invisibleRootItem(), 0);
        printModel(model.invisibleRootItem(), 0);

        QTreeView view = new QTreeView();
        view.setModel(model);
        view.show();

        QApplication.exec();
    }
}

Message 3 in thread

Gunnar Sletta wrote:

> Steve Rosen wrote:
>> Hi,
>> 
>> I'm new to Qt and QtJambi, and so far I'm impressed. But I'm having a
>> problem trying to code a tree view with QTreeView and QStandardItemModel.
>> 
>> When I construct a large, hierarchical QStandardItemModel (8000+ items),
>> the JVM is crashing when trying to display it in a QTreeView. I'm not
>> sure if there's a problem with how I'm constructing the model or not.
>> 
>> One area I can use help is trying to debug what's in the
>> QStandardItemModel. I've written the following bit of code to print a
>> hierarchical view of what's in the model, but it doesn't seem to print
>> everything that should be in the tree. Am I doing it correctly?
>> 
>>         printModel(myFilesModel.invisibleRootItem(), 0);
>> 
>>         ...
>>         
>>         private void printModel(QStandardItem item, int level) {
>>                 for (int i=0; i<level; ++i)
>>                         System.out.print("  ");
>>                 System.out.println(item.text());
>>                 
>>                 for (int j=0; j<item.rowCount(); ++j) {
>>                         if (item.child(j) != null) {
>>                                 printModel(item.child(j),level+1);
>>                         }
>>                 }
>>         }
>> 
>> Thanks for any help you can give!
> 
> Problem is probably that your QStandardItem's are being garbage
> collected. This is a bug that we'll fix for the next release. In the
> meantime you can fix this by manually disabling gc for these objects.
> See the attached example.
> 
> best regards,
> Gunnar

Yes, that seems to have fixed it! Thanks so much for the help; I was banging
my head on the wall before your answer.

Regards,
  Steve