PDA

View Full Version : QTreeView Problem



sujan.dasmahapatra
6th March 2009, 10:14
Dear Friends
I am into the development of a software for fluid engineering application. I am in the initial stage of the development.I am able to load a geometry through dialog.Now when the geometry is loaded that part’s name that is the file’s name should be updated in my tree view.

Now what I have done that I want to tell u. Please understand. I am creating a QTreeView which is set on the screen beside the central widget which is a QGraphicsView.Now for treeView I have done it like this ,

QTreeView *treeView;
QStandardItemModel *model;
QStandardItem *item;

treeView = new QTreeView;
model = new QStandardItemModel;
treeView->setModel(model);
treeView->show();

Now where in the open SLOT I am getting the fileName when my part is being read or loaded.That time I am doing like this
item = new QStandardItem(filename); // filename is QString
model->appendRow(item);

This way I am able to load the geometry and the filename is also updated in the treeView.Now I want to select the geometry from the treeView.If I load more than one geometry then I want to select that name in the treeView and then on right mouse press I should get 3 options select if not selected,deselect if selected and delete.And I want to perform the operations.
Could anyone please tell me whether my approach is correct and what I need to do to make selection from the treeView.

roxton
7th March 2009, 11:32
Find the selected index first:



const QModelIndex CMyClass::index_from_name (const QString &name)
{
QList <QStandardItem *> lst = model->findItems (name); //assuming model is the field of the class
if (lst.size() > 0)
return model->indexFromItem (lst[0]);
else
return QModelIndex();
}


Then, to select the item by the filename, do something like this:


QModelIndex index = index_from_name (filename);
tree_view->selectionModel()->setCurrentIndex (index, QItemSelectionModel::Select);
tree_view->scrollTo (index);