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
class MenuItem;
{
Q_OBJECT
public:
enum MenuRoles {
NameRole = Qt::UserRole + 1
};
void addMenu(const MenuItem &menuitem);
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<MenuItem> m_menuItems;
};
class MenuItem
{
public:
explicit MenuItem();
~MenuItem();
private:
MenuModel m_subMenuModel;
};
class MenuItem;
class MenuModel : public QAbstractListModel
{
Q_OBJECT
public:
enum MenuRoles {
NameRole = Qt::UserRole + 1
};
MenuModel(QObject *parent = 0);
void addMenu(const MenuItem &menuitem);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<MenuItem> m_menuItems;
};
class MenuItem
{
public:
explicit MenuItem();
~MenuItem();
void setName(QString name);
private:
QString m_strName;
MenuModel m_subMenuModel;
};
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?
Bookmarks