Waldemar Wittmann wrote:
Hi all,
i am vizualising a real Tree Datastructure. It's a binary Tree where
each Node has always 2 Childs (leafs have 0 childs) and one parent (in
the case of the root node no parent).
I have extended QTreeView and I extended QTreeModel. The problem is, the
Tree Datastructure changes, for example new nodes are inserted or nodes
are deleted and i want to see immediately the changes in the QTreeView
without clicking (it's a vizualisation).
My tree-node class extends the QSignalEmitter and sends signals to my
methods in the extended QTreeModel class. So i have the required
information on which node the change is, for example that a node had 0
childs and now 2 childs.
I looked for Methods how to inform the QTreeModel or the QTreeView about
the changes but didn't really find them. I tried the following in the
slot method (the signals from the tree-nodes are connected with this
method) in my extended QTreeModel class:
Hi Waldemar,
The methods you are looking for are childrenInserted and childrenRemoved:
http://doc.trolltech.com/qtjambi-4.4/html/com/trolltech/qt/gui/QTreeModel.html#childrenInserted(com.trolltech.qt.core.QModelIndex,%20int,%20int)
http://doc.trolltech.com/qtjambi-4.4/html/com/trolltech/qt/gui/QTreeModel.html#childrenRemoved(com.trolltech.qt.core.QModelIndex,%20int,%20int)
As for the Treemodel file example, its more an example bug. The example
should really listen to filesystem using QFileSystemWatcher and updated
repeadedly but it currently doesn't ;-)
The attached example illustrates adding and removing items programatically.
best regards,
Gunnar
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
import java.util.*;
/**
* A pretty basic node implementation...
*/
class Node {
public Node(String s, Model model) {
this.text = s;
this.model = model;
}
@Override
public String toString() { return text + ":" + counter; }
List<Node> children = new ArrayList<Node>();
String text;
int counter;
Model model;
}
/**
* An example model implementation. It reimplements child(), childCount() and
text() to
* represent the data in a tree of Node's
*/
class Model extends QTreeModel {
/**
* Called to query the child of parent at index. If parent is null we have
only one child,
* the root.
*/
@Override
public Object child(Object parent, int index) {
if (parent == null)
return root;
return ((Node) parent).children.get(index);
}
/**
* Called to query the number of children of the given object or the number
of root objects if
* parent is null.
*/
@Override
public int childCount(Object parent) {
int count = parent == null ? 1 : ((Node) parent).children.size();
return count;
}
/**
* Convenience virtual function to get the textual value of an object. I
could also
* implement icon() for pixmap data or the data() function for other types
of roles.
*/
@Override
public String text(Object value) {
return "" + value;
}
public Node root() { return root; }
private Node root = new Node("Root", this);
}
/**
* A simple test application. It adds 3 actions, "add", "remove" and
"increment" to test
* that we can add, remove and change the value of nodes in the tree.
*/
public class TreeModelTester extends QTreeView {
public TreeModelTester() {
this(null);
}
public TreeModelTester(QWidget parent) {
super(parent);
setModel(model);
QAction add = new QAction(this);
add.setShortcut("Ctrl+A");
add.triggered.connect(this, "add()");
QAction remove = new QAction(this);
remove.setShortcut("Ctrl+R");
remove.triggered.connect(this, "remove()");
QAction increment = new QAction(this);
increment.setShortcut("Ctrl+I");
increment.triggered.connect(this, "increment()");
addAction(add);
addAction(remove);
addAction(increment);
}
public void setModel(Model model) {
this.model = model;
super.setModel(model);
}
@SuppressWarnings("unused")
private void add() {
List<QModelIndex> pos = selectedIndexes();
for (QModelIndex i : pos) {
Node n = (Node) model.indexToValue(i);
assert n != null;
int size = n.children.size();
Node child = new Node(names[(int) (Math.random() * names.length)],
model);
n.children.add(child);
model.childrenInserted(i, size, size);
}
}
@SuppressWarnings("unused")
private void remove() {
List<QModelIndex> pos = selectedIndexes();
for (QModelIndex i : pos) {
Node n = (Node) model.indexToValue(i);
assert n != null;
if (n == model.root())
continue;
QModelIndex parentIndex = model.parent(i);
Node parent = (Node) model.indexToValue(parentIndex);
parent.children.remove(n);
model.childrenRemoved(parentIndex, i.row(), i.row());
}
}
@SuppressWarnings("unused")
private void increment() {
List<QModelIndex> pos = selectedIndexes();
for (QModelIndex i : pos) {
Node n = (Node) model.indexToValue(i);
n.counter++;
model.dataChanged.emit(i, i);
}
}
private Model model = new Model();
private String names[] = { "Alpha", "Beta", "Gamma", "Delta" };
public static void main(String args[]) {
QApplication.initialize(args);
TreeModelTester w = new TreeModelTester();
w.setWindowTitle("Tree Model Tester");
w.show();
if (args.length > 0 && args[0].equals("--extra-view")) {
for (int i=0; i<3; ++i) {
TreeModelTester t = new TreeModelTester();
t.setWindowTitle("Extra view: " + (i+1));
t.disableGarbageCollection();
t.show();
t.setModel(w.model);
}
}
QApplication.exec();
}
}
_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest