PDA

View Full Version : qtreeview + checkboxes



lamera
12th July 2008, 14:04
hi there!

I had a look at some other threads concerning my question but still I'm not sure how to get this working.

I'm doing an application where I have got a QTreeView in QSplitter. The Tree should fill itself up with data from a QList<QString> and it's depth will be just 1, there should be 2 more columns with checkboxes for the children so:

column0 column1 column2
parent
child checkbox checkbox
child checkbox checkbox
child checkbox checkbox
parent
child checkbox checkbox
parent
child checkbox checkbox
child checkbox checkbox etc.

and the checkboxes should be editable and other elements in the QSplitter will change according to what checkboxes are checked.

So should I implement my own Model and somehow override the flags or is it better to do it just using the QStandardItemModel and then model->setIndexWidget(index,QCheckbox) but then I can't get the children's indexes, it just works for the parents.

Maybe someone's done something similar and could send me a sample?

jpn
13th July 2008, 12:44
You should definitely use checkable items instead of index widgets. Writing a tree model is not trivial so using QStandardItemModel might be easier to start with.

lamera
21st August 2008, 13:32
thanks for your reply.

I tried building my model, but it doesn't work, so I think I'll go back to the QStandardItemModel idea as you suggested.

I had a look at the QT website and there's an example to create a tree:


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


and then:



QTreeView *treeView = new QTreeView(this);
treeView->setModel(myStandardItemModel);
connect(treeView, SIGNAL(clicked(QModelIndex)),
this, SLOT(clicked(QModelIndex)));


and what is this myStandardItemModel? is it just a defined variable in the header file like:


QStandardItemModel *myStandardItemModel;

pls help me out with this. I'm really lost now.

spirit
21st August 2008, 15:20
why you can't use this method


void QStandardItem::setCheckable ( bool checkable )

?

jpn
21st August 2008, 16:45
They are two different code example snippets. Variable names don't necessarily match each others. Just like all over Qt docs, basic C++ knowledge is necessary to understand examples. You should try to concentrate on understanding what they actually do instead of copy-pasting them directly to your own code and compiling blindly. :)

lamera
21st August 2008, 18:08
Right :) thanks a lot for the replies :)

Yeah I got a bit confused and fed up with my work so far but I'm back on the road again.
and now I got a piece of code like this:


model = new QStandardItemModel(0,2,this);
model->setHeaderData(0, Qt::Horizontal, QObject::tr("BLA1"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("BLA2"));
QStandardItem *parentItem = new QStandardItem("parent");
model->appendRow(parentItem);

QStandardItem *subItem1 = new QStandardItem("item1");
parentItem->appendRow(subItem1);

and now I want a checkbox in the second row and second column i.e. next to subitem1, same row, next column, so I do:

model->setData(model->index(1, 1), Qt::Checked, Qt::CheckStateRole);
and thiiiis does nooooot work! tell me why tell me why ;)

wysota
21st August 2008, 19:54
I think you have to make the item checkable first by calling the method mentioned earlier.

lamera
23rd August 2008, 11:24
right so if I use the following statement:

item->setCheckable(true)
in a while loop, how can I now control what happens to the items, I mean, how can I apply some kind of action if the user checks a box?
I tried using the
connect() method, but it doesn't work for QStandarditems...

I'm sorry to bother you guys, I'm a real newbie :)
thanks for any help.

jpn
23rd August 2008, 11:28
You could connect to QStandardItemModel::itemChanged().

lamera
6th September 2008, 22:10
hey there!
Thanks for your help!
I really appreciate it :) I finally did it using

void QStandardItem::setCheckable ( bool checkable )
and then

Qt::CheckState checkState () const to manage what has been checked.
I still got maaaany problems, but I'm moving ahead!
thanks again.