Results 1 to 5 of 5

Thread: Highlight Tree Item Viw

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Highlight Tree Item Viw

    Hello forum,

    I am trying to highlight a particular tree-item based on the string entered by the user in the line edit and i am doing the following:


    1. I subclass the QSortFilterProxyModel and over-ride the data() function as follows:

    1.1. map the model index to the source.

    1.2. Check if the mapped model index is valid or not.

    1.3. And change the color of the background using the background role.

    2. In the widget i have the line edit and the tree view which will be interacting with each other based on the user input.

    3. In the widget's constructor i instantiate the object of the subclass that i have creaed in step 1.

    4. Set its source model.

    5. Set view to both the model and proxy model.

    6. Made a signal & slot connection - whenever the text inside the textedit is changed, the custom slot is called to do the highlighting s follows:


    6.1. Create a regular expression object , which takes the following parameter:

    6.1.1. The text from the line edit - 1st parameter.
    6.1.2. Flag - Qt::CaseInsensitive.
    6.1.3. Flag - Qt::FixedString.


    6.2. Declare two model indexes and initializing as follows(THIS IS WHERE I AM MAKING THE MISTAKE I GUESS):

    Qt Code:
    1. const QModelIndex topLeft = m_highlightModel->index(0,0,QModelIndex());
    2. const QModelIndex bottomRight = m_highlightModel->index(0,m_highlightModel->columnCount()-1,topLeft);
    3.  
    4.  
    5.  
    6.  
    7.  
    8.  
    9.  
    10.  
    11.  
    12. emit proxyModelHighlightChanged(topLeft,bottomRight);
    13.  
    14.  
    15.  
    16. //emit m_highlightModel->dataChanged(topLeft,bottomRight);
    17.  
    18.  
    19. connect(this,SIGNAL(proxyModelHighlightChanged(QModelIndex,QModelIndex)),m_highlightModel,SIGNAL(dataChanged(QModelIndex,QModelIndex)));
    To copy to clipboard, switch view to plain text mode 

    Now i am not getting the effect i am looking for. I am looking for the following:



    1. Whenever the user enters any text in the line edit, the text will be matched against all the tree item and highlighted.


    2. Some of the item may be hidden as the parent item is not expanded. In that case the parent will be expanded and the child will be
    highlighted.


    3. If the entered string does not match , nothing will happen.



    But i am getting the following:


    1. All the items are highlighted, when the application is loaded.

    2. The item inside the tree is filtered when the entered string matches any item, thus reducing the number of items in the tree . I do not want to alter anything inside the

    tree view. I just want to highlight the tree item by changing the background color.


    3. I can match only the parent item, but cannot go down to the child and sub-childs item and high-light it.


    I hope that i am elaborated enough. If it is not clear enough for any one of you, please do not hesitate to comment. It has been
    quite a while i am stuck with this issue. Any tips will be helping a lot.



    NOTE: Couple of days ago my email account has been cracked and it might had spammed the forum. If there were anything totally
    non-related to Qt, it was not me. The account setting is restored now. If those materials disturbed anyone i deeply apologise
    for it.


    Thanks
    Sajjad

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Highlight Tree Item Viw

    Quote Originally Posted by sajis997
    5. Set view to both the model and proxy model.
    You should be setting model to both the view and proxy model (may be just a typo)
    Quote Originally Posted by sajis997
    const QModelIndex topLeft = m_highlightModel->index(0,0,QModelIndex());
    const QModelIndex bottomRight = m_highlightModel->index(0,m_highlightModel->columnCount()-1,topLeft);
    This will not work, as you are just cheking the first row, rest of the rows are ignored


    You should be running the search on all the items recessively something like this

    Qt Code:
    1. // Check for the string, including all children
    2. void filterCheck(const QModelIndex & parent) //Pass root index here, it should take care of rest
    3. {
    4. if(parent.isValid())
    5. {
    6. if(!parent.data().toString().isNull())
    7. {
    8. bool value = false;
    9. if(LineEdit filter is matching) // Check filter
    10. value = true;
    11. emit highlightItem(parent, value);
    12. }
    13.  
    14. int rowCount = parent.model()->rowCount(parent);
    15. int colCount = parent.model()->columnCount(parent);
    16.  
    17. for(int i = 0; i < rowCount; i++)
    18. for(int j = 0; j < colCount; j++)
    19. filterCheck(parent.model()->index(i, j, parent)); //recursive call
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Highlight Tree Item Viw

    Hello Santosh,

    I have tried to follow the suggestion as follows:

    Qt Code:
    1. QVariant H3DHighlighterProxyModel::filterCheck(const QModelIndex &parent) const
    2. {
    3. if(!filterRegExp().isEmpty())
    4. {
    5. if(sourceModel()->data(parent).toString().contains(filterRegExp()))
    6. {
    7. return Qt::yellow;
    8. }
    9. else
    10. {
    11. int rowCount = parent.model()->rowCount(parent);
    12. int columnCount = parent.model()->columnCount(parent);
    13.  
    14. for(int i = 0; i < rowCount; i++)
    15. for(int j = 0; j < columnCount; j++)
    16. filterCheck(parent.model()->index(i,j,parent));
    17. }
    18. }
    19. else
    20. {
    21. //return the default background color
    22. //whatever may it be
    23. return QVariant();
    24. }
    25. }
    26.  
    27.  
    28. QVariant H3DHighlighterProxyModel::data(const QModelIndex &index, int role) const
    29. {
    30. //get the handle to the underlyiing data - map the model index to the source model index
    31. QModelIndex sourceIndex = mapToSource(index);
    32.  
    33. //make sure that the model index is valid
    34. if(!sourceIndex.isValid())
    35. {
    36. return QVariant();
    37. }
    38.  
    39. if(role == Qt::BackgroundRole)
    40. {
    41. return filterCheck(sourceIndex);
    42. }
    43. else
    44. {
    45. return sourceIndex.data(role);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    But i am getting the same effect as before. Do i have to check for like if the the tree-view is expanded or not, because the child item is not highlighted.

    And again,i have sub-classed the QSortFilterProxyModel and i have over-ridden the data() function as you can see in the above code snippet. I am getting the highlighting effect as before on the parent item and not to the child items . The items that are not matched are removed from the tree view. I do not want the tree view to change at all. I just want the matched item to be highlighted and the non-matched item to remain same as it was in the tree-view.

    Any more hint on the issue?


    Regards
    Sajjad

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Highlight Tree Item Viw

    Ok, try this, sub-class QSortFilterProxyModel, and re-implement only data(), like this

    Qt Code:
    1. QVariant H3DHighlighterProxyModel::data(const QModelIndex &index, int role) const
    2. {
    3. if(role == Qt::BackgroundRole)
    4. {
    5. QString text = QSortFilterProxyModel::data(index).toString();
    6.  
    7. if(text.contains(hightLight) and !hightLight.isEmpty()) // QString hightLight; //match string, replace this with QRegExp if required.
    8. return QColor(Qt::lightGray); //hightlight color
    9. }
    10. return QSortFilterProxyModel::data(index, role);
    11. }
    To copy to clipboard, switch view to plain text mode 

    this is simple, and will work

  5. #5
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Highlight Tree Item Viw

    Hello Santosh,

    I am getting the old effect as before. Let me elaborate again , i must be missing something here.

    In one of the previous posts you have mentioned to Set view to both the model and proxy model. Does it mean something as follows:

    treeView->setModel(sourceModel);

    treeView->setModel(proxyModel);


    I do not understand the point of doing above. Instead should not it be something as follows:


    Qt Code:
    1. highlightModel = new highlightModel(this); // subclass of the QSortFilterProxyModel
    2. highlightModel->setSourceModel(sourceModel);
    3.  
    4. treeView->setModel(highlightModel);
    To copy to clipboard, switch view to plain text mode 


    Regards
    Sajjad

Similar Threads

  1. Graphics item highlight
    By mukunda in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2011, 18:21
  2. QTableView item's Highlight in QCombox
    By litterflybug in forum Qt Programming
    Replies: 1
    Last Post: 26th August 2009, 14:06
  3. Replies: 1
    Last Post: 18th November 2008, 06:57
  4. Menu Item has no highlight
    By blackfox in forum Qt Programming
    Replies: 6
    Last Post: 24th September 2008, 12:17
  5. Replies: 14
    Last Post: 9th November 2006, 08:35

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.