Results 1 to 2 of 2

Thread: QTreeView Problem

  1. #1
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default QTreeView Problem

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView Problem

    Find the selected index first:

    Qt Code:
    1. const QModelIndex CMyClass::index_from_name (const QString &name)
    2. {
    3. QList <QStandardItem *> lst = model->findItems (name); //assuming model is the field of the class
    4. if (lst.size() > 0)
    5. return model->indexFromItem (lst[0]);
    6. else
    7. return QModelIndex();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Then, to select the item by the filename, do something like this:
    Qt Code:
    1. QModelIndex index = index_from_name (filename);
    2. tree_view->selectionModel()->setCurrentIndex (index, QItemSelectionModel::Select);
    3. tree_view->scrollTo (index);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTreeView and QDirmodel 's problem?
    By ggs0110 in forum Qt Programming
    Replies: 8
    Last Post: 31st July 2012, 12:11
  2. QTreeView Problem.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 22nd December 2009, 05:55
  3. Qtreeview selection highlighted problem?
    By thefisher in forum Qt Programming
    Replies: 4
    Last Post: 24th November 2006, 09:50
  4. Problem with index in a QTreeView
    By hubert_p in forum Qt Programming
    Replies: 4
    Last Post: 22nd October 2006, 15:54
  5. Selection problem in QTreeView
    By Valheru in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2006, 16:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.