Hi all,

I already read a few threads on this forum dealing with similar issues but didn't provide "the" solution.

I built my own QAbstractTableModel class which fills a QTableView with data from a .csv file. The file is used for logging purposes and stores its (new) data every 10 minutes. The data is shown inside a QTableView but needs to be updated manually (button or closing and reopening the QDialog). What I want to achieve is that the view is automatically updated whenever new data is written to the .csv file.

The code snippets of my TableModel looks as follows:
Qt Code:
  1. int QCsvTableModel::rowCount(const QModelIndex &parent) const
  2. {
  3. Q_UNUSED(parent);
  4. return csvMatrix.rowCount();
  5. }
  6.  
  7. int QCsvTableModel::columnCount(const QModelIndex &parent) const
  8. {
  9. Q_UNUSED(parent);
  10. return csvMatrix.columnCount();
  11. }
  12.  
  13. QVariant QCsvTableModel::data(const QModelIndex &index, int role) const
  14. {
  15. if (index.isValid())
  16. if (role == Qt::DisplayRole || role == Qt::EditRole)
  17. return csvMatrix.at(index.row(), index.column());
  18. return QVariant();
  19. }
  20.  
  21. bool QCsvTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
  22. {
  23. if (index.isValid() && role == Qt::EditRole) {
  24. csvMatrix.setValue(index.row(), index.column(), value.toString());
  25.  
  26. emit dataChanged(index,index); // No difference if dataChanged is emitted or not
  27.  
  28. return true;
  29. }
  30.  
  31. return false;
  32. }
To copy to clipboard, switch view to plain text mode 

MainWindow source:
Qt Code:
  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6.  
  7. QString fileName = ":/value.csv";
  8.  
  9. if (!fileName.isEmpty()) {
  10. QCsvTableModel *model = new QCsvTableModel(this);
  11.  
  12. QString extension = QFileInfo(QFile(fileName)).completeSuffix();
  13.  
  14. if (extension.toLower() == "csv") // known file extension
  15. model->loadFromFile(fileName);
  16.  
  17. ui->tableView->setModel(model);
  18. } // if fileName ..
  19.  
  20. connect(ui->tableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onModelsDataChanged(const QModelIndex&, const QModelIndex&)));
  21. //connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onModelsDataChanged(const QModelIndex&, const QModelIndex&))); // Didn't work either ..
  22. }
  23.  
  24. void MainWindow::onModelsDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
  25. {
  26. Q_UNUSED(topLeft);
  27. Q_UNUSED(bottomRight);
  28.  
  29. qDebug() << "The data has changed." << endl;
  30. }
To copy to clipboard, switch view to plain text mode 

But the dataChanged() signal isn't fired at all and "The data has changed." never shows up.

What am I missing? Any help is appreciated!

Michael