Qt-jambi-interest Archive, August 2006
QAbstractItemModel
Message 1 in thread
Hi,
I've decided to try making a custom QTreeView. I am currently stuck on the
createIndex() function. As I would like to use a tree I need to be able pass
the parent and current data into the createIndex(int, int,???) function. I
just don't know how. I have looked the I can either use a QNativePointer or
and int.
I have no idea how to use QNativePointer and in order to use an int I will
have to create a lookup table, which has significant overhead. I intend to
use the table for extensive updates and changes which can occur a few times a
second.
The tree model I intend to use has two layers. The first layer is the "title"
and has a constant column count of 1. The second layer (stem?) can have a
variety of columns but constant in each instance of the tree widget. My
question is can that work? Parents and children of different column count?
If so how would it look?
The Tree model will not have any decoration and always be expanded.
Is there any way to specify the width of a column? Or are they automatic?
I have read though the examples and they have helped a bit. This is my second
attempt and I have had no previous experience in creating custom Model/View
stuff.
Any help would be greatly appreciated.
David
Message 2 in thread
David Naylor wrote:
> I've decided to try making a custom QTreeView. I am currently stuck on the
> createIndex() function. As I would like to use a tree I need to be able pass
> the parent and current data into the createIndex(int, int,???) function. I
> just don't know how. I have looked the I can either use a QNativePointer or
> and int.
>
> I have no idea how to use QNativePointer and in order to use an int I will
> have to create a lookup table, which has significant overhead. I intend to
> use the table for extensive updates and changes which can occur a few times a
> second.
Hi David,
The QTreeView and QAbstractItemModel paradigm doesn't map perfectly into
Java at the moment. We are looking into some various options here to
make this API smoother. The approach with the int / lookup table is the
best alternative if you need a custom model.
The solution that would work right now is to use a QStandardItemModel
like I have illustrated in the example below.
http://doc.trolltech.com/4.1/qstandarditemmodel
Another option is to use the convenience classes QTreeWidget and
QTreeWidgetItem if you don't need model/view:
http://doc.trolltech.com/4.1/qtreewidget
http://doc.trolltech.com/4.1/qtreewidgetitem.html
> The tree model I intend to use has two layers. The first layer is the "title"
> and has a constant column count of 1. The second layer (stem?) can have a
> variety of columns but constant in each instance of the tree widget. My
> question is can that work?
Sure.
> Parents and children of different column count?
> If so how would it look?
Run the example and you'll see
> Is there any way to specify the width of a column? Or are they automatic?
You have access to that through the header.
treeView.header()->resizeSection(index, newSize);
There are also some other options, such as stretch factors etc in the
QHeaderView class:
http://doc.trolltech.com/4.1/qheaderview.html
> I have read though the examples and they have helped a bit. This is my second
> attempt and I have had no previous experience in creating custom Model/View
> stuff.
I hope this helps...
<snip>
import com.trolltech.qt.*;
import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;
public class Test extends QTreeView
{
private QStandardItemModel model = new QStandardItemModel();
public Test() {
model.insertColumns(0, 10);
model.insertRows(0, 10);
for (int i=0; i<10; ++i) {
QModelIndex index = model.index(i, 0);
// Create the title
model.setData(index, "Title for item " + i);
// Create the sub table under it
int count = (int) (Math.random() * 10);
model.insertColumns(0, count, index);
model.insertRows(0, count, index);
for (int r=0; r<count; ++r) {
for (int c=0; c<count; ++c) {
model.setData(model.index(r, c, index), "Subitem ["
+ r + "," + c + "] of " + i);
}
}
}
setModel(model);
}
public static void main(String args[]) {
QApplication.initialize(args);
Test t = new Test();
t.show();
QApplication.exec();
}
}
</snip>