diff options
author | Mark Brand <mabrand@mabrand.nl> | 2012-02-15 11:23:54 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-06 06:34:33 +0100 |
commit | e999ca25c66dfb9d056a1a83b93043ece5733d93 (patch) | |
tree | b9eaa8e1959f09968b7ef59c9347c24a998c76cf /examples/sql/sqlbrowser/browser.cpp | |
parent | 698e620aad68d702c32b62e8114fe12a7b3e2ca5 (diff) |
add strategy support to sqlbrowser example
Several useful options added to context menu.
Also added yellow background for unsubmitted
changes.
Change-Id: I132cd4498a5fb7275ea10c0497910aba99c06a2b
Reviewed-by: Honglei Zhang <honglei.zhang@nokia.com>
Reviewed-by: Yunqiao Yin <charles.yin@nokia.com>
Diffstat (limited to 'examples/sql/sqlbrowser/browser.cpp')
-rw-r--r-- | examples/sql/sqlbrowser/browser.cpp | 75 |
1 files changed, 67 insertions, 8 deletions
diff --git a/examples/sql/sqlbrowser/browser.cpp b/examples/sql/sqlbrowser/browser.cpp index 3aa330e227..eaaec25b16 100644 --- a/examples/sql/sqlbrowser/browser.cpp +++ b/examples/sql/sqlbrowser/browser.cpp @@ -52,6 +52,12 @@ Browser::Browser(QWidget *parent) table->addAction(insertRowAction); table->addAction(deleteRowAction); + table->addAction(fieldStrategyAction); + table->addAction(rowStrategyAction); + table->addAction(manualStrategyAction); + table->addAction(submitAction); + table->addAction(revertAction); + table->addAction(selectAction); if (QSqlDatabase::drivers().isEmpty()) QMessageBox::information(this, tr("No database drivers found"), @@ -144,7 +150,7 @@ void Browser::addConnection() void Browser::showTable(const QString &t) { - QSqlTableModel *model = new QSqlTableModel(table, connectionWidget->currentDatabase()); + QSqlTableModel *model = new CustomModel(table, connectionWidget->currentDatabase()); model->setEditStrategy(QSqlTableModel::OnRowChange); model->setTable(connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName)); model->select(); @@ -215,8 +221,6 @@ void Browser::deleteRow() if (!model) return; - model->setEditStrategy(QSqlTableModel::OnManualSubmit); - QModelIndexList currentSelection = table->selectionModel()->selectedIndexes(); for (int i = 0; i < currentSelection.count(); ++i) { if (currentSelection.at(i).column() != 0) @@ -224,24 +228,79 @@ void Browser::deleteRow() model->removeRow(currentSelection.at(i).row()); } - model->submitAll(); - model->setEditStrategy(QSqlTableModel::OnRowChange); - updateActions(); } void Browser::updateActions() { - bool enableIns = qobject_cast<QSqlTableModel *>(table->model()); + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + bool enableIns = tm; bool enableDel = enableIns && table->currentIndex().isValid(); insertRowAction->setEnabled(enableIns); deleteRowAction->setEnabled(enableDel); + + fieldStrategyAction->setEnabled(tm); + rowStrategyAction->setEnabled(tm); + manualStrategyAction->setEnabled(tm); + submitAction->setEnabled(tm); + revertAction->setEnabled(tm); + selectAction->setEnabled(tm); + + if (tm) { + QSqlTableModel::EditStrategy es = tm->editStrategy(); + fieldStrategyAction->setChecked(es == QSqlTableModel::OnFieldChange); + rowStrategyAction->setChecked(es == QSqlTableModel::OnRowChange); + manualStrategyAction->setChecked(es == QSqlTableModel::OnManualSubmit); + } } void Browser::about() { QMessageBox::about(this, tr("About"), tr("The SQL Browser demonstration " "shows how a data browser can be used to visualize the results of SQL" - "statements on a live database")); + "statements on a live database")); +} + +void Browser::on_fieldStrategyAction_triggered() +{ + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + if (tm) + tm->setEditStrategy(QSqlTableModel::OnFieldChange); +} + +void Browser::on_rowStrategyAction_triggered() +{ + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + if (tm) + tm->setEditStrategy(QSqlTableModel::OnRowChange); +} + +void Browser::on_manualStrategyAction_triggered() +{ + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + if (tm) + tm->setEditStrategy(QSqlTableModel::OnManualSubmit); +} + +void Browser::on_submitAction_triggered() +{ + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + if (tm) + tm->submitAll(); +} + +void Browser::on_revertAction_triggered() +{ + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + if (tm) + tm->revertAll(); +} + +void Browser::on_selectAction_triggered() +{ + QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + if (tm) + tm->select(); } + |