PDA

View Full Version : QSqlTableModel problem



waynew
13th December 2009, 23:09
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?



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();
}
}

waynew
13th December 2009, 23:51
OOOPS! I can see that openLog gets called twice, so of course there are 2 models/views. Maybe I need some help restructuring this.

bood
14th December 2009, 03:21
Keep the view pointer as a member of your MainWindow.
and you can get the model of the View by


view->model();

waynew
14th December 2009, 22:36
When I put the following in the header:



QSqlTableView *view;


and the following in the class:



QSqlTableView *view = new QTableView;



I get a compile error: forbids declaration of `QSqlTableView' with no type
on the header line.

Lykurg
14th December 2009, 22:40
There is no class called QSqlTableView in Qt...:eek: It has to be QTableView.

waynew
14th December 2009, 22:59
Thanks Lykurg. I have been staring at this code too long!

But, now when I try to reference the view in a local function, like

view->show()

The application crashes.

Lykurg
15th December 2009, 09:08
In the code you pasted you don't use any layout. I would recomend to use any kind of QLayout/QHBoxLayout... Then you don't have to call show().

And if you don't know why your app crashes, debug your application.

giacomelli.fabio
31st December 2009, 16:20
Hi

I'm using the same code

But I don't understand how to edit ( alter some values ) in an already inserted record ....

... I need to in a first step populate some values and in a second step add some others ... but I can't get the correct row and I don't know ( after found it ) if I risk to delete already written values

waynew
1st January 2010, 21:57
I'm not using the model anywhere in my code for updates.
Only inserts and selects.

But if I needed to do updates, the only way I would know how would be to
make your original table with an id primary key column (unique number value). Then use QsqlQuery to update the record where id = <id of row you want updated> The only column values that will be updated are the ones specified in your update statement - the other column values will remain unchanged.

Good Luck!

giacomelli.fabio
2nd January 2010, 18:14
QSqlTableView is fantastic .... I need only to give to it table and then I could also edit it on the fly !!! Without a lot of code !!! The only problem I found was the one I wrote before .

I supposed that a "hidden" (for me) function provides a way to find the correct row.

I will found a solution ;)

Thank you Bye

Tanuki-no Torigava
8th January 2010, 00:48
You can subclass directly from the QAbstractView or overwrite that function