This is really a c++ problem but I'm confused and I'm sure someone can enlighten me easily....
In the function openLog, I'm trying to set up a TableModel and a View and pass back the model so I can write to the model in a different function.
checkPrefs() works fine and the view displays the data ok.
When writeLog is called, it creates a new model/view instead of inserting into the model created in openLog. I guess the question is how to refer to the model passed back by openLog?
model->setTable("log");
view->setModel(model);
view->setMinimumSize(1200,100);
view->move(35,80);
view->setSortingEnabled(TRUE);
view->setWindowTitle("Log: " + logName);
header->setMovable(TRUE);
view->show();
return model;
}
void MainWindow::checkPrefs() {
ControlDB ctrl;
QString open
= ctrl.
getOpenLastLog();
if (open == "Y") {
if (log != "") {
Connection conn;
bool base = conn.createConnection(logName);
openLog(log);
}
}
}
void MainWindow::writeLog() {
ControlDB ctrl;
QString logTitle
= ctrl.
getLastLog();
QString logName
= logTitle
+ "_log.sqlite";
int newID = ctrl.getMaxID(logName) + 1;
Connection conn;
bool base = conn.createConnection(logName);
record = model->record();
record.setValue("id", newID);
record.setValue("call", ui->mwCall->text());
record.setValue("sent", ui->mwSent->text());
record.setValue("rcvd", ui->mwRcvd->text());
record.setValue("date", d);
record.setValue("timeon", t);
bool insertMe = model->insertRecord(-1, record);
if (insertMe) {
model->select();
clear(); // clear the mainwindow fields after a database save
}else{
qDebug() << model->lastError();
}
}
QSqlTableModel *MainWindow::openLog(QString logName) {
QSqlTableModel *model = new QSqlTableModel;
model->setTable("log");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
QTableView *view = new QTableView;
view->setModel(model);
view->setMinimumSize(1200,100);
view->move(35,80);
view->setSortingEnabled(TRUE);
view->setWindowTitle("Log: " + logName);
QHeaderView *header = view->horizontalHeader();
header->setMovable(TRUE);
view->show();
return model;
}
void MainWindow::checkPrefs() {
ControlDB ctrl;
QString open = ctrl.getOpenLastLog();
QString log = ctrl.getLastLog();
if (open == "Y") {
if (log != "") {
QString logName = log + "_log";
Connection conn;
bool base = conn.createConnection(logName);
openLog(log);
}
}
}
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());
Connection conn;
bool base = conn.createConnection(logName);
QSqlTableModel *model = openLog(logTitle);
QSqlRecord record;
record = model->record();
record.setValue("id", newID);
record.setValue("call", ui->mwCall->text());
record.setValue("sent", ui->mwSent->text());
record.setValue("rcvd", ui->mwRcvd->text());
record.setValue("date", d);
record.setValue("timeon", t);
bool insertMe = model->insertRecord(-1, record);
if (insertMe) {
model->select();
clear(); // clear the mainwindow fields after a database save
}else{
qDebug() << model->lastError();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks