Results 1 to 6 of 6

Thread: Finding a item in second column of treeView

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Finding a item in second column of treeView

    Ok. The default implementation of match() only searches in a column (as documented). The only column in which a recursive search makes sense is column 0 (the only one with a hierarchy) and that column does not contain the values you are seeking. You need to provide a reimplementation of match() if you wish to recurse down column 0 but search a different (or all) columns in each row.

    You could also/instead store the ID on the Qt::UserRole of the item in column 0 to allow match() to search that way.
    Qt Code:
    1. #include <QApplication>
    2. #include <QStandardItemModel>
    3. #include <QTreeView>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9.  
    10. for (int i = 0; i < 4; ++i) {
    11. QStandardItem *parentItem = model.invisibleRootItem();
    12. for (int j = 0; j < 3; ++j) {
    13. QStandardItem *item0 = new QStandardItem(QString("item %0").arg(j));
    14. item0->setData(100 + j, Qt::UserRole);
    15. QStandardItem *item1 = new QStandardItem(QString::number(100 + j));
    16. parentItem->appendRow(QList<QStandardItem*>() << item0 << item1);
    17. parentItem = item0;
    18. }
    19. }
    20.  
    21. QModelIndexList matches = model.match(
    22. model.index(0,0),
    23. Qt::UserRole,
    24. QVariant(102),
    25. -1,
    26. Qt::MatchRecursive);
    27.  
    28. QTreeView view;
    29. view.setModel(&model);
    30. view.show();
    31. view.expandAll();
    32. foreach (const QModelIndex &match, matches)
    33. view.selectionModel()->select(match, QItemSelectionModel::Select);
    34.  
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisW67; 20th September 2013 at 00:27.

Similar Threads

  1. Finding QwtPlot child item to detach it
    By Momergil in forum Qwt
    Replies: 2
    Last Post: 16th January 2013, 19:01
  2. Replies: 4
    Last Post: 11th September 2011, 01:39
  3. Set the last column of a treeview to minimal size
    By Donner in forum Qt Programming
    Replies: 1
    Last Post: 21st August 2010, 23:08
  4. How to find which column clicked from a treeview.
    By mekos in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 16:44
  5. current item in treeview
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 19:26

Tags for this Thread

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.