Insert and delete rows in QTreeView - c++

Good day, I Have base model inherited from QAbstractItemModel, and some background threads which notify this model from time to time, in examples the insertions rows implemens somthing like this
bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
bool success;
beginInsertRows(parent, position, position + rows - 1);
success = parentItem->insertChildren(position, rows, rootItem->columnCount());
endInsertRows();
return success;
}
But I can't do it like this because my model is single which uses 4 views, I've implemented my insertion this way:
void notifyEventImpl(file_item_type *sender,helper<ITEM_ACTION_ADDED>)
{
base_class::setSize(file_item_type::size()+sender->size());
m_listDirectory.push_back(sender);
file_item_type::filesystem_type::s_notify.insert(this); // notify my model
}
Where s_notify is a class with implementation:
void Notifaer::dataChange(void * item){emit dataChanged(item);}
void Notifaer::remove(void * item){emit removed(item);}
void Notifaer::insert(void * item){emit inserted(item);}
void Notifaer::push_back(const FileItemModel * model)
{
VERIFY(QObject::connect(this,SIGNAL(dataChanged(void*)),model,SLOT(dataChangeItem(void*)) ));
VERIFY(QObject::connect(this,SIGNAL(removed(void*)),model,SLOT(removeItem(void*)) ));
VERIFY(QObject::connect(this,SIGNAL(inserted(void*)),model,SLOT(insertItem(void*)) ));
}
Given this, I invoke the method:
void FileItemModel::insertItem(void *it)
{
file_item_type *item = dynamic_cast<file_item_type*>(static_cast<file_item_type*>(it));
{
QModelIndex index = createIndex(0,0,item);
if (index.isValid())
{
beginInsertRows(index, 0, item->childCount()-1);
endInsertRows();
}
}
}
void FileItemModel::removeItem(void *it)
{
file_item_type *item = static_cast<file_item_type*>(it);
{
QModelIndex index = createIndex(0,0,item);
if (index.isValid())
{
beginRemoveRows(index, 0, item->childCount()-1);
endRemoveRows();
}
}
}
Remove rows works perfectly, but insert does not work. What's wrong in my implementation?

Try with
beginInsertRows(QModelIndex(), 0, item->childCount()-1);
Have you checked QT doc http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html or QT examples to get any clue http://qt-project.org/doc/qt-4.8/itemviews-editabletreemodel.html?
As you said threads, maybe this could be interesting to read:
Design Pattern, Qt Model/View and multiple threads
QTreeView & QAbstractItemModel & insertRow

Related

Qt: Updating data in a TableView with QAbstractTableModel

I try to implement a table view with two columns. The right columns shows parameters, which should not be changed by runtime, and left column shows values, that should be updated constantly at runtime. For this I implement a data model (derived from QAbstractTableModel). After setting up this I got a table with 3 rows and 2 columns and the right columns shows the parameters. The left columns however remains empty. And after hours I didn't find a solution for this.
This is the relevant code of my data model I think:
QVariant DataTableModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
if (index.row() >= m_parameter.size()) {
return QVariant();
}
if (role == Qt::DisplayRole || role == Qt::EditRole) {
switch (index.column()) {
case 0:
return m_parameter.at(index.row());
break;
case 1:
return m_value.at(index.row());
break;
}
}
return QVariant();
}
bool DataTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!index.isValid() && role != Qt::EditRole) {
return false;
}
//change only values in column 1
if (index.column() == 1) {
m_value.replace(index.row(), value.toString());
emit dataChanged(index, index, { role });
return true;
}
return true;
}
bool DataTableModel::insertRows(int row, int count, const QModelIndex& parent)
{
//the following two methods emits signals that tells the view that the data should be changed
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int i = 0; i < count; ++i) {
m_parameter.insert(row, "");
m_value.insert(row, "");
}
endInsertRows();
return true;
}
In the main class I have a function that calles the functions of the model:
void QVideoMeter::QAddTableContent()
{
QVariant value(QValueList());
m_tableDataModel->insertRows(0, 3, QModelIndex()); //insert 3 rows
m_tableDataModel->AddData(QParameterList());
m_tableDataModel->setData(QModelIndex(), value, 2);
}
QList<QString> QVideoMeter::QValueList()
{
QList<QString> values;
values.append("Test");
values.append("Hallo");
values.append("Welt");
return values;
}
Please, can someone of you review my code and tell me what I'm doing wrong?
Refer to the below two links. They provide the necessary concepts and examples:
QAbstractItemModel Subclass
Editable Tree Model Example
An editable model needs to provide implementations of setData() and
setHeaderData(), and must return a suitable combination of flags from
its flags() function.
Since this example allows the dimensions of the model to be changed,
we must also implement insertRows(), insertColumns(), removeRows(),
and removeColumns().
void MainWindow::insertRow()
{
const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (!model->insertRow(index.row()+1, index.parent()))
return;
updateActions();
for (int column = 0; column < model->columnCount(index.parent()); ++column) {
const QModelIndex child = model->index(index.row() + 1, column, index.parent());
model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
}
}

How to use Qt beginInsertRows correctly

I have my custom ItemModel and ItemDelegate:
class ItemModel : public QAbstractListModel {
Q_OBJECT
public:
// return items_.size();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
// return items_[index.row()];
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
void Insert(const QVector<QString> &data);
private:
QVector<QString> items_;
};
void ItemModel::Insert(const QVector<QString> &data) {
// my question is the 'first' and 'last' args of beginInsertRows
beginInsertRows(QModelIndex(), 0, 0);
items_.insert(items_.begin(), data.begin(), begin.end());
endInsertRows();
}
From Qt Documentation, it say beginInsertRows has three args:
void QAbstractItemModel::beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
When reimplementing insertRows() in a subclass, you must call this function before inserting data into the model's underlying data store.
The parent index corresponds to the parent into which the new rows are inserted; first and last are the row numbers that the new rows will have after they have been inserted.
I am not sure how to pass last and first, in my ItemModel::Insert, whatever the size of the inserted data is 0 or 10 or other count, I pass first = 0 and last = 0, the view work correctly. When I insert 10 items, and pass first = 0 and last = 9, the view work correctly too. It confuse me.
void ItemModel::Insert() {
beginInsertRows(QModelIndex(), 0, 0);
for(int i = 0; i < 10; ++i) {
items_.push_back(QString::number(i);
}
endInsertRows();
}
// or
void ItemModel::Insert() {
beginInsertRows(QModelIndex(), 0, 9);
for(int i = 0; i < 10; ++i) {
items_.push_back(QString::number(i));
}
endInsertRows();
}
0, 0 is not correct because "first and last are the row numbers that the new rows will have after they have been inserted." The view may still look correct using these parameters but there might be problems from it you haven't seen yet.
0, 9 is correct, but only the first time Insert() is called, because you are adding the new numbers to the end. You need to add items_.size() to both parameters. I.e.:
beginInsertRows(QModelIndex(), items_.size(), items_.size() + 9);
As the doc you highlighted says, you need to call those two functions when you reimplement the virtual function QAbstractItemModel::insertRows
https://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows
Which is obviously not what you are doing in your ItemModel::Insert function.

How to create a Generic ListModel for QML

My application consists of many Lists that I display in QML-ListViews by using QAbstractListModel derived model-classes. It's always the same, with the difference of the Item-Type. That's why I want to know how to build a class-template for this approach.
I figured out, that it is not possible to use the Q_OBJECT-Macro in a class-template. That's why my GenericListModel consists of two part.
1. GenericListModelData
The first part is the Model itself that derives from QAbstractListModel and implements the basic functions data(), rowCount() and roleNames().
2. GenericListModel
The second part is the class-template that is used as a wrapper to provide functions similar to a QListView.
If you have any suggestions or questions please let me know. It would be really nice to improve this solution.
I uploaded the full sourcecode here:
https://github.com/sebabebibobu/QGenericListModel/
1. GenericListModelData
QVariant GenericListModelData::data(const QModelIndex &index, int role) const
{
QObject *item = m_itemList.at(index.row());
return item->property(item->metaObject()->property(role).name());
}
/*
* Returns the number of items attached to the list.
*/
int GenericListModelData::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_itemList.size();
}
/*
* Generates a hash out of QMetaObject property-index and property-name.
*/
QHash<int, QByteArray> GenericListModelData::roleNames() const
{
QHash<int, QByteArray> roles;
if (!m_itemList.isEmpty()) {
for(int i = 0; i < m_itemList.at(0)->metaObject()->propertyCount(); i++) {
roles[i] = m_itemList.at(0)->metaObject()->property(i).name();
}
}
return roles;
}
/*
* Append Item to List.
*/
void GenericListModelData::appendItem(QObject *item)
{
/* map the notify()-signal-index with the property-index when the first item get's inserted */
if (m_itemList.isEmpty()) {
for(int i = 0; i < item->metaObject()->propertyCount(); i++) {
m_propertySignalIndexHash.insert(item->metaObject()->property(i).notifySignalIndex(), i);
}
}
/* connect each notify()-signals to the onDataChanged()-slot which call's the dataChanged()-signal */
for(int i = 0; i < item->metaObject()->propertyCount(); i++) {
connect(item, "2" + item->metaObject()->property(i).notifySignal().methodSignature(), this, SLOT(onDataChanged()));
}
/* finally append the item the list */
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_itemList.append(item);
endInsertRows();
}
/*
* Helper-Slot that emit's the dataChanged()-signal of QAbstractListModel.
*/
void GenericListModelData::onDataChanged()
{
QModelIndex index = createIndex(m_itemList.indexOf(sender()),0);
QVector<int> roles;
roles.append(m_propertySignalIndexHash.value(senderSignalIndex()));
emit dataChanged(index, index, roles);
}
2. GenericListModel
template <typename T>
class GenericListModel : public GenericListModelData
{
public:
explicit GenericListModel(QObject *parent) : GenericListModelData(parent) {
}
void append(T *item) {
appendItem(item);
}
T *at(int i) {
return qobject_cast<T *>(m_itemList.at(i));
}
};
Update 01.05.2016
GrecKo posted in the comments, that a project like mine already exists. That's why I decided to share the link of this project here too:
http://gitlab.unique-conception.org/qt-qml-tricks/qt-qml-models

How to refresh a QSqlTableModel while preserving the selection?

I am using a QSqlTableModel and QTableView to view an SQLite database table.
I would like to have the table auto refresh every second or so (it's not going to be a very large table - a couple of hundred rows). And i can do this - like so:
QTimer *updateInterval = new QTimer(this);
updateInterval->setInterval(1000);
updateInterval->start();
connect(updateInterval, SIGNAL(timeout()),this, SLOT(update_table()));
...
void MainWindow::update_table()
{
model->select(); //QSqlTableModel*
sqlTable->reset(); //QTableView*
}
But this removes any selection I have, so the selections only last for up to a second. This is annoying, as another pane in the GUI depends on what is selected. If nothing is selected, then it resets to an explanation splash page.
I then tried a somewhat hacky approach, which gets the selected row number, resets the table, and then selects that row. But this doesn't work either, as the selected row can move up or down based on additions to the table.
I know other classes have a dataChanged() signal, which would be ideal.
Do any of you know how I could have the table refresh to reflect changes to the database (from either command line usage, or other instances of the program) AND keep the current selection?
I know I could get data from the current selection, and then after the reset search for the same row and then reselect it, but this seems like a counter productive and bad solution to the problem.
EDIT: Current attempt at solution:
void MainWindow::update_table()
{
QList<QModelIndex> selection = sqlTable->selectionModel()->selection().indexes();
QList<int> selectedIDs;
bool somethingSelected = true;
for(QList<QModelIndex>::iterator i = selection.begin(); i != selection.end(); ++i){
int col = i->column();
QVariant data = i->data(Qt::DisplayRole);
if(col == 0) {
selectedIDs.append(data.toInt());
}
}
if(selectedIDs.empty()) somethingSelected = false;
model->select();
sqlTable->reset();
if(somethingSelected){
QList<int> selectedRows;
int rows = model->rowCount(QModelIndex());
for(int i = 0; i < rows; ++i){
sqlTable->selectRow(i);
if(selectedIDs.contains(sqlTable->selectionModel()->selection().indexes().first().data(Qt::DisplayRole).toInt())) selectedRows.append(i);
}
for(QList<int>::iterator i = selectedRows.begin(); i != selectedRows.end(); ++i){
sqlTable->selectRow(*i);
}
}
}
Okay so this more or less works now...
The real deal is the primary key on the result of your query. Qt's APIs offer a rather circuitous route from QSqlTableModel::primaryKey() to a list of columns. The result of primaryKey() is a QSqlRecord, and you can iterate over its field()s to see what they are. You can also look up all of the fields that comprise the query proper from QSqlTableModel::record(). You find the former in the latter to get a list of model columns that comprise the query.
If your query doesn't contain a primary key, you'll have to design one yourself and offer it using some protocol. For example, you can choose that if primaryKey().isEmpty() is true, the last column returned by the model is to be used as the primary key. It's up to you to figure out how to key the result of an arbitrary query.
The selected rows can then be indexed simply by their primary keys (a list of values of the cells that comprise the key -- a QVariantList). For this, you could use a custom selection model (QItemSelectionModel) if its design wasn't broken. The key methods, such as isRowSelected() aren't virtual, and you can't reimplement them :(.
Instead, you can use a proxy model that mimicks selection by providing a custom Qt::BackgroundRole for the data. Your model sits on top of the table model, and keeps a sorted list of selected keys. Each time the proxy model's data() is called, you get the row's key from underlying query model, then search for it in your sorted list. Finally, you return a custom background role if the item is selected. You'll have to write relevant comparison operator for QVariantList. If QItemSelectionModel was usable for this purpose, you could put this functionality into a reimplementation of isRowSelected().
The model is generic since you subscribe to a certain protocol for extracting the key from a query model: namely, using primaryKey().
Instead of using primary keys explicitly, you can also use persistent indices if the model supports them. Alas, until at lest Qt 5.3.2, QSqlTableModel does not preserve the persistent indices when the query is rerun. Thus, as soon as the view changes the sort order, the persistent indices become invalid.
Below is a fully worked out example of how one might implement such a beast:
#include <QApplication>
#include <QTableView>
#include <QSqlRecord>
#include <QSqlField>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QIdentityProxyModel>
#include <QSqlDatabase>
#include <QMap>
#include <QVBoxLayout>
#include <QPushButton>
// Lexicographic comparison for a variant list
bool operator<(const QVariantList &a, const QVariantList &b) {
int count = std::max(a.count(), b.count());
// For lexicographic comparison, null comes before all else
Q_ASSERT(QVariant() < QVariant::fromValue(-1));
for (int i = 0; i < count; ++i) {
auto aValue = i < a.count() ? a.value(i) : QVariant();
auto bValue = i < b.count() ? b.value(i) : QVariant();
if (aValue < bValue) return true;
}
return false;
}
class RowSelectionEmulatorProxy : public QIdentityProxyModel {
Q_OBJECT
Q_PROPERTY(QBrush selectedBrush READ selectedBrush WRITE setSelectedBrush)
QMap<QVariantList, QModelIndex> mutable m_selection;
QVector<int> m_roles;
QBrush m_selectedBrush;
bool m_ignoreReset;
class SqlTableModel : public QSqlTableModel {
public:
using QSqlTableModel::primaryValues;
};
SqlTableModel * source() const {
return static_cast<SqlTableModel*>(dynamic_cast<QSqlTableModel*>(sourceModel()));
}
QVariantList primaryValues(int row) const {
auto record = source()->primaryValues(row);
QVariantList values;
for (int i = 0; i < record.count(); ++i) values << record.field(i).value();
return values;
}
void notifyOfChanges(int row) {
emit dataChanged(index(row, 0), index(row, columnCount()-1), m_roles);
}
void notifyOfAllChanges(bool remove = false) {
auto it = m_selection.begin();
while (it != m_selection.end()) {
if (it->isValid()) notifyOfChanges(it->row());
if (remove) it = m_selection.erase(it); else ++it;
}
}
public:
RowSelectionEmulatorProxy(QObject* parent = 0) :
QIdentityProxyModel(parent), m_roles(QVector<int>() << Qt::BackgroundRole),
m_ignoreReset(false) {
connect(this, &QAbstractItemModel::modelReset, [this]{
if (! m_ignoreReset) {
m_selection.clear();
} else {
for (auto it = m_selection.begin(); it != m_selection.end(); ++it) {
*it = QModelIndex(); // invalidate the cached mapping
}
}
});
}
QBrush selectedBrush() const { return m_selectedBrush; }
void setSelectedBrush(const QBrush & brush) {
if (brush == m_selectedBrush) return;
m_selectedBrush = brush;
notifyOfAllChanges();
}
QList<int> selectedRows() const {
QList<int> result;
for (auto it = m_selection.begin(); it != m_selection.end(); ++it) {
if (it->isValid()) result << it->row();
}
return result;
}
bool isRowSelected(const QModelIndex &proxyIndex) const {
if (! source() || proxyIndex.row() >= rowCount()) return false;
auto primaryKey = primaryValues(proxyIndex.row());
return m_selection.contains(primaryKey);
}
Q_SLOT void selectRow(const QModelIndex &proxyIndex, bool selected = true) {
if (! source() || proxyIndex.row() >= rowCount()) return;
auto primaryKey = primaryValues(proxyIndex.row());
if (selected) {
m_selection.insert(primaryKey, proxyIndex);
} else {
m_selection.remove(primaryKey);
}
notifyOfChanges(proxyIndex.row());
}
Q_SLOT void toggleRowSelection(const QModelIndex &proxyIndex) {
selectRow(proxyIndex, !isRowSelected(proxyIndex));
}
Q_SLOT virtual void clearSelection() {
notifyOfAllChanges(true);
}
QVariant data(const QModelIndex &proxyIndex, int role) const Q_DECL_OVERRIDE {
QVariant value = QIdentityProxyModel::data(proxyIndex, role);
if (proxyIndex.row() < rowCount() && source()) {
auto primaryKey = primaryValues(proxyIndex.row());
auto it = m_selection.find(primaryKey);
if (it != m_selection.end()) {
// update the cache
if (! it->isValid()) *it = proxyIndex;
// return the background
if (role == Qt::BackgroundRole) return m_selectedBrush;
}
}
return value;
}
bool setData(const QModelIndex &, const QVariant &, int) Q_DECL_OVERRIDE {
return false;
}
void sort(int column, Qt::SortOrder order) Q_DECL_OVERRIDE {
m_ignoreReset = true;
QIdentityProxyModel::sort(column, order);
m_ignoreReset = false;
}
void setSourceModel(QAbstractItemModel * model) Q_DECL_OVERRIDE {
m_selection.clear();
QIdentityProxyModel::setSourceModel(model);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget w;
QVBoxLayout layout(&w);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (! db.open()) return 255;
QSqlQuery query(db);
query.exec("create table chaps (name, age, constraint pk primary key (name, age));");
query.exec("insert into chaps (name, age) values "
"('Bob', 20), ('Rob', 30), ('Sue', 25), ('Hob', 40);");
QSqlTableModel model(nullptr, db);
model.setTable("chaps");
RowSelectionEmulatorProxy proxy;
proxy.setSourceModel(&model);
proxy.setSelectedBrush(QBrush(Qt::yellow));
QTableView view;
view.setModel(&proxy);
view.setEditTriggers(QAbstractItemView::NoEditTriggers);
view.setSelectionMode(QAbstractItemView::NoSelection);
view.setSortingEnabled(true);
QObject::connect(&view, &QAbstractItemView::clicked, [&proxy](const QModelIndex & index){
proxy.toggleRowSelection(index);
});
QPushButton clearSelection("Clear Selection");
QObject::connect(&clearSelection, &QPushButton::clicked, [&proxy]{ proxy.clearSelection(); });
layout.addWidget(&view);
layout.addWidget(&clearSelection);
w.show();
app.exec();
}
#include "main.moc"

Qt QAbstractItemModel - item removal crashes

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 item may have child items which are accessible through the following member
QList<StatisticsEntry*> _children;
// each item is defined by a set of vallues stored in the following vector
QVector<QVariant> _data;
}
I have a class called it StatisticsModel that implements QAbstractItemModel - use it to manipulate and present the data stored in the StatisticsEntry tree.
The class has a method called addStatisticsData which I use to push StatisticsEntry records into the model. The method roughly looks like this:
QModelIndex StatisticsModel::addStatisticsData(const QString &title, const QString &description, const QModelIndex &parent)
{
int row = rowCount(parent);
if (!insertRow(row, parent))
return QModelIndex();
// Get new item index
QModelIndex child = index(row, 0, parent);
// set item data
setTitle(child, title);
setDescription(child, description);
return child;
}
SetTitle and setDescription methods are identical - here's the setTitle one:
void StatisticsModel::setTitle(const QModelIndex &index, const QString& title)
{
QModelIndex columnIndex = this->index(index.row(), StatColumn::Title, index.parent());
this->setData(columnIndex, title, Qt::EditRole);
}
The setData method is as follows:
bool StatisticsModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role != Qt::EditRole)
return false;
int column = index.column();
StatisticsEntry *item = getItem(index);
if (!item->setData(column, value))
return false;
emit dataChanged(index, index);
return true;
}
What is left is the getItem method:
StatisticsEntry *StatisticsModel::getItem(const QModelIndex &index) const
{
if (index.isValid())
{
StatisticsEntry *item = static_cast<StatisticsEntry*>(index.internalPointer());
if (item)
return item;
}
return _rootItem;
}
That is about all there is when speaking of adding new and modifying existing entries. In my application I have implemented QSortFilterProxyModel as well - nothing special, just the lessThan method. I use the proxy model to provide a sort feature in the QTreeView that displays the data. There is the following code that connects the models to the treeview widget:
in the header of the main window:
...
StatisticsModel* _statisticsModel;
StatisticsProxyModel* _statisticsProxyModel;
...
in the constructor of the main widow
...
_statisticsModel = new StatisticsModel(ths);
_statisticsProxyModel = new StatisticsProxyModel(htis);
...
_statisticsProxyModel->setSourceModel(_statisticsModel);
ui->statisticsTreeView->setModel(_statisticsProxyModel);
...
The application also has a button that allows me to remove the selected item from the model - I'm currently only testing with the QTreeView::selectionModel()->currentIndex, no multi-selections for the time being.
I have the following code for the StatisticsModel::removeRows method
bool StatisticsModel::removeRows(int position, int rows, const QModelIndex &parent)
{
StatisticsEntry *parentItem = getItem(parent);
bool success1 = true;
bool success2 = true;
beginRemoveRows(parent, position, position + rows - 1);
for (int i = position + rows - 1; i >= position; i--)
{
QModelIndex child = index(i, 0, parent);
QString title = this->title(child); // the title method is the getter method that matches the setTitle one
success1 = success1 && removeRows(0, rowCount(child), child); //rowCount is implemented to return the number of items stored in StatisticsEntry::_children list for the specified parent index
success2 = success2 && parentItem->removeChild(i); // deletes an entry from the StatisticsEntry::_children list
}
endRemoveRows();
return success1 && success2;
}
The problem is that sometimes when I remove an item using QAbstractItemModel::removeRow method I get an exception and the stack trace looks like:
StatisticsModel::parent StatisticsModel.cpp 307 0x13e7bf8
QModelIndex::parent qabstractitemmodel.h 393 0x72e57265
QSortFilterProxyModelPrivate::source_to_proxy qsortfilterproxymodel.cpp 386 0x711963e2
QSortFilterProxyModel::mapFromSource qsortfilterproxymodel.cpp 2514 0x7119d28b
QSortFilterProxyModel::parent qsortfilterproxymodel.cpp 1660 0x7119a32c
QModelIndex::parent qabstractitemmodel.h 393 0x72e57265
QPersistentModelIndex::parent qabstractitemmodel.cpp 347 0x72e58b86
QItemSelectionRange::isValid qitemselectionmodel.h 132 0x70e3f62b
QItemSelection::merge qitemselectionmodel.cpp 466 0x711503d1
QItemSelectionModelPrivate::finalize qitemselectionmodel_p.h 92 0x7115809a
QItemSelectionModelPrivate::_q_rowsAboutToBeRemoved qitemselectionmodel.cpp 623 0x71151132
QItemSelectionModel::qt_static_metacall moc_qitemselectionmodel.cpp 113 0x711561c2
QMetaObject::activate qobject.cpp 3547 0x72e8d9a4
QAbstractItemModel::rowsAboutToBeRemoved moc_qabstractitemmodel.cpp 204 0x72f08a76
QAbstractItemModel::beginRemoveRows qabstractitemmodel.cpp 2471 0x72e5c53f
QSortFilterProxyModelPrivate::remove_proxy_interval qsortfilterproxymodel.cpp 558 0x71196ce7
QSortFilterProxyModelPrivate::remove_source_items qsortfilterproxymodel.cpp 540 0x71196c7f
QSortFilterProxyModelPrivate::source_items_about_to_be_removed qsortfilterproxymodel.cpp 841 0x71197c77
QSortFilterProxyModelPrivate::_q_sourceRowsAboutToBeRemoved qsortfilterproxymodel.cpp 1291 0x711995cc
QSortFilterProxyModel::qt_static_metacall moc_qsortfilterproxymodel.cpp 115 0x7119d506
QMetaCallEvent::placeMetaCall qobject.cpp 525 0x72e883fd
QObject::event qobject.cpp 1195 0x72e894ba
QApplicationPrivate::notify_helper qapplication.cpp 4550 0x709d710e
QApplication::notify qapplication.cpp 3932 0x709d4d87
QCoreApplication::notifyInternal qcoreapplication.cpp 876 0x72e6b091
Oddly enough this seems to happen after all immediate method calls concerning the item removal are already over. It seems that the proxy model is looking for model indices that should not be present anymore (or so I think). The StatisticsModel::parent method is as follows:
QModelIndex StatisticsModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
StatisticsEntry *childItem = getItem(index);
StatisticsEntry *parentItem = childItem->parent();
if (NULL != parentItem)
return createIndex(parentItem->childNumber(), 0, parentItem);
return QModelIndex();
}
When the exception happens the values associated with the childItem and parentItem variables from the above method seem invalid - either the pointers themselves point to non-accessible memory or the member QLists either have no entries or their entries give Memory access violation.
It may be that the parent method is not correct, but then how to fetch the parent index - the qabstractItemModel documentation discourages the usage of QModelIndex::parent in that method for it will create an infinite recursion.
Any help would be appreciated,
When doing your StatisticsModel::removeRows, You are nesting begingRemoveRows/endRemoveRows, which, AFAIK, does not work. You should do:
bool StatisticsModel::removeRows(int position, int rows, const QModelIndex &parent)
{
StatisticsEntry *parentItem = getItem(parent);
bool success1 = true;
bool success2 = true;
for (int i = position + rows - 1; i >= position; i--)
{
QModelIndex child = index(i, 0, parent);
QString title = this->title(child); // the title method is the getter method that matches the setTitle one
success1 = success1 && removeRows(0, rowCount(child), child); //rowCount is implemented to return the number of items stored in StatisticsEntry::_children list for the specified parent index
beginRemoveRows(parent, i, i);
success2 = success2 && parentItem->removeChild(i); // deletes an entry from the StatisticsEntry::_children list
endRemoveRows();
}
return success1 && success2;
}