This should be simple, but I can't see how to do it.
From the mainwiindow of the application, I create a QSqlTableModel and QTableView and show the view. It pops up in a nice separate window.
void MainWindow
::showLog(QString fileName
) { QString logTitle
= fileName.
mid(fileName.
lastIndexOf("/")+1,
(fileName.
indexOf(".")-4)-(fileName.
lastIndexOf("/")+1));
QString logName
= logTitle
+ "_log.sqlite";
db.setDatabaseName(logName);
db.open();
model->setTable("log");
model->select();
view->setModel(model);
view->setWindowTitle(logTitle);
view->setMinimumSize(1200,100);
view->move(35,80);
view->setSortingEnabled(TRUE);
view->show();
ControlDB ctrl;
ctrl.setLastLog(logTitle);
}
void MainWindow::showLog(QString fileName) {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QString logTitle = fileName.mid(fileName.lastIndexOf("/")+1,(fileName.indexOf(".")-4)-(fileName.lastIndexOf("/")+1));
QString logName = logTitle + "_log.sqlite";
db.setDatabaseName(logName);
db.open();
QSqlTableModel *model = new QSqlTableModel;
model->setTable("log");
model->select();
QTableView *view = new QTableView;
view->setModel(model);
view->setWindowTitle(logTitle);
view->setMinimumSize(1200,100);
view->move(35,80);
view->setSortingEnabled(TRUE);
view->show();
ControlDB ctrl;
ctrl.setLastLog(logTitle);
}
To copy to clipboard, switch view to plain text mode
Then I insert into the database behind the model. This works ok. I can see the data in the database with a browser tool.
void MainWindow::writeLog() {
ControlDB ctrl;
QString logTitle
= ctrl.
getLastLog();
QString logName
= logTitle
+ "_log.sqlite";
int newID = ctrl.getMaxID(logName) + 1;
db.setDatabaseName(logName);
db.open();
query.prepare("INSERT into log (id, call, sent, rcvd, date, timeon)VALUES(?, ?, ?, ?, ?, ?)");
query.addBindValue(newID);
query.addBindValue(ui->mwCall->text());
query.addBindValue(ui->mwSent->text());
query.addBindValue(ui->mwRcvd->text());
query.addBindValue(d);
query.addBindValue(t);
query.exec();
}
void MainWindow::writeLog() {
ControlDB ctrl;
QString logTitle = ctrl.getLastLog();
QString logName = logTitle + "_log.sqlite";
int newID = ctrl.getMaxID(logName) + 1;
QTime t(QTime::currentTime());
QDate d(QDate::currentDate());
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(logName);
db.open();
QSqlQuery query;
query.prepare("INSERT into log (id, call, sent, rcvd, date, timeon)VALUES(?, ?, ?, ?, ?, ?)");
query.addBindValue(newID);
query.addBindValue(ui->mwCall->text());
query.addBindValue(ui->mwSent->text());
query.addBindValue(ui->mwRcvd->text());
query.addBindValue(d);
query.addBindValue(t);
query.exec();
}
To copy to clipboard, switch view to plain text mode
So, the question is, after the new data is inserted, how do I refresh the query in the model/view so it is updated?
Bookmarks