void MainWindow::initMainWindow()
{
database.setDatabaseName(dbName);
if(!database.open())
{
QMessageBox::critical(NULL, qApp
->tr
("Cannot open database"),
qApp->tr("Unable to establish a connection with database '%1'.")
.arg(dbName),
return;
}
if(!query.exec("create table tracks (id integer primary key autoincrement, artist varchar(50), "
"title varchar(80), rating integer, status integer)"))
{
printf("Error 1\n");
}
else
{
printf("OK 1\n");
}
if(!query.exec("insert into tracks (artist, title, rating, status) values ('The Rolling Stones', 'Satisfaction', 33, 1)"))
{
printf("Error 2\n");
}
else
{
printf("OK 2\n");
}
if(!query.exec("insert into tracks (artist, title, rating, status) values ('The Doors', 'Riders on the Storm', 75, 1)"))
{
printf("Error 3\n");
}
else
{
printf("OK 3\n");
}
query.next();
model.setTable("tracks");
model.select();
setTable(&model);
}
{
trackTable->setModel(tblModel);
trackTable->show();
}
void MainWindow::initMainWindow()
{
settings = new QSettings(APPNAME, DEVNAME);
QString dbName = ":memory:";
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName(dbName);
if(!database.open())
{
QMessageBox::critical(NULL, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a connection with database '%1'.")
.arg(dbName),
QMessageBox::Cancel);
return;
}
QSqlQuery query;
if(!query.exec("create table tracks (id integer primary key autoincrement, artist varchar(50), "
"title varchar(80), rating integer, status integer)"))
{
printf("Error 1\n");
}
else
{
printf("OK 1\n");
}
if(!query.exec("insert into tracks (artist, title, rating, status) values ('The Rolling Stones', 'Satisfaction', 33, 1)"))
{
printf("Error 2\n");
}
else
{
printf("OK 2\n");
}
if(!query.exec("insert into tracks (artist, title, rating, status) values ('The Doors', 'Riders on the Storm', 75, 1)"))
{
printf("Error 3\n");
}
else
{
printf("OK 3\n");
}
query.next();
QSqlRelationalTableModel model;
model.setTable("tracks");
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
model.select();
setTable(&model);
}
void MainWindow::setTable(QSqlTableModel *tblModel)
{
trackTable->setModel(tblModel);
trackTable->setItemDelegate(new QSqlRelationalDelegate(trackTable));
trackTable->show();
}
To copy to clipboard, switch view to plain text mode
-------------------------------------
| 1 | 2 | 3 | 4 | 5 |
-------------------------------------
1 | | | | | |
-------------------------------------
2 | | | | | |
-------------------------------------
-------------------------------------
| 1 | 2 | 3 | 4 | 5 |
-------------------------------------
1 | | | | | |
-------------------------------------
2 | | | | | |
-------------------------------------
To copy to clipboard, switch view to plain text mode
Bookmarks