Results 1 to 3 of 3

Thread: QComboBox with custom tree view is not showing its items

  1. #1
    Join Date
    Jul 2019
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default QComboBox with custom tree view is not showing its items

    Hello! I try to make a QComboBox with custom tree view, but combo box is not showing its items when clicking on it. Can you help me please?

    My code:

    mainWindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QWidget>
    5.  
    6. class mainWindow : public QWidget
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit mainWindow(QWidget *parent = nullptr);
    11. ~mainWindow();
    12.  
    13. signals:
    14.  
    15. public slots:
    16. };
    17.  
    18. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "treecombobox.h"
    3. #include <QStandardItem>
    4. #include <QStandardItemModel>
    5. #include <QList>
    6. #include <QString>
    7.  
    8. mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
    9. {
    10. QStandardItem * parentItem = model.invisibleRootItem ();
    11. QStandardItem * item = new QStandardItem (QString ("item 0");
    12. QList<QStandardItem *> list;
    13. list << item;
    14. parentItem-> appendRow(list);
    15.  
    16. parentItem = item;
    17. QStandardItem * item = new QStandardItem (QString ("item 1") ;
    18. list.clear();
    19. QList<QStandardItem *> list;
    20. list << item;
    21. parentItem->appendRow(list);
    22. list.clear();
    23. QStandardItem * item2 = new QStandardItem (QString ("item 2");
    24. list << item2;
    25. parentItem->appendRow(list);
    26. TreeComboBox *t = new TreeComboBox(this);
    27. t->setModel (& model);
    28. t->expandAll();
    29. }
    30.  
    31. mainWindow::~mainWindow(){
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

    treecombobox.h
    Qt Code:
    1. #ifndef TREECOMBOBOX_H
    2. #define TREECOMBOBOX_H
    3.  
    4. #include <QComboBox>
    5. #include <QTreeView>
    6. #include <QWidget>
    7.  
    8. class TreeComboBox final: public QComboBox
    9. {
    10. public:
    11. TreeComboBox (QWidget *parrent = nullptr);
    12. void showPopup () override;
    13. void hidePopup () override;
    14. void hideColumn (int n);
    15. void expandAll ();
    16. void selectIndex (const QModelIndex & index);
    17. private:
    18. QTreeView * m_view = nullptr;
    19. };
    20.  
    21. #endif // TREECOMBOBOX_H
    To copy to clipboard, switch view to plain text mode 

    treecombobox.cpp
    Qt Code:
    1. #include "treecombobox.h"
    2. #include <QHeaderView>
    3.  
    4. TreeComboBox :: TreeComboBox (QWidget *parent) : QComboBox(parent)
    5. {
    6. m_view = new QTreeView;
    7. m_view-> setFrameShape (QFrame :: NoFrame);
    8. m_view-> setEditTriggers (QTreeView :: NoEditTriggers);
    9. m_view-> setAlternatingRowColors (true);
    10. m_view-> setSelectionBehavior (QTreeView :: SelectRows);
    11. m_view-> setRootIsDecorated (false);
    12. m_view-> setWordWrap (true);
    13. m_view-> setAllColumnsShowFocus (true);
    14. m_view-> setItemsExpandable (false);
    15. setView (m_view);
    16. m_view-> header () -> setVisible (false);
    17. }
    18.  
    19. void TreeComboBox :: hideColumn (int n)
    20. {
    21. m_view-> hideColumn (n);
    22. }
    23.  
    24. void TreeComboBox :: expandAll ()
    25. {
    26. m_view-> expandAll ();
    27. }
    28.  
    29. void TreeComboBox :: selectIndex (const QModelIndex & index)
    30. {
    31. setRootModelIndex (index.parent ());
    32. setCurrentIndex (index.row ());
    33. m_view-> setCurrentIndex (index);
    34. }
    35.  
    36. void TreeComboBox :: showPopup ()
    37. {
    38. setRootModelIndex (QModelIndex ());
    39. QComboBox :: showPopup ();
    40. }
    41.  
    42. void TreeComboBox :: hidePopup ()
    43. {
    44. setRootModelIndex (m_view-> currentIndex (). parent ());
    45. setCurrentIndex (m_view-> currentIndex (). row ());
    46. QComboBox :: hidePopup ();
    47. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char * argv[]){
    5. QApplication app(argc, argv);
    6. mainWindow *w = new mainWindow();
    7. w->resize(100, 100);
    8. w->show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox with custom tree view is not showing its items

    Qt Code:
    1. mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
    2. {
    3. ...
    4. }
    To copy to clipboard, switch view to plain text mode 

    How long do you think lives your model?

  3. #3
    Join Date
    Jul 2019
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox with custom tree view is not showing its items

    And what I need to do?


    Added after 19 minutes:


    Quote Originally Posted by INeedADollar View Post
    And what I need to do?
    I found what is wrong. Thank you very much!
    Last edited by INeedADollar; 19th September 2019 at 18:40.

Similar Threads

  1. QComboBox with a tree model - adding items problem.
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2017, 11:33
  2. Replies: 5
    Last Post: 4th March 2015, 23:02
  3. [Snippet] QComboBox showing tree models
    By Auliyaa in forum Qt Programming
    Replies: 0
    Last Post: 25th November 2011, 15:42
  4. Multible QCombobox tree with model view
    By mrandreas in forum Newbie
    Replies: 2
    Last Post: 22nd September 2010, 15:00
  5. QComboBox not showing all items
    By musikit in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 17:01

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.