From e999ca25c66dfb9d056a1a83b93043ece5733d93 Mon Sep 17 00:00:00 2001 From: Mark Brand Date: Wed, 15 Feb 2012 11:23:54 +0100 Subject: 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 Reviewed-by: Yunqiao Yin --- examples/sql/sqlbrowser/browser.cpp | 75 +++++++++++-- examples/sql/sqlbrowser/browser.h | 20 ++++ examples/sql/sqlbrowser/browserwidget.ui | 184 +++++++++++++++++++------------ 3 files changed, 200 insertions(+), 79 deletions(-) (limited to 'examples/sql') 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(table->model()); + QSqlTableModel * tm = qobject_cast(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(table->model()); + if (tm) + tm->setEditStrategy(QSqlTableModel::OnFieldChange); +} + +void Browser::on_rowStrategyAction_triggered() +{ + QSqlTableModel * tm = qobject_cast(table->model()); + if (tm) + tm->setEditStrategy(QSqlTableModel::OnRowChange); +} + +void Browser::on_manualStrategyAction_triggered() +{ + QSqlTableModel * tm = qobject_cast(table->model()); + if (tm) + tm->setEditStrategy(QSqlTableModel::OnManualSubmit); +} + +void Browser::on_submitAction_triggered() +{ + QSqlTableModel * tm = qobject_cast(table->model()); + if (tm) + tm->submitAll(); +} + +void Browser::on_revertAction_triggered() +{ + QSqlTableModel * tm = qobject_cast(table->model()); + if (tm) + tm->revertAll(); +} + +void Browser::on_selectAction_triggered() +{ + QSqlTableModel * tm = qobject_cast(table->model()); + if (tm) + tm->select(); } + diff --git a/examples/sql/sqlbrowser/browser.h b/examples/sql/sqlbrowser/browser.h index 0769812ec8..e9f45345df 100644 --- a/examples/sql/sqlbrowser/browser.h +++ b/examples/sql/sqlbrowser/browser.h @@ -43,6 +43,7 @@ #define BROWSER_H #include +#include #include "ui_browserwidget.h" class ConnectionWidget; @@ -77,6 +78,12 @@ public slots: { insertRow(); } void on_deleteRowAction_triggered() { deleteRow(); } + void on_fieldStrategyAction_triggered(); + void on_rowStrategyAction_triggered(); + void on_manualStrategyAction_triggered(); + void on_submitAction_triggered(); + void on_revertAction_triggered(); + void on_selectAction_triggered(); void on_connectionWidget_tableActivated(const QString &table) { showTable(table); } void on_connectionWidget_metaDataRequested(const QString &table) @@ -96,4 +103,17 @@ signals: void statusMessage(const QString &message); }; +class CustomModel: public QSqlTableModel +{ + Q_OBJECT +public: + CustomModel(QObject *parent = 0, QSqlDatabase db = QSqlDatabase()):QSqlTableModel(parent, db) {} + QVariant data(const QModelIndex &idx, int role) const + { + if (role == Qt::BackgroundRole && isDirty(idx)) + return QBrush(QColor(Qt::yellow)); + return QSqlTableModel::data(idx, role); + } +}; + #endif diff --git a/examples/sql/sqlbrowser/browserwidget.ui b/examples/sql/sqlbrowser/browserwidget.ui index 20946f0ede..ff65b1f199 100644 --- a/examples/sql/sqlbrowser/browserwidget.ui +++ b/examples/sql/sqlbrowser/browserwidget.ui @@ -1,10 +1,8 @@ - - - - + + Browser - - + + 0 0 @@ -12,100 +10,90 @@ 515 - + Qt SQL Browser - - - 8 - - + + 6 + + 8 + - - - - 7 - 7 + + + 0 0 - + Qt::Horizontal - - - - 13 - 7 + + + 1 0 - - - - 7 - 7 + + + 2 0 - + Qt::ActionsContextMenu - + QAbstractItemView::SelectRows - - - - 5 - 3 + + + 0 0 - + 16777215 180 - + SQL Query - - - 9 - - + + 6 + + 9 + - - - - 7 - 3 + + + 0 0 - + 0 18 - + 0 120 @@ -114,19 +102,19 @@ - - - 1 - - + + 6 + + 1 + - + Qt::Horizontal - + 40 20 @@ -135,15 +123,15 @@ - - + + &Clear - - + + &Submit @@ -154,37 +142,91 @@ - - + + false - + &Insert Row - + Inserts a new Row - - + + false - + &Delete Row - + Deletes the current Row + + + true + + + Submit on &Field Change + + + Commit on Field Change + + + + + true + + + Submit on &Row Change + + + Commit on Row Change + + + + + true + + + Submit &Manually + + + Commit Manually + + + + + &Submit All + + + Submit Changes + + + + + &Revert All + + + Revert + + + + + S&elect + + + Refresh Data from Database + + - ConnectionWidget QTreeView
connectionwidget.h
- 0 -
-- cgit v1.2.3