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?

Qt Code:
  1. QSqlTableModel *MainWindow::openLog(QString logName) {
  2. model->setTable("log");
  3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  4.  
  5. QTableView *view = new QTableView;
  6. view->setModel(model);
  7. view->setMinimumSize(1200,100);
  8. view->move(35,80);
  9. view->setSortingEnabled(TRUE);
  10. view->setWindowTitle("Log: " + logName);
  11. QHeaderView *header = view->horizontalHeader();
  12. header->setMovable(TRUE);
  13. view->show();
  14. return model;
  15. }
  16.  
  17. void MainWindow::checkPrefs() {
  18. ControlDB ctrl;
  19. QString open = ctrl.getOpenLastLog();
  20. QString log = ctrl.getLastLog();
  21.  
  22. if (open == "Y") {
  23. if (log != "") {
  24. QString logName = log + "_log";
  25. Connection conn;
  26. bool base = conn.createConnection(logName);
  27. openLog(log);
  28. }
  29. }
  30. }
  31.  
  32. void MainWindow::writeLog() {
  33. ControlDB ctrl;
  34. QString logTitle = ctrl.getLastLog();
  35. QString logName = logTitle + "_log.sqlite";
  36. int newID = ctrl.getMaxID(logName) + 1;
  37. QTime t(QTime::currentTime());
  38. QDate d(QDate::currentDate());
  39. Connection conn;
  40.  
  41. bool base = conn.createConnection(logName);
  42. QSqlTableModel *model = openLog(logTitle);
  43.  
  44. QSqlRecord record;
  45. record = model->record();
  46. record.setValue("id", newID);
  47. record.setValue("call", ui->mwCall->text());
  48. record.setValue("sent", ui->mwSent->text());
  49. record.setValue("rcvd", ui->mwRcvd->text());
  50. record.setValue("date", d);
  51. record.setValue("timeon", t);
  52. bool insertMe = model->insertRecord(-1, record);
  53.  
  54. if (insertMe) {
  55. model->select();
  56. clear(); // clear the mainwindow fields after a database save
  57. }else{
  58. qDebug() << model->lastError();
  59. }
  60. }
To copy to clipboard, switch view to plain text mode