[Qt-interest] QTree

Alex Strickland sscc at mweb.co.za
Fri Jan 21 12:29:28 CET 2011


On 2011/01/20 03:55 PM, Alex Strickland wrote:

 > I am just starting out to learn about Model/View via the Simple Tree
 > Model example. The tree used in the example looks functional, but it
 > lead me to look for a QTree (or similar) class but I couldn't find
 > anything. Is there such a thing?

I took Konrad Rosenbaums suggestion and used QStandardItemModel. From 
the docs I took something like this :

     QStandardItemModel *treeModel = new QStandardItemModel();
     QStandardItem *parentItem = treeModel->invisibleRootItem();

     for (int i = 0; i < 4; ++i) {
         QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
         parentItem->appendRow(item);
         parentItem = item;
     }

which works. Then, because I wanted checkboxes, I did this, which also 
works, but does not create a tree:

     for (int i = 0; i < 4; ++i) {
         QList<QStandardItem *> items;
         items.append(new QStandardItem(QString("item %0").arg(i)));
         QStandardItem *itemX = new QStandardItem();
         itemX->setCheckable(true);
         items.append(itemX);

         parentItem->appendRow(items);
     }

For the life of me I can't figure out how to make it a hierarchy (the 
equivalent of "parentItem = item;") like the first example. Any one take 
pity?

Thanks for the previous replies and definitely no offence taken.

-- 
Alex



More information about the Qt-interest-old mailing list