Results 1 to 6 of 6

Thread: Finding a item in second column of treeView

  1. #1
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Finding a item in second column of treeView

    Hi.
    The topic was discussed couple of time on this forum, but not in the way that satisfies me.
    My tree looks like
    Qt Code:
    1. name DESC
    2. |-> ENTRY ID_100
    3. |-> ENTRY ID_101
    4. |-> ENTRY_2 ID_102
    5. |-> ENTRY_2 ID_103
    To copy to clipboard, switch view to plain text mode 
    It has 2 columns, first with name and second with ID (unique) what I want to do is to find index of ENTRY, when having only ID, and I really don't know how to do that.
    I'm now trying to use the match() function but with no success. When I look for text in first column everything works great.
    Code
    Qt Code:
    1. QModelIndexList Items = model->match(
    2. model->index(0, 0),
    3. Qt::DisplayRole,
    4. QVariant::fromValue(QString("ENTRY_2")),
    5. -1,
    6. Qt::MatchRecursive);
    To copy to clipboard, switch view to plain text mode 
    returns me indexes of all entries with same text, but changing code to
    Qt Code:
    1. QModelIndexList Items = model->match(
    2. model->index(0, 1),
    3. Qt::DisplayRole,
    4. QVariant::fromValue(102),
    5. -1,
    6. Qt::MatchRecursive);
    To copy to clipboard, switch view to plain text mode 
    results in empty list of indexes.
    Is there a simple way to find index of "ENTRY" by finding the ID?

  2. #2
    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

    In your example the DisplayRole of the second column would be QVariant("ID_102") not QVariant(102). If that was not done for illustrative purposes then this may be why your result set is empty.

  3. #3
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Finding a item in second column of treeView

    ID in my code is the same in tree and in code.
    The "ID_" tag in my "visual" illustration is only for shoving that this is ID column not a text from NAME column in real life is equal to "102"

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Finding a item in second column of treeView

    Show us the implementation for data(), please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Finding a item in second column of treeView

    I've manage to solve my problem by changing sqlQuery, to get not only ID but also name. Then I look for entries that match my groupName, and then for every match I check second column for fit with the ID.
    Qt Code:
    1. currentGroupID = query.value("groupID").toInt();
    2. QAbstractItemModel *model = ui->groupTree->model();
    3. QModelIndexList Items = model->match(
    4. model->index(0,0),
    5. Qt::DisplayRole,
    6. QVariant::fromValue(QString(query.value("groupName").toString())),
    7. -1,
    8. Qt::MatchRecursive);
    9. if (Items.size() == 1){
    10. // Only 1 group with this name ID must match
    11. doSomethingWithGroup(Items.first());
    12. }
    13. else{
    14. for(int i=0;i<Items.size();i++){
    15. QModelIndex parent = Items.at(i).parent();
    16. int tmpGroupID = Items.at(i).model()->index(0,1,parent).data().toInt();
    17. if (tmpGroupID == currentGroupID){
    18. doSomethingWithGroup(Items.at(i));
    19. break;
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    It work for me but, the main topic still remains unsolved.
    I use QStandardItemModel so data() is a buildin function.

  6. #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 01:27.

Similar Threads

  1. Finding QwtPlot child item to detach it
    By Momergil in forum Qwt
    Replies: 2
    Last Post: 16th January 2013, 20:01
  2. Replies: 4
    Last Post: 11th September 2011, 02:39
  3. Set the last column of a treeview to minimal size
    By Donner in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2010, 00:08
  4. How to find which column clicked from a treeview.
    By mekos in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2008, 17:44
  5. current item in treeview
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 30th October 2007, 20: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.