Qt-jambi-interest Archive, December 2006
QStandardModel
Message 1 in thread
Hello Qt Jambi people,
IÂm trying to implement a really simple model through subclassing
QStandardModel. I want to have a tree like model and therefore IÂm
adding Subclasses of QStandardItems to this model, via appendRow(item).
The problem is the following:
I want to build up an hierarchical structure and therefore I`m adding
new sub-items only to the parent item via appendRow(sub-item) These
sub-items are added successfully, but when I`m applying the model to a
TreeView Widget, this view only displays the first-level root-items,
which were directly appended to the model.
The sub-items (which were only appended to the first-level root-items)
are rendered with no shown information. What is the default behaviour of
the QStandardItem? I`ve done some experiments with a implementation of
the data(int role) method and I assume that whenever a call from the
view to the model occurs, only the parent-item is returned and not the
sub-item, although the view tries to get information about that sub-item
- so no appropriate data could be applied.
Did I understand the model/view architecture wrong?
Do I still have to inform the model about the sub-item QModelIndexes,
during the appendRow call?
Please could you provide a simple tree like model in java, not only the
simple string list example?
Pseudocode goes like this:
class Item extends QStandardItem { .. }
class SubItem extends QStandardItem {...}
class Model extends QStandardModel { ... }
example, how I understood it:
// create model
Model model = new Model();
// create 1st level root item
Item item = new Item();
// add this item
model.appendRow(item);
// create sub-item
SubItem subItem = new SubItem();
// append this sub-item to the item
item.appendRow(subItem);
Greetings,
Felix Beyer
Message 2 in thread
Felix Beyer wrote:
> Hello Qt Jambi people,
>
> IÂm trying to implement a really simple model through subclassing
> QStandardModel. I want to have a tree like model and therefore IÂm
> adding Subclasses of QStandardItems to this model, via appendRow(item).
> The problem is the following:
> I want to build up an hierarchical structure and therefore I`m adding
> new sub-items only to the parent item via appendRow(sub-item) These
> sub-items are added successfully, but when I`m applying the model to a
> TreeView Widget, this view only displays the first-level root-items,
> which were directly appended to the model.
Hi Felix,
Because of some missing setup call in Qt Jambi, your QStandardItems are
collected by the garbage collector. Since they are collected the results
will be undefined.
This happens because we don't keep a reference the items when you add
them to the model or to their parent item. We'll fix this for the next
Qt Jambi release. In the meantime, you will have to do this explicitly
by calling disableGarbageCollection() on the all items. I've attached an
example that demonstrates this.
best regards,
Gunnar
package com.trolltech.tests;
package com.trolltech.tests;
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
class Item extends QStandardItem {
public Item() {
disableGarbageCollection();
}
public Object data(int role) {
if (role == Qt.ItemDataRole.DisplayRole)
return "Item " + id;
if (role == Qt.ItemDataRole.DecorationRole)
return pixmap;
return null;
}
public void setIcon(QPixmap p) {
pixmap = p;
}
private static int idCounter = 0;
private int id = ++idCounter;
private QPixmap pixmap;
}
public class TreeItems {
public static QStandardItemModel setupModel() {
QStandardItemModel model = new QStandardItemModel();
Item r1 = new Item();
Item r2 = new Item();
Item r3 = new Item();
Item s11 = new Item();
Item s12 = new Item();
Item s21 = new Item();
r1.appendRow(s11);
r1.appendRow(s12);
r2.appendRow(s21);
model.appendRow(r1);
model.appendRow(r2);
model.appendRow(r3);
return model;
}
public static void main(String args[]) {
QApplication.initialize(args);
QTreeView view = new QTreeView();
view.show();
view.setModel(setupModel());
QApplication.exec();
}
}