[Qt-interest] QTree
Xiaolei Shang
xiaolei.shang at cobham.com
Fri Jan 21 16:21:18 CET 2011
Hi:
> 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);
> }
I think you missed the line setting the last item in a level as parent.
If you still want to form a 4 levels tree structure as your first
example, you should do:
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);
parentItem = items.at(0);
}
Hope this helps.
Xiaolei
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110121/013a58de/attachment.html
More information about the Qt-interest-old
mailing list