Results 1 to 9 of 9

Thread: Creating a nested QAbstractListModel containing a QAbstractListModel

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Creating a nested QAbstractListModel containing a QAbstractListModel

    To create a nested QAbstractListModel containing a QAbstractListModel

    I have a structure i.e., Menu containing sub menu
    I'm creating the ListModel in Qt subclassing the QAbstractListModel

    Here's the code

    Qt Code:
    1. class MenuItem;
    2.  
    3. class MenuModel : public QAbstractListModel
    4. {
    5. Q_OBJECT
    6. public:
    7. enum MenuRoles {
    8. NameRole = Qt::UserRole + 1
    9. };
    10.  
    11. MenuModel(QObject *parent = 0);
    12.  
    13. void addMenu(const MenuItem &menuitem);
    14.  
    15. int rowCount(const QModelIndex & parent = QModelIndex()) const;
    16.  
    17. QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    18.  
    19. protected:
    20. QHash<int, QByteArray> roleNames() const;
    21.  
    22. private:
    23. QList<MenuItem> m_menuItems;
    24. };
    25.  
    26. class MenuItem
    27. {
    28. public:
    29. explicit MenuItem();
    30. ~MenuItem();
    31. void setName(QString name);
    32. private:
    33. QString m_strName;
    34. MenuModel m_subMenuModel;
    35. };
    To copy to clipboard, switch view to plain text mode 

    However I get compile time error saying " error: C2248: 'QAbstractListModel::QAbstractListModel' : cannot access private member declared in class 'QAbstractListModel' "

    Is it possible to structure this way? or is there a better way?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    MenuModel is a QObject derived class.
    QObject has disabled copy constructor and assignment operator, it cannot be copied.
    MenuItem holds a MenuModel as a normal member.
    It doesn't have a copy constructor or assignment operator so the default generated ones apply.
    Default copy constructor and assignment operator do a member-by-member copy.
    MenuItem is therefore not copyable as the m_subMenuModel is not copyable.
    QList requires that its value type is copyable.

    You could use QList<MenuItem*> or a single tree model

    Cheers,
    _

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

    volcano (9th March 2016)

  4. #3
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    Thanks anda_skoa, the advice really helped me.

  5. #4
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    One more doubt. I managed to set the submenu and return using

    Declaration
    Qt Code:
    1. MenuModel *subMenu();
    To copy to clipboard, switch view to plain text mode 

    Definition
    Qt Code:
    1. MenuModel* MenuItem::subMenu()
    2. {
    3. return &m_subMenuModel;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Now I need to set the role and return using the function
    Qt Code:
    1. QVariant MenuModel::data(const QModelIndex & index, int role) const
    2. {
    3. if (index.row() < 0 || index.row() >= m_menuItems.count())
    4. return QVariant();
    5.  
    6. MenuItem *menu = m_menuItems[index.row()];
    7. switch (role) {
    8. case NameRole:
    9. return menu->menuName();
    10. case SubMenuRole:
    11. return menu->subMenu();
    12. default:
    13. return QVariant();
    14. break;
    15. }
    16. return QVariant();
    17. }
    To copy to clipboard, switch view to plain text mode 


    But i get the error on line "return menu->subMenu();" saying "error: C2248: 'QVariant::QVariant' : cannot access private member declared in class 'QVariant'"

    Kindly advice what i'm missing

  6. #5
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    I managed to export the model to qml using the line

    Qt Code:
    1. MenuModel* MenuModel::subMenuModel(int index)
    2. {
    3. return m_menuItems.at(index)->subMenu();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is this the right way to do so?

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    Quote Originally Posted by volcano View Post
    But i get the error on line "return menu->subMenu();" saying "error: C2248: 'QVariant::QVariant' : cannot access private member declared in class 'QVariant'"
    maybe you need a
    Qt Code:
    1. Q_DECLARE_METATYPE(MenuModel*);
    To copy to clipboard, switch view to plain text mode 
    in the menu model header.

    Quote Originally Posted by volcano View Post
    I managed to export the model to qml using the line

    Qt Code:
    1. MenuModel* MenuModel::subMenuModel(int index)
    2. {
    3. return m_menuItems.at(index)->subMenu();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is this the right way to do so?
    You mean as a Q_INVOKABLE function?
    That is also ok, but you need to make sure that the ownership of the pointer is clear.
    Either make sure that each model has a parent, or use QQmlEngine::setObjectOwnership() to use the ownership to C++ on the pointer before returning it.

    Otherwise QML will delete/garbage collect the model when it doesn't need it anymore and your C++ code might not like that

    Cheers,
    _

  8. #7
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    That is also ok, but you need to make sure that the ownership of the pointer is clear.
    Either make sure that each model has a parent, or use QQmlEngine::setObjectOwnership() to use the ownership to C++ on the pointer before returning it.
    Could you elaborate more on how to achieve this?

    As currently, i'm using the invokable method to return the model. The model is updated when menu model has changed. After some changes the app crashes with the error "Second Chance Assertion Failed: File f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp, Line 52".

    This is caused by the problem you mentioned
    Otherwise QML will delete/garbage collect the model when it doesn't need it anymore and your C++ code might not like that
    Can you suggest how to handle this?

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    As I said, use QQmlEngine::setObjectOwnership():
    Qt Code:
    1. MenuModel* MenuModel::subMenuModel(int index)
    2. {
    3. MenuModel *subMenu = m_menuItems.at(index)->subMenu();
    4. QQmlEngine::setObjectOwnership(subMenu, QQmlEngine::CppOwnership);
    5. return subMenu;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    volcano (11th March 2016)

  11. #9
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Creating a nested QAbstractListModel containing a QAbstractListModel

    Thanks anda_skoa for all the advice and help, it really helped me.

Similar Threads

  1. QAbstractListModel
    By Archa4 in forum Newbie
    Replies: 9
    Last Post: 9th February 2011, 14:43
  2. Problem with QAbstractListModel
    By chandan in forum Newbie
    Replies: 0
    Last Post: 12th April 2010, 00:24
  3. QAbstractListModel problem
    By UltimatePace in forum Qt Programming
    Replies: 5
    Last Post: 27th September 2009, 10:51
  4. Problem with QAbstractListModel
    By eekhoorn12 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2009, 14:26
  5. QAbstractListModel searching.
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:41

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.