qabstractlistmodel

QListView with QAbstractListModel shows an empty list

喜夏-厌秋 提交于 2019-12-01 02:12:36
问题 I have created a very simple example of QListView with a custom QAbstractListModel . The QListView is displayed but it is empty. What am I doing wrong? Code: #include <QListView> #include <QAbstractListModel> #include <QApplication> class DataModel: public QAbstractListModel { public: DataModel() : QAbstractListModel() {} int rowCount( const QModelIndex & parent = QModelIndex() ) const { return 2; } QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const { return "a"; } }

How do you access the roles of the currentItem from a listview in QML?

£可爱£侵袭症+ 提交于 2019-11-30 06:13:39
I'm trying to access a role from a ListView in QML. Essentially, I have this in my QML: ListView { id: myId model: myModel delegate: Item { Text { text: model.text } Text { text: model.moreText } } } myModel is a QAbstractListModel implementation. The QML portion of this is a reusable component, so the model could have any number of different roles with various data types. What I would like to do is bind to the value of a given role of the currentItem property of the ListView. In other words, I'd like to have some other Component on the page that could bind a property to the currently selected

Remove rows from QAbstractListModel

丶灬走出姿态 提交于 2019-11-29 07:19:29
I have a custom model which derives from QAbstractListModel which is exposed to QML. I need to support operations to add new items and remove existing items. While insertion operation works without any problems, removal operation causes the application to crash while calling endRemoveRows() function. void GPageModel::addNewPage() { if(m_pageList.count()<9) { beginInsertRows(QModelIndex(),rowCount(),rowCount()); GPage * page = new GPage(); QQmlEngine::setObjectOwnership(page,QQmlEngine::CppOwnership); page->setParent(this); page->setNumber(m_pageList.count()); page->setName("Page " + QString: