The header fills automatically with the column names from the database - that is ok.
I tried the following declaration:
model = new QSqlTableModel(this, logdb);
view = new QTableView();
header = new QHeaderView(Qt::Horizontal);
To copy to clipboard, switch view to plain text mode
Now, it opens previously created/saved databases and saves the open db/view header without crashing.
It will also create a new database if I do it from a lineEdit/button on the mainwindow ok,
but if I try that same thing from a lineEdit/button connected with signal/slot from a dialog, then it crashes when it tries to save the existing open header.
Something different is happening when done from the dialog.
The method is called the same way though.
// method called from mainwindow
void MainWindow::addLog() {
if (ui->mwCall->text() != "") {
QMessageBox::information(this,
"QtLogger", tr
("Last QSO not saved. Please save or clear before creating a new log."));
}
QString log = ui
->leNewLog
->text
();
ControlDB ctrl;
ctrl.createLogDB(log, ctrlConn);
openLog(log); // this is where the model/header/view are created on the heap after existing header is saved and then model/view/header are deleted
ui->leNewLog->setText("");
}
// method called from mainwindow
void MainWindow::addLog() {
if (ui->mwCall->text() != "") {
QMessageBox::information(this, "QtLogger", tr("Last QSO not saved. Please save or clear before creating a new log."));
}
QString log = ui->leNewLog->text();
ControlDB ctrl;
ctrl.createLogDB(log, ctrlConn);
openLog(log); // this is where the model/header/view are created on the heap after existing header is saved and then model/view/header are deleted
ui->leNewLog->setText("");
}
To copy to clipboard, switch view to plain text mode
// this is method called from the dialog - same functionality as above
void MainWindow
::newLog(QString log) { if (ui->mwCall->text() != "") {
QMessageBox::information(this,
"QtLogger", tr
("Last QSO not saved. Please save or clear before creating a new log."));
}
ControlDB ctrl;
ctrl.createLogDB(log, ctrlConn);
openLog(log);
}
// this is method called from the dialog - same functionality as above
void MainWindow::newLog(QString log) {
if (ui->mwCall->text() != "") {
QMessageBox::information(this, "QtLogger", tr("Last QSO not saved. Please save or clear before creating a new log."));
}
ControlDB ctrl;
ctrl.createLogDB(log, ctrlConn);
openLog(log);
}
To copy to clipboard, switch view to plain text mode
So you can see everything that goes on....
void MainWindow
::openLog(QString logName
) { ControlDB ctrl;
QString openLog
= ctrl.
getOpenLog(ctrlConn
);
if (openLog != "") {
db.setDatabaseName("header.sqlite");
db.open();
ha = header->saveState();
query.prepare("SELECT count(*) from header where headerLog = ?");
query.addBindValue(logName);
query.exec();
query.last();
int cnt = query.value(0).toInt();
if (cnt == 0) {
query.prepare("INSERT into header(positions, headerLog) VALUES (?, ?)");
query.addBindValue(ha);
query.addBindValue(logName);
query.exec();
}else{
query.prepare("UPDATE header set positions=? where headerLog=? ");
query.addBindValue(ha);
query.addBindValue(logName);
query.exec();
query.last();
qDebug() << "saveHeader error 3 = " << query.lastError();
}
db.close();
openDB.close();
delete header; // these only deleted if there was an open db and after header was saved.
delete view;
delete model;
}
query.prepare("SELECT count (*) from header where headerLog = ?");
query.addBindValue(logName);
query.exec();
query.last();
int cnt = query.value(0).toInt();
ctrl.createConnection(logName + ".log", logName);
logdb.setDatabaseName(logName + ".log.sqlite");
logdb.open();
view->setHorizontalHeader(header);
//etc
void MainWindow::openLog(QString logName) {
ControlDB ctrl;
QString openLog = ctrl.getOpenLog(ctrlConn);
if (openLog != "") {
QSqlDatabase db = QSqlDatabase::database(hdrConn);
db.setDatabaseName("header.sqlite");
db.open();
QByteArray ha;
ha = header->saveState();
QSqlQuery query(db);
query.prepare("SELECT count(*) from header where headerLog = ?");
query.addBindValue(logName);
query.exec();
query.last();
int cnt = query.value(0).toInt();
if (cnt == 0) {
query.prepare("INSERT into header(positions, headerLog) VALUES (?, ?)");
query.addBindValue(ha);
query.addBindValue(logName);
query.exec();
}else{
query.prepare("UPDATE header set positions=? where headerLog=? ");
query.addBindValue(ha);
query.addBindValue(logName);
query.exec();
query.last();
qDebug() << "saveHeader error 3 = " << query.lastError();
}
db.close();
QSqlDatabase openDB = model->database();
openDB.close();
delete header; // these only deleted if there was an open db and after header was saved.
delete view;
delete model;
}
QSqlDatabase hdrdb = QSqlDatabase::database(hdrConn);
QSqlQuery query(hdrdb);
query.prepare("SELECT count (*) from header where headerLog = ?");
query.addBindValue(logName);
query.exec();
query.last();
int cnt = query.value(0).toInt();
ctrl.createConnection(logName + ".log", logName);
QSqlDatabase logdb = QSqlDatabase::database(logName);
logdb.setDatabaseName(logName + ".log.sqlite");
logdb.open();
model = new QSqlTableModel(this, logdb);
view = new QTableView();
header = new QHeaderView(Qt::Horizontal);
view->setHorizontalHeader(header);
//etc
To copy to clipboard, switch view to plain text mode
Is Qt starting a new thread for the dialog - could that be the problem?
The dialog passes the new db name to a function in the mainwindow class just like the lineEdit/button on the mainwindow does.
Bookmarks