Since there is a lot of code, I'll take off a piece) I do the following. Wrote my model from QAbstractTableModel. Tied to QTableView. By timer (0.5 sec) I update the list of processes and put it in a table, while calculating the difference between the previous and updated list of processes and, in accordance with this difference, I add or delete rows in the table, and then rewrite the data. The problem is this: when displaying a table, the data does not change at all. However, if you select a row or scroll the table back and forth, then they are updated ... Some kind of problem with drawing something) How to fix this? Thanks in advance)

void ProcessMonitor::updateProcessList() { QDir *dir = new QDir("/proc"); QStringList dirProcessList = dir->entryList(QStringList("*"), QDir::AllDirs); QFile stat; int len = processList.size(); processList.clear(); foreach(QString str, dirProcessList) { if (str.toInt()) { stat.setFileName("/proc/" + str.toUtf8() + "/stat"); stat.open(QIODevice::ReadOnly); processList.append(getProcessInfoByStat(&stat)); stat.close(); } else continue; } int dif = len - processList.size(); if (dif < 0) { processTableModel->insertRows(0, -dif, QModelIndex()); } else if (dif > 0) { processTableModel->removeRows(0, dif, QModelIndex()); } for (int i = 0; i < processList.size(); i++) { processTableModel->setData(processTableModel->index(i, 0), processList.at(i)->comm, Qt::DisplayRole); processTableModel->setData(processTableModel->index(i, 1), processList.at(i)->pid, Qt::DisplayRole); processTableModel->setData(processTableModel->index(i, 2), processList.at(i)->stat, Qt::DisplayRole); processTableModel->setData(processTableModel->index(i, 3), processList.at(i)->nice, Qt::DisplayRole); processTableModel->setData(processTableModel->index(i, 4), processList.at(i)->cpu, Qt::DisplayRole); processTableModel->setData(processTableModel->index(i, 5), processList.at(i)->stime, Qt::DisplayRole); } sort(); } 

    1 answer 1

    Good day!

    By that part of the code that you provided it is difficult to judge exactly what the problem is. However, there are several places where a mistake can be made. Their essence boils down to about one thing: the view (i.e., QTableView or its successor) is not notified in any way about changes that have occurred in the model .

    1) Implementing setData () in your model. The Qt documentation for this function says:

    The dataChanged () signal should be emitted if the data was successfully set.

    Check whether your model emits a dataChanged() signal when the model element has changed successfully.

    2) The default implementation of insertRows () does nothing according to the Qt documentation:

    Note: This function doesn’t need to return false.

    If you overlap the implementation of insertRows() check that there are beginInsertRows() and endInsertRows() calls inside its implementation, again according to the documentation:

    If you want to support your own model, you can reimplement this function. Alternatively, you can provide your own API for altering the data. In both cases, you need to call you.

    I wish you success!

    PS "If nothing works - finally, read the instructions" (c) popular wisdom))

    • one
      Thanks a lot) The problem was in dataChanged (). From now on I will try to follow the popular wisdom;) - cub4d
    • Mark the answer as correct) - progzdeveloper