Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QAbstractTableModel data function is called many time.



  • QAbstractTableModeldata function is called many time. When i resize MainWindow i see that it resizes slowly.
    I think that it is caused by data function and how can i solve it?

    #include "mymodel.h"
    #include "QDebug"
    
    MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
    {
    
    }
    
    int MyModel::rowCount(const QModelIndex &parent) const
    {
        return this->array.size();
    }
    
    int MyModel::columnCount(const QModelIndex &parent) const
    {
        return 5;
    }
    
    QVariant MyModel::data(const QModelIndex &index, int role) const
    {
        static int cnt = 0;
        qDebug() << "dataaa: " << ++cnt;
        if(role == Qt::DisplayRole) {
            return this->array[index.row()][index.column()];
        }
        return QVariant::Invalid;
    }
    
    void MyModel::addNewData(QVector<int> newRow)
    {
        qDebug() << "add new data";
        this->array.insert(this->array.begin(), newRow);
        QModelIndex first(createIndex(0, 4)), second(createIndex(this->array.size(), 4));
        emit dataChanged(first, second);
    }
    
    void MyModel::atachList(QVector<QVector<int> > v)
    {
        beginInsertRows(QModelIndex(), 0, v.size() - 1);
        this->array = v;
        QModelIndex first(createIndex(0, 4)), second(createIndex(this->array.size(), 4));
        emit dataChanged(first, second);
        endInsertRows();
    }
    
    
    
    void MainWindow::on_pushButton_clicked()
    {
        MyModel *model = static_cast<MyModel*>(ui->tableView->model());
        QVector<int> v;
        v.push_back(5);
        v.push_back(15);
        v.push_back(25);
        v.push_back(35);
        v.push_back(45);
        model->addNewData(v);
    }
    
    void MainWindow::on_pushButton_2_clicked()
    {
        QVector<QVector<int> > array;
        for (int i = 1; i <= 1000; i++) {
            QVector<int> v;
            for (int j = 1; j <= 5; j++) {
                v.push_back((i + j) * 10);
            }
            array.push_back(v);
        }
        MyModel *model = static_cast<MyModel*>(ui->tableView->model());
        model->atachList(array);
    }
    
    

  • Lifetime Qt Champion

    Hi,

    Printing in the data function might slow things down a bit.

    What else do you have in your MainWindow ?



  • @SGaist
    I have just one tableView button and frame. http://b.pix.ge:81/j/zmvkw.gif http://a.pix.ge:81/n/sxo8n.gif


  • Lifetime Qt Champion

    Hi
    It seems the sliding out effect ( altering the area for the TableView)
    cause full redraw of all 5000 items. ( 1000 rows, 5 cols)

    Its easy to see from sample fi you first open/collapse a few times, then presses
    the attach button and fill the table. then try again.

    This is also supported from the fact that smaller window or few rows removes this
    noticeable delay.

    sample to test with.
    https://www.dropbox.com/s/rfgcpf7ueh1npha/MyTableViewModel.zip?dl=0



  • To add to @mrjj

    • data is called multiple times with different roles but given your initail if it should execute very fast.
    • addNewData forces a refresh fo the whole model, is this what you really want?
    • addNewData does not call beginInsertRows/endInsertRow
    • emitting dataChanged on an index of thew inserted row before calling endInsertRows I would call undefined behaviour (officially that row does not exist yet so how can its data change?)

Log in to reply