Results 1 to 7 of 7

Thread: QTreeView search

  1. #1
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView search

    Hi folks,

    I don't know is this question was asked before. I haven't found it on the forum, so here I am.

    I'm having trouble with my QtreeView. I'm showing data in multiple columns, acting as a "QTableView with groups". No edit is available, only navigation.

    Even in the simpler case with no groups ( so QTreeView looks exactly like a QTableView ) when user selects a cell and begins typing starts "navigation" through data, as an auto-search. I think that's Qt's behaviour, I coded no line to achieve this. Simply it worked...

    But now does'nt work anymore. User begins to type and only the first 2 chars they type move selection. After 2 chars it stops moving.

    Why ? There is a role in the model I've to implement ? A flag I must set ? I cannot find nothing in documentation ( perhaps I'm not searching in the rigth direction... )

    Any idea ?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView search

    But now does'nt work anymore. User begins to type and only the first 2 chars they type move selection
    what state is NOW ? i mean did u change anything in the code ?

    also what is the speed of typing ? usually the moment you delay typing, the text entered till that time is searched.

  3. The following user says thank you to aamer4yu for this useful post:

    jpujolf (2nd March 2009)

  4. #3
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView search [SOLVED]

    Sorry for the delay. Weekend...

    Quote Originally Posted by aamer4yu View Post
    what state is NOW ? i mean did u change anything in the code ?
    I've found a workaround. Now is finally working, but with some hacking. I've been looking inside QTreeView's "keyboardSearch" method. It moves current view's index when necessary, but it also moves to the first column. I don't know why...

    Perhaps an example would be clearer. Image a "simple" tree structure like this ( it's shown on screen as a plain table ) :

    ROOT
    |---- Child 0 ( 5 columns )
    |---- Child 1 ( 5 columns )
    |---- Child 2 ( 5 columns )
    ...

    If you are selecting third column, and you move up / down with arrows, you may stay on third column. I cannot find the reason because trolls decided to switch on every row change to column 0. So I made this hack :

    Qt Code:
    1. void keyboardSearch ( const QString & search )
    2. {
    3. int iCol = currentIndex().column();
    4. QTreeView::keyboardSearch ( search );
    5. setCurrentIndex ( model()->sibling ( currentIndex().row(), iCol, currentIndex() ) );
    6. }
    To copy to clipboard, switch view to plain text mode 

    Also, I'm doing some minor changes to even method, to catch Left/Right, PgUp/PgDown, Home/End keys and allow user to move in a logical way.

    Qt Code:
    1. void SelectCell ( int Key )
    2. {
    3. int iCol = 0;
    4. switch ( Key )
    5. {
    6. case Qt::Key_Right : iCol = 1; break;
    7. case Qt::Key_Left : iCol = -1; break;
    8. }
    9.  
    10. QModelIndex AUX = currentIndex ();
    11. iCol = qMax ( 0, qMin ( AUX.column() + iCol, model()->columnCount() - 1 ) );
    12. setCurrentIndex ( model()->sibling ( AUX.row(), iCol, currentIndex() ) );
    13. };
    14.  
    15. bool event ( QEvent * qe )
    16. {
    17. if ( qe->type() != QEvent::KeyPress )
    18. return QTreeView::event ( qe );
    19.  
    20. QKeyEvent * ke = dynamic_cast<QKeyEvent *>(qe);
    21. switch ( ke->key() )
    22. {
    23. case Qt::Key_Right :
    24. case Qt::Key_Left :
    25. {
    26. SelectCell ( ke->key() );
    27. qe->accept();
    28. return true;
    29. }
    30. case Qt::Key_Up :
    31. case Qt::Key_Down :
    32. case Qt::Key_PageUp :
    33. case Qt::Key_PageDown :
    34. case Qt::Key_Home :
    35. case Qt::Key_End :
    36. {
    37. int iCol = currentIndex().column();
    38. if ( inherited::event ( qe ) )
    39. {
    40. setCurrentIndex ( model()->sibling ( currentIndex().row(), iCol, currentIndex() ) );
    41. return true;
    42. }
    43. return false;
    44. }
    45. break;
    46.  
    47. <other stuff ...>
    48.  
    49. return QTreeView::event ( qe );
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    I wonder about Qt's default behavior . I cannot find the reason because they consider better to "jump" to the first column every time you change the row of the index.

    They must, at least, consider this option. In many cases ( a great % of times ) when you show a tree with multiple columns, the data has the same number of columns in all the rows.

    also what is the speed of typing ? usually the moment you delay typing, the text entered till that time is searched.
    It's not an speed problem, I've done this on my program startup :

    QApplication::setKeyboardInputInterval ( 1000 );

    So 1 second between keystrokes is sufficient enough for my users...

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView search

    What is your tree widget's QAbstractItemView::selectionBehavior set to ?
    i am not sure if in a tree u can select single cell...

    but seeing ur problem, a tableview seems better... even in qt demo example, you can move the cells with keyboard in the way you want to...ie., maintain the previous row and column..

  6. The following user says thank you to aamer4yu for this useful post:

    jpujolf (2nd March 2009)

  7. #5
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView search

    Quote Originally Posted by aamer4yu View Post
    What is your tree widget's QAbstractItemView::selectionBehavior set to ?
    i am not sure if in a tree u can select single cell...
    Yes, we can ( Sorry, I've heard too much times that phrase... )

    I've set it to single rows. If you test on any QTreeView, when you select a cell with mouse, a dotted square is painted around that cell and if you call currentIndex(), column is set to the one you selected.

    My complains come because even when selecting entire rows, the column where I've clicked MUST persist even if I change to another row, or not ?

    If not, "keyboardSearch" method only is granted to work for first column searches.

    but seeing ur problem, a tableview seems better... even in qt demo example, you can move the cells with keyboard in the way you want to...ie., maintain the previous row and column..
    It must be better if all the cases I've had where the extreme case I've shown ( a simple tree structure ). But a QTableView is not an option, because I've to show tabular data, but user can "group by column" with many levels. So I've to expand/collapse rows,as show in attached picture.

    I will report that to trolls to suggest if is acceptable to them to add new behavior options for QTreeView. You must think that, in the extreme case, a QTreeView looks like a QTableView and must act a s a QTableView.

    As you've seen on my last post, I've solved it with some hacking. I answered myself to legate a possible solution to other people with the same problem. I usually ask for help here, but if my experiences could be useful for others...
    Attached Images Attached Images

  8. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView search

    You are right with your doubt..
    The behaviour can be seen in Qt demo DirView example too...
    though sometime one will ignore the dotted rect, as it is hard to see over a selected area.

  9. #7
    Join Date
    Jul 2006
    Location
    Catalunya - Spain
    Posts
    117
    Thanks
    16
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView search

    Quote Originally Posted by aamer4yu View Post
    You are right with your doubt..
    The behaviour can be seen in Qt demo DirView example too...
    though sometime one will ignore the dotted rect, as it is hard to see over a selected area.
    You're right !! That's the reason I've open another thread, asking for one solution to that too-thin dotted square ( thanls for your answer, I will try it )

    http://www.qtcentre.org/forum/f-qt-p...html#post94657

Similar Threads

  1. Replies: 4
    Last Post: 25th May 2008, 20:01
  2. Qt 4.4.0 Help Module : Search not working
    By Ankitha Varsha in forum Qt Programming
    Replies: 0
    Last Post: 22nd May 2008, 08:45
  3. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22
  4. QTreeView: Holding a line on screen
    By gri in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 11:42

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.