R
R
Robotex2014-08-02 01:39:29
Qt
Robotex, 2014-08-02 01:39:29

Qt: QAbstractItemModel is slow if I throw a layoutChanged() signal. What is the problem?

I have inherited from QAbstractItemModel and am using this model in TableView in QML.
I am trying to remove an element:

bool DownloadListModel::removeDownloadById(const QString &id)
{
    DownloadFile* download = getDownloadById(id);
    int index = getDownloadIndex(download);
    if (download) {
        beginRemoveRows(QModelIndex(),index,index);

        _downloadsList.removeOne(download);
        _downloadsMap.remove(id);
        download->deleteLater();

        _db->deleteDownload(id);

        endRemoveRows();

        return true;
    }
    return false;
}

This works, but there is one thing: when I select an element in the table, another element is highlighted (if one is deleted, the element directly above the selected one will be highlighted). So I'm trying to send the layoutChanged() signal:
bool DownloadListModel::removeDownloadById(const QString &id)
{
    DownloadFile* download = getDownloadById(id);
    int index = getDownloadIndex(download);
    if (download) {
        emit layoutAboutToBeChanged();
        beginRemoveRows(QModelIndex(),index,index);

        _downloadsList.removeOne(download);
        _downloadsMap.remove(id);
        download->deleteLater();

        _db->deleteDownload(id);

        endRemoveRows();
        emit layoutChanged();

        return true;
    }
    return false;
}

Now it works correctly, but slowly. What is the problem? How to do it right?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xseven, 2014-08-04
@xseven

It's hard to say what works slowly without seeing all the code.
To begin with, I would advise you to go through the profiler + provide a link to the code if possible.
In general, a somewhat strange approach in my opinion.
Why is removeRows/removeColumns and their insert antagonists not used?
If you really want removeById, why not display it as an adapter?
It is somewhat easier, in my opinion, to use the capabilities of the interface of the standard model to the maximum.
Plus how is the index calculated and where are the dataChanged signals? This is probably why you use signaling for layout. Because without sending dataChanged, you should not hope for the correct display or selection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question