Results 1 to 12 of 12

Thread: TreeView as view for ComboBox

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default TreeView as view for ComboBox

    Hello,

    is this supported?
    Qt Code:
    1. int main(int argc, char **argv)
    2. {
    3. QApplication app(argc,argv);
    4. QDirModel* model = new QDirModel();
    5. QTreeView* view = new QTreeView();
    6. view->setModel(model);
    7. QComboBox* combo = new QComboBox();
    8. combo->setView(view);
    9. combo->setModel(model);
    10. combo->show();
    11. app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    If so - what do I make wrong?
    (The view isn't rendered correctly - only 10px height, selecting a file doesn't work)

    thanks,
    niko

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: TreeView as view for ComboBox

    Here's something you can start with.
    Attached Files Attached Files
    J-P Nurmi

  3. The following 2 users say thank you to jpn for this useful post:

    niko (4th February 2008), qtCuckoo (11th April 2012)

  4. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TreeView as view for ComboBox

    I'm getting the same result as before; see attached screenshot.

    Does it work better for you?

    (Qt version 4.3.3)
    Attached Images Attached Images

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: TreeView as view for ComboBox

    What if you change in showPopup():
    Qt Code:
    1. setRootModelIndex(QModelIndex());
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. setRootModelIndex(static_cast<QDirModel*>(model())->index(QDir::rootPath()));
    To copy to clipboard, switch view to plain text mode 
    ?
    J-P Nurmi

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

    niko (5th February 2008)

  7. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TreeView as view for ComboBox

    great! that works now!!

    Why did you add the eventFilter and the skipNextHide? I disabled it and it still worked for me.

    thanks,
    niko

  8. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: TreeView as view for ComboBox

    Quote Originally Posted by niko View Post
    Why did you add the eventFilter and the skipNextHide? I disabled it and it still worked for me.
    Otherwise you can't expand/collapse branches without the popup view getting hid...
    J-P Nurmi

  9. #7
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TreeView as view for ComboBox

    aah, i see

    many thanks!!

  10. #8
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TreeView as view for ComboBox

    I have to bring this topic up again, because the solution is not really working for me.
    The problem is that QComboBox calculates the height of the popup by counting the root-items.
    Jpn suggested using another root, so that there are more items. But in my case this doesn't work - as it can happen that there is only one item.

    My modified testcase:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TreeComboBox : public QComboBox
    4. {
    5. public:
    6. TreeComboBox(QWidget* parent = 0) : QComboBox(parent), skipNextHide(false)
    7. {
    8. QTreeView* v = new QTreeView(this);
    9. setView(v);
    10. v->header()->hide();
    11. v->viewport()->installEventFilter(this);
    12. }
    13.  
    14. bool eventFilter(QObject* object, QEvent* event)
    15. {
    16. if (event->type() == QEvent::MouseButtonPress && object == view()->viewport())
    17. {
    18. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
    19. QModelIndex index = view()->indexAt(mouseEvent->pos());
    20. if (!view()->visualRect(index).contains(mouseEvent->pos()))
    21. skipNextHide = true;
    22. }
    23. return false;
    24. }
    25.  
    26. virtual void showPopup()
    27. {
    28. setRootModelIndex(QModelIndex());
    29. QComboBox::showPopup();
    30. }
    31.  
    32. virtual void hidePopup()
    33. {
    34. setRootModelIndex(view()->currentIndex().parent());
    35. setCurrentIndex(view()->currentIndex().row());
    36. if (skipNextHide)
    37. skipNextHide = false;
    38. else
    39. QComboBox::hidePopup();
    40. }
    41.  
    42. private:
    43. bool skipNextHide;
    44. };
    45.  
    46. int main(int argc, char* argv[])
    47. {
    48. QApplication app(argc, argv);
    49. TreeComboBox combo;
    50. model.appendRow(new QStandardItem("Test1"));
    51. model.item(0)->appendRow(new QStandardItem("Test1.1"));
    52. model.item(0)->appendRow(new QStandardItem("Test1.2"));
    53. model.item(0)->appendRow(new QStandardItem("Test1.3"));
    54. combo.setModel(&model);
    55. combo.show();
    56. app.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: TreeView as view for ComboBox

    I can't think of any better solution than faking the amount of top level items:
    Qt Code:
    1. class Model : public QStandardItemModel
    2. {
    3. public:
    4. int rowCount(const QModelIndex& index = QModelIndex()) const
    5. {
    6. int count = QStandardItemModel::rowCount(index);
    7. return !index.isValid() ? qMax(10, count) : count; // at least 10 when top-level
    8. }
    9. };
    To copy to clipboard, switch view to plain text mode 
    This will, however, give false branches whilst expanding the tree. I guess one could get rid of them with a hackish reimplementation of QTreeView::drawBranches().
    J-P Nurmi

  12. #10
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    21
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TreeView as view for ComboBox

    That dind't help - because for those non-existing items the TreeView returned a height of 0 in the visualRect function.

    My solution was to re-implement visualRect for the TreeView - and modify the height of the last top-level row.

    see attached source.

    However I still have a problem:
    steps to reproduce (with attached application):
    * open popup
    * select 1.2
    * open popup again
    * close branch
    * Test 1 gets the new root?!
    Attached Files Attached Files
    Last edited by niko; 5th April 2008 at 22:15. Reason: spelling error

  13. #11
    Join Date
    Sep 2007
    Posts
    31
    Thanked 1 Time in 1 Post

    Default Re: TreeView as view for ComboBox

    Setting minimum size helped me avoiding the height problem, although it's not perfect:

    Qt Code:
    1. comboBox->view()->setMinimumHeight(comboBox->count()*10);
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Oscar

  14. #12
    Join Date
    May 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: TreeView as view for ComboBox

    Quote Originally Posted by niko View Post
    However I still have a problem:
    steps to reproduce (with attached application):
    * open popup
    * select 1.2
    * open popup again
    * close branch
    * Test 1 gets the new root?!
    Does anyone know how to solve this?

    removing lines
    setRootModelIndex(view()->currentIndex().parent());
    setCurrentIndex(view()->currentIndex().row());
    will solve this problem partially.

    If ComboBox is non-editable it work correct, but if it is editable bug comes back.

Similar Threads

  1. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 09:50

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.