qabstractitemmodel

Understanding Qt view-model architecture: when to create and how to cleanup indexes in QAbstractItemModel implementation?

做~自己de王妃 提交于 2019-12-01 06:41:53
问题 I'm currently migrating my project from QTreeWidget to QtreeView , and have a lot of problems caused by poor understanding of the Qt model-view design. So far I couldn't find answers even in Qt examples. I've implemented my QAbstractItemModel . I'm returning strings to be viewed in the QTreeView through data method. Now, the underlying data will change in runtime. To handle this my model is subscribed to a notification that does emit dataChanged(index(0,0), index(rowCount() - 1, LastColumn));

Qt QAbstractItemModel - item removal crashes

懵懂的女人 提交于 2019-12-01 06:00:02
问题 I'm writing an application using Qt classes where I have a hierarchical structure and I need to display it in a tree view (I'm using a QTreeView widget). The data itself look like this: class StatisticsEntry { // additional data management methods ... bool setData(int column, const QVariant &value) { if (column < 0 || column >= _data.size()) return false; _data[column] = value; return true; } private: // each item has a parent item except the top-most one StatisticsEntry *_parent; // each

When to emit dataChanged from a QAbstractItemModel

狂风中的少年 提交于 2019-12-01 05:01:43
问题 In Qt, I have a model subclassing QAbstractItemModel - it's a tree displayed in a QTreeView. The model supports various forms of change which all work OK. The two of relevance are: 1) Some data in a small number of related rows changes 2) A visualisation change means that the majority of rows should change their formatting - in particular they have a change of background highlighting. Their DisplayRole data does not change. The current design deals with both of these in the same way: for

To set widgets on children items on QTreeView

眉间皱痕 提交于 2019-11-30 15:41:36
Thanks to this thread , I'm able to add widgets to 2nd or later column of QAbstractItemView (in my example QTreeView ) of top level items of a view . But is it possible to add widgets to the children items? Here's what I've tried which partly went well: #!/usr/bin/env python import os from PyQt4.QtCore import QModelIndex, Qt from PyQt4.QtGui import QApplication, QItemSelectionModel, \ QPushButton, QStandardItem, \ QStandardItemModel, QTreeView from PyQt4.uic import loadUi class PrvTreeviewNest(QTreeView): def __init__(self): super(PrvTreeviewNest, self).__init__() loadUi('/home/user

How to implement QML ListModel like get method for an QAbstractListModel derived model

安稳与你 提交于 2019-11-30 02:13:26
I want to use an QAbstractListModel derived model in QML. Binding the model to views already works great. The next thing I want achieve is the ability to access specific items and their role like it is possible with a QML ListModel grid.model.get(index).DisplayRole But I have no idea how to implement this get method in my QAbstractListModel derived model. Any hints? You can add an Q_INVOKABLE function to the QAbstractItemModel derived class like this: ... Q_INVOKABLE QVariantMap get(int row); ... QVariantMap get(int row) { QHash<int,QByteArray> names = roleNames(); QHashIterator<int,

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:

How to implement QML ListModel like get method for an QAbstractListModel derived model

寵の児 提交于 2019-11-28 23:13:10
问题 I want to use an QAbstractListModel derived model in QML. Binding the model to views already works great. The next thing I want achieve is the ability to access specific items and their role like it is possible with a QML ListModel grid.model.get(index).DisplayRole But I have no idea how to implement this get method in my QAbstractListModel derived model. Any hints? 回答1: You can add an Q_INVOKABLE function to the QAbstractItemModel derived class like this: ... Q_INVOKABLE QVariantMap get(int