Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractTableModel data function is called many time.
Forum Updated to NodeBB v4.3 + New Features

QAbstractTableModel data function is called many time.

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 2.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

    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);
    }
    
    

    Do what you want.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      What else do you have in your MainWindow ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Taz742T 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

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

        What else do you have in your MainWindow ?

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #3

        @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

        Do what you want.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          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

          1 Reply Last reply
          2
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            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?)

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved