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:
int QCsvTableModel
::rowCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return csvMatrix.rowCount();
}
int QCsvTableModel
::columnCount(const QModelIndex &parent
) const {
Q_UNUSED(parent);
return csvMatrix.columnCount();
}
{
if (index.isValid())
if (role == Qt::DisplayRole || role == Qt::EditRole)
return csvMatrix.at(index.row(), index.column());
}
{
if (index.isValid() && role == Qt::EditRole) {
csvMatrix.setValue(index.row(), index.column(), value.toString());
emit dataChanged(index,index); // No difference if dataChanged is emitted or not
return true;
}
return false;
}
int QCsvTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return csvMatrix.rowCount();
}
int QCsvTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return csvMatrix.columnCount();
}
QVariant QCsvTableModel::data(const QModelIndex &index, int role) const
{
if (index.isValid())
if (role == Qt::DisplayRole || role == Qt::EditRole)
return csvMatrix.at(index.row(), index.column());
return QVariant();
}
bool QCsvTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
csvMatrix.setValue(index.row(), index.column(), value.toString());
emit dataChanged(index,index); // No difference if dataChanged is emitted or not
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
MainWindow source:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!fileName.isEmpty()) {
QCsvTableModel *model = new QCsvTableModel(this);
if (extension.toLower() == "csv") // known file extension
model->loadFromFile(fileName);
ui->tableView->setModel(model);
} // if fileName ..
connect(ui->tableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onModelsDataChanged(const QModelIndex&, const QModelIndex&)));
//connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onModelsDataChanged(const QModelIndex&, const QModelIndex&))); // Didn't work either ..
}
{
Q_UNUSED(topLeft);
Q_UNUSED(bottomRight);
qDebug() << "The data has changed." << endl;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString fileName = ":/value.csv";
if (!fileName.isEmpty()) {
QCsvTableModel *model = new QCsvTableModel(this);
QString extension = QFileInfo(QFile(fileName)).completeSuffix();
if (extension.toLower() == "csv") // known file extension
model->loadFromFile(fileName);
ui->tableView->setModel(model);
} // if fileName ..
connect(ui->tableView->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onModelsDataChanged(const QModelIndex&, const QModelIndex&)));
//connect(model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(onModelsDataChanged(const QModelIndex&, const QModelIndex&))); // Didn't work either ..
}
void MainWindow::onModelsDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
Q_UNUSED(topLeft);
Q_UNUSED(bottomRight);
qDebug() << "The data has changed." << endl;
}
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
Bookmarks