diff options
Diffstat (limited to 'examples/sql/sqlbrowser')
-rw-r--r-- | examples/sql/sqlbrowser/CMakeLists.txt | 47 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/browser.cpp | 265 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/browser.h | 117 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/connectionwidget.cpp | 114 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/connectionwidget.h | 60 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/main.cpp | 91 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/qsqlconnectiondialog.cpp | 96 | ||||
-rw-r--r-- | examples/sql/sqlbrowser/qsqlconnectiondialog.h | 68 |
8 files changed, 294 insertions, 564 deletions
diff --git a/examples/sql/sqlbrowser/CMakeLists.txt b/examples/sql/sqlbrowser/CMakeLists.txt index 833b57f1a1..a124ae203c 100644 --- a/examples/sql/sqlbrowser/CMakeLists.txt +++ b/examples/sql/sqlbrowser/CMakeLists.txt @@ -1,37 +1,44 @@ -# Generated from sqlbrowser.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.16) project(sqlbrowser LANGUAGES CXX) set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(CMAKE_AUTOMOC ON) -set(CMAKE_AUTORCC ON) -set(CMAKE_AUTOUIC ON) +find_package(Qt6 REQUIRED COMPONENTS Core Gui Sql Widgets) -set(INSTALL_EXAMPLEDIR "examples/sql/sqlbrowser") +qt_standard_project_setup() -find_package(Qt6 COMPONENTS Core) -find_package(Qt6 COMPONENTS Gui) -find_package(Qt6 COMPONENTS Sql) -find_package(Qt6 COMPONENTS Widgets) - -add_qt_gui_executable(sqlbrowser +qt_add_executable(sqlbrowser browser.cpp browser.h browserwidget.ui connectionwidget.cpp connectionwidget.h main.cpp qsqlconnectiondialog.cpp qsqlconnectiondialog.h qsqlconnectiondialog.ui ) -target_link_libraries(sqlbrowser PUBLIC - Qt::Core - Qt::Gui - Qt::Sql - Qt::Widgets + +set_target_properties(sqlbrowser PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_link_libraries(sqlbrowser PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Sql + Qt6::Widgets ) install(TARGETS sqlbrowser - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION . + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} +) + +qt_generate_deploy_app_script( + TARGET sqlbrowser + OUTPUT_SCRIPT deploy_script + NO_UNSUPPORTED_PLATFORM_ERROR ) +install(SCRIPT ${deploy_script}) diff --git a/examples/sql/sqlbrowser/browser.cpp b/examples/sql/sqlbrowser/browser.cpp index b05cf52099..9a99999702 100644 --- a/examples/sql/sqlbrowser/browser.cpp +++ b/examples/sql/sqlbrowser/browser.cpp @@ -1,72 +1,51 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "browser.h" #include "qsqlconnectiondialog.h" - -#include <QtWidgets> -#include <QtSql> +#include <ui_browserwidget.h> + +#include <QAction> +#include <QMessageBox> +#include <QStandardItemModel> +#include <QSqlDriver> +#include <QSqlError> +#include <QSqlField> +#include <QSqlQuery> +#include <QSqlRecord> +#include <QTextEdit> +#include <QTimer> Browser::Browser(QWidget *parent) : QWidget(parent) + , m_ui(new Ui::Browser) { - setupUi(this); - - table->addAction(insertRowAction); - table->addAction(deleteRowAction); - table->addAction(fieldStrategyAction); - table->addAction(rowStrategyAction); - table->addAction(manualStrategyAction); - table->addAction(submitAction); - table->addAction(revertAction); - table->addAction(selectAction); + m_ui->setupUi(this); + + m_ui->table->addAction(m_ui->insertRowAction); + m_ui->table->addAction(m_ui->deleteRowAction); + m_ui->table->addAction(m_ui->fieldStrategyAction); + m_ui->table->addAction(m_ui->rowStrategyAction); + m_ui->table->addAction(m_ui->manualStrategyAction); + m_ui->table->addAction(m_ui->submitAction); + m_ui->table->addAction(m_ui->revertAction); + m_ui->table->addAction(m_ui->selectAction); + + connect(m_ui->insertRowAction, &QAction::triggered, this, &Browser::insertRow); + connect(m_ui->deleteRowAction, &QAction::triggered, this, &Browser::deleteRow); + connect(m_ui->fieldStrategyAction, &QAction::triggered, this, &Browser::onFieldStrategyAction); + connect(m_ui->rowStrategyAction, &QAction::triggered, this, &Browser::onRowStrategyAction); + connect(m_ui->sqlEdit, &QTextEdit::textChanged, this, &Browser::updateActions); + + connect(m_ui->connectionWidget, &ConnectionWidget::tableActivated, + this, &Browser::showTable); + connect(m_ui->connectionWidget, &ConnectionWidget::metaDataRequested, + this, &Browser::showMetaData); + + connect(m_ui->submitButton, &QPushButton::clicked, + this, &Browser::onSubmitButton); + connect(m_ui->clearButton, &QPushButton::clicked, + this, &Browser::onClearButton); if (QSqlDatabase::drivers().isEmpty()) QMessageBox::information(this, tr("No database drivers found"), @@ -74,18 +53,22 @@ Browser::Browser(QWidget *parent) "Please check the documentation how to build the " "Qt SQL plugins.")); - emit statusMessage(tr("Ready.")); + QTimer::singleShot(0, this, [this]() { + updateActions(); + emit statusMessage(tr("Ready.")); + }); } Browser::~Browser() { + delete m_ui; } void Browser::exec() { - QSqlQueryModel *model = new QSqlQueryModel(table); - model->setQuery(QSqlQuery(sqlEdit->toPlainText(), connectionWidget->currentDatabase())); - table->setModel(model); + QSqlQueryModel *model = new QSqlQueryModel(m_ui->table); + model->setQuery(QSqlQuery(m_ui->sqlEdit->toPlainText(), m_ui->connectionWidget->currentDatabase())); + m_ui->table->setModel(model); if (model->lastError().type() != QSqlError::NoError) emit statusMessage(model->lastError().text()); @@ -99,7 +82,7 @@ void Browser::exec() } QSqlError Browser::addConnection(const QString &driver, const QString &dbName, const QString &host, - const QString &user, const QString &passwd, int port) + const QString &user, const QString &passwd, int port) { static int cCount = 0; @@ -113,12 +96,12 @@ QSqlError Browser::addConnection(const QString &driver, const QString &dbName, c db = QSqlDatabase(); QSqlDatabase::removeDatabase(QString("Browser%1").arg(cCount)); } - connectionWidget->refresh(); + m_ui->connectionWidget->refresh(); return err; } -void Browser::addConnection() +void Browser::openNewConnectionDialog() { QSqlConnectionDialog dialog(this); if (dialog.exec() != QDialog::Accepted) @@ -129,10 +112,14 @@ void Browser::addConnection() QSqlDatabase::removeDatabase("in_mem_db"); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "in_mem_db"); db.setDatabaseName(":memory:"); - if (!db.open()) - QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while " - "opening the connection: ") + db.lastError().text()); - QSqlQuery q("", db); + if (!db.open()) { + QMessageBox::warning(this, tr("Unable to open database"), + tr("An error occurred while " + "opening the connection: %1") .arg(db.lastError().text())); + return; + } + db.transaction(); + QSqlQuery q(db); q.exec("drop table Movies"); q.exec("drop table Names"); q.exec("create table Movies (id integer primary key, Title varchar, Director varchar, Rating number)"); @@ -141,43 +128,49 @@ void Browser::addConnection() q.exec("insert into Movies values (2, 'Bis ans Ende der Welt', 'Wim Wenders', '6.5')"); q.exec("insert into Movies values (3, 'Hardware', 'Richard Stanley', '5.2')"); q.exec("insert into Movies values (4, 'Mitchell', 'Andrew V. McLaglen', '2.1')"); - q.exec("create table Names (id integer primary key, Firstname varchar, Lastname varchar, City varchar)"); + q.exec("create table Names (id integer primary key, FirstName varchar, LastName varchar, City varchar)"); q.exec("insert into Names values (0, 'Sala', 'Palmer', 'Morristown')"); q.exec("insert into Names values (1, 'Christopher', 'Walker', 'Morristown')"); q.exec("insert into Names values (2, 'Donald', 'Duck', 'Andeby')"); q.exec("insert into Names values (3, 'Buck', 'Rogers', 'Paris')"); q.exec("insert into Names values (4, 'Sherlock', 'Holmes', 'London')"); - connectionWidget->refresh(); + db.commit(); + m_ui->connectionWidget->refresh(); } else { QSqlError err = addConnection(dialog.driverName(), dialog.databaseName(), dialog.hostName(), - dialog.userName(), dialog.password(), dialog.port()); + dialog.userName(), dialog.password(), dialog.port()); if (err.type() != QSqlError::NoError) - QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while " - "opening the connection: ") + err.text()); + QMessageBox::warning(this, tr("Unable to open database"), + tr("An error occurred while " + "opening the connection: %1").arg(err.text())); } } void Browser::showTable(const QString &t) { - QSqlTableModel *model = new CustomModel(table, connectionWidget->currentDatabase()); + QSqlTableModel *model = new CustomModel(m_ui->table, m_ui->connectionWidget->currentDatabase()); model->setEditStrategy(QSqlTableModel::OnRowChange); - model->setTable(connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName)); + model->setTable(m_ui->connectionWidget->currentDatabase().driver()->escapeIdentifier(t, QSqlDriver::TableName)); model->select(); if (model->lastError().type() != QSqlError::NoError) emit statusMessage(model->lastError().text()); - table->setModel(model); - table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed); - connect(table->selectionModel(), &QItemSelectionModel::currentRowChanged, - this, &Browser::currentChanged); + m_ui->table->setModel(model); + m_ui->table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed); + connect(m_ui->table->selectionModel(), &QItemSelectionModel::currentRowChanged, + this, &Browser::updateActions); + + connect(m_ui->submitAction, &QAction::triggered, model, &QSqlTableModel::submitAll); + connect(m_ui->revertAction, &QAction::triggered, model, &QSqlTableModel::revertAll); + connect(m_ui->selectAction, &QAction::triggered, model, &QSqlTableModel::select); updateActions(); } void Browser::showMetaData(const QString &t) { - QSqlRecord rec = connectionWidget->currentDatabase().record(t); - QStandardItemModel *model = new QStandardItemModel(table); + QSqlRecord rec = m_ui->connectionWidget->currentDatabase().record(t); + QStandardItemModel *model = new QStandardItemModel(m_ui->table); model->insertRows(0, rec.count()); model->insertColumns(0, 7); @@ -193,9 +186,7 @@ void Browser::showMetaData(const QString &t) for (int i = 0; i < rec.count(); ++i) { QSqlField fld = rec.field(i); model->setData(model->index(i, 0), fld.name()); - model->setData(model->index(i, 1), fld.typeID() == -1 - ? QString(QMetaType::typeName(fld.type())) - : QString("%1 (%2)").arg(QMetaType::typeName(fld.type())).arg(fld.typeID())); + model->setData(model->index(i, 1), QString::fromUtf8(fld.metaType().name())); model->setData(model->index(i, 2), fld.length()); model->setData(model->index(i, 3), fld.precision()); model->setData(model->index(i, 4), fld.requiredStatus() == -1 ? QVariant("?") @@ -204,37 +195,37 @@ void Browser::showMetaData(const QString &t) model->setData(model->index(i, 6), fld.defaultValue()); } - table->setModel(model); - table->setEditTriggers(QAbstractItemView::NoEditTriggers); + m_ui->table->setModel(model); + m_ui->table->setEditTriggers(QAbstractItemView::NoEditTriggers); updateActions(); } void Browser::insertRow() { - QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model()); + QSqlTableModel *model = qobject_cast<QSqlTableModel *>(m_ui->table->model()); if (!model) return; - QModelIndex insertIndex = table->currentIndex(); + QModelIndex insertIndex = m_ui->table->currentIndex(); int row = insertIndex.row() == -1 ? 0 : insertIndex.row(); model->insertRow(row); insertIndex = model->index(row, 0); - table->setCurrentIndex(insertIndex); - table->edit(insertIndex); + m_ui->table->setCurrentIndex(insertIndex); + m_ui->table->edit(insertIndex); } void Browser::deleteRow() { - QSqlTableModel *model = qobject_cast<QSqlTableModel *>(table->model()); + QSqlTableModel *model = qobject_cast<QSqlTableModel *>(m_ui->table->model()); if (!model) return; - QModelIndexList currentSelection = table->selectionModel()->selectedIndexes(); - for (int i = 0; i < currentSelection.count(); ++i) { - if (currentSelection.at(i).column() != 0) + const QModelIndexList currentSelection = m_ui->table->selectionModel()->selectedIndexes(); + for (const auto &idx : currentSelection) { + if (idx.column() != 0) continue; - model->removeRow(currentSelection.at(i).row()); + model->removeRow(idx.row()); } updateActions(); @@ -242,74 +233,70 @@ void Browser::deleteRow() void Browser::updateActions() { - QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); - bool enableIns = tm; - bool enableDel = enableIns && table->currentIndex().isValid(); + QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model()); + bool enableIns = tm != nullptr; + bool enableDel = enableIns && m_ui->table->currentIndex().isValid(); - insertRowAction->setEnabled(enableIns); - deleteRowAction->setEnabled(enableDel); + m_ui->insertRowAction->setEnabled(enableIns); + m_ui->deleteRowAction->setEnabled(enableDel); - fieldStrategyAction->setEnabled(tm); - rowStrategyAction->setEnabled(tm); - manualStrategyAction->setEnabled(tm); - submitAction->setEnabled(tm); - revertAction->setEnabled(tm); - selectAction->setEnabled(tm); + m_ui->submitAction->setEnabled(tm); + m_ui->revertAction->setEnabled(tm); + m_ui->selectAction->setEnabled(tm); + + const bool isEmpty = m_ui->sqlEdit->toPlainText().isEmpty(); + m_ui->submitButton->setEnabled(m_ui->connectionWidget->currentDatabase().isOpen() && !isEmpty); + m_ui->clearButton->setEnabled(!isEmpty); if (tm) { QSqlTableModel::EditStrategy es = tm->editStrategy(); - fieldStrategyAction->setChecked(es == QSqlTableModel::OnFieldChange); - rowStrategyAction->setChecked(es == QSqlTableModel::OnRowChange); - manualStrategyAction->setChecked(es == QSqlTableModel::OnManualSubmit); + m_ui->fieldStrategyAction->setChecked(es == QSqlTableModel::OnFieldChange); + m_ui->rowStrategyAction->setChecked(es == QSqlTableModel::OnRowChange); + m_ui->manualStrategyAction->setChecked(es == QSqlTableModel::OnManualSubmit); + } else { + m_ui->fieldStrategyAction->setEnabled(false); + m_ui->rowStrategyAction->setEnabled(false); + m_ui->manualStrategyAction->setEnabled(false); } } 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")); + 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")); } -void Browser::on_fieldStrategyAction_triggered() +void Browser::onFieldStrategyAction() { - QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model()); if (tm) tm->setEditStrategy(QSqlTableModel::OnFieldChange); } -void Browser::on_rowStrategyAction_triggered() +void Browser::onRowStrategyAction() { - QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->table->model()); if (tm) tm->setEditStrategy(QSqlTableModel::OnRowChange); } -void Browser::on_manualStrategyAction_triggered() +void Browser::onManualStrategyAction() { - QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); + QSqlTableModel *tm = qobject_cast<QSqlTableModel *>(m_ui->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() +void Browser::onSubmitButton() { - QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); - if (tm) - tm->revertAll(); + exec(); + m_ui->sqlEdit->setFocus(); } -void Browser::on_selectAction_triggered() +void Browser::onClearButton() { - QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model()); - if (tm) - tm->select(); + m_ui->sqlEdit->clear(); + m_ui->sqlEdit->setFocus(); } - diff --git a/examples/sql/sqlbrowser/browser.h b/examples/sql/sqlbrowser/browser.h index 3f98f487c3..c61048c84a 100644 --- a/examples/sql/sqlbrowser/browser.h +++ b/examples/sql/sqlbrowser/browser.h @@ -1,124 +1,63 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef BROWSER_H #define BROWSER_H #include <QWidget> #include <QSqlTableModel> -#include "ui_browserwidget.h" -class ConnectionWidget; -QT_FORWARD_DECLARE_CLASS(QTableView) -QT_FORWARD_DECLARE_CLASS(QPushButton) -QT_FORWARD_DECLARE_CLASS(QTextEdit) QT_FORWARD_DECLARE_CLASS(QSqlError) -class Browser: public QWidget, private Ui::Browser +QT_BEGIN_NAMESPACE +namespace Ui +{ +class Browser; +} +QT_END_NAMESPACE + +class Browser : public QWidget { Q_OBJECT public: Browser(QWidget *parent = nullptr); - virtual ~Browser(); + ~Browser(); QSqlError addConnection(const QString &driver, const QString &dbName, const QString &host, - const QString &user, const QString &passwd, int port = -1); + const QString &user, const QString &passwd, int port); +public slots: + void openNewConnectionDialog(); + void about(); + +protected: void insertRow(); void deleteRow(); void updateActions(); -public slots: +protected slots: void exec(); void showTable(const QString &table); void showMetaData(const QString &table); - void addConnection(); - void currentChanged() { updateActions(); } - void about(); - void on_insertRowAction_triggered() - { 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) - { showMetaData(table); } - void on_submitButton_clicked() - { - exec(); - sqlEdit->setFocus(); - } - void on_clearButton_clicked() - { - sqlEdit->clear(); - sqlEdit->setFocus(); - } + void onFieldStrategyAction(); + void onRowStrategyAction(); + void onManualStrategyAction(); + void onSubmitButton(); + void onClearButton(); signals: void statusMessage(const QString &message); + +private: + Ui::Browser *m_ui; }; -class CustomModel: public QSqlTableModel +class CustomModel : public QSqlTableModel { Q_OBJECT public: - explicit CustomModel(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()) - : QSqlTableModel(parent, db) {} - + using QSqlTableModel::QSqlTableModel; QVariant data(const QModelIndex &idx, int role) const override { if (role == Qt::BackgroundRole && isDirty(idx)) diff --git a/examples/sql/sqlbrowser/connectionwidget.cpp b/examples/sql/sqlbrowser/connectionwidget.cpp index 658cfb77dc..da6796d7e5 100644 --- a/examples/sql/sqlbrowser/connectionwidget.cpp +++ b/examples/sql/sqlbrowser/connectionwidget.cpp @@ -1,66 +1,21 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "connectionwidget.h" -#include <QtWidgets> -#include <QtSql> +#include <QAction> +#include <QHeaderView> +#include <QSqlDatabase> +#include <QTreeWidget> +#include <QVBoxLayout> ConnectionWidget::ConnectionWidget(QWidget *parent) : QWidget(parent) + , tree(new QTreeWidget(this)) { QVBoxLayout *layout = new QVBoxLayout(this); - tree = new QTreeWidget(this); - tree->setObjectName(QLatin1String("tree")); - tree->setHeaderLabels(QStringList(tr("database"))); - tree->header()->setSectionResizeMode(QHeaderView::Stretch); + tree->setHeaderLabels(QStringList(tr("Database"))); + tree->header()->setStretchLastSection(true); QAction *refreshAction = new QAction(tr("Refresh"), tree); metaDataAction = new QAction(tr("Show Schema"), tree); connect(refreshAction, &QAction::triggered, this, &ConnectionWidget::refresh); @@ -71,34 +26,37 @@ ConnectionWidget::ConnectionWidget(QWidget *parent) layout->addWidget(tree); - QMetaObject::connectSlotsByName(this); + connect(tree, &QTreeWidget::itemActivated, + this, &ConnectionWidget::onItemActivated); + connect(tree, &QTreeWidget::currentItemChanged, + this, &ConnectionWidget::onCurrentItemChanged); } ConnectionWidget::~ConnectionWidget() { } -static QString qDBCaption(const QSqlDatabase &db) -{ - QString nm = db.driverName(); - nm.append(QLatin1Char(':')); - if (!db.userName().isEmpty()) - nm.append(db.userName()).append(QLatin1Char('@')); - nm.append(db.databaseName()); - return nm; -} void ConnectionWidget::refresh() { + const auto qDBCaption = [](const QSqlDatabase &db) + { + QString nm = db.driverName() + QLatin1Char(':'); + if (!db.userName().isEmpty()) + nm += db.userName() + QLatin1Char('@'); + nm += db.databaseName(); + return nm; + }; + tree->clear(); - QStringList connectionNames = QSqlDatabase::connectionNames(); + const QStringList connectionNames = QSqlDatabase::connectionNames(); bool gotActiveDb = false; - for (int i = 0; i < connectionNames.count(); ++i) { + for (const auto &connectionName : connectionNames) { QTreeWidgetItem *root = new QTreeWidgetItem(tree); - QSqlDatabase db = QSqlDatabase::database(connectionNames.at(i), false); + QSqlDatabase db = QSqlDatabase::database(connectionName, false); root->setText(0, qDBCaption(db)); - if (connectionNames.at(i) == activeDb) { + if (connectionName == activeDb) { gotActiveDb = true; setActive(root); } @@ -123,15 +81,15 @@ QSqlDatabase ConnectionWidget::currentDatabase() const return QSqlDatabase::database(activeDb); } -static void qSetBold(QTreeWidgetItem *item, bool bold) -{ - QFont font = item->font(0); - font.setBold(bold); - item->setFont(0, font); -} - void ConnectionWidget::setActive(QTreeWidgetItem *item) { + const auto qSetBold = [](QTreeWidgetItem *item, bool bold) + { + QFont font = item->font(0); + font.setBold(bold); + item->setFont(0, font); + }; + for (int i = 0; i < tree->topLevelItemCount(); ++i) { if (tree->topLevelItem(i)->font(0).bold()) qSetBold(tree->topLevelItem(i), false); @@ -144,7 +102,7 @@ void ConnectionWidget::setActive(QTreeWidgetItem *item) activeDb = QSqlDatabase::connectionNames().value(tree->indexOfTopLevelItem(item)); } -void ConnectionWidget::on_tree_itemActivated(QTreeWidgetItem *item, int /* column */) +void ConnectionWidget::onItemActivated(QTreeWidgetItem *item) { if (!item) return; @@ -166,7 +124,7 @@ void ConnectionWidget::showMetaData() emit metaDataRequested(cItem->text(0)); } -void ConnectionWidget::on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *) +void ConnectionWidget::onCurrentItemChanged(QTreeWidgetItem *current) { metaDataAction->setEnabled(current && current->parent()); } diff --git a/examples/sql/sqlbrowser/connectionwidget.h b/examples/sql/sqlbrowser/connectionwidget.h index a805765f69..a00296f3c5 100644 --- a/examples/sql/sqlbrowser/connectionwidget.h +++ b/examples/sql/sqlbrowser/connectionwidget.h @@ -1,52 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef CONNECTIONWIDGET_H #define CONNECTIONWIDGET_H @@ -56,14 +9,13 @@ QT_FORWARD_DECLARE_CLASS(QTreeWidget) QT_FORWARD_DECLARE_CLASS(QTreeWidgetItem) QT_FORWARD_DECLARE_CLASS(QSqlDatabase) -QT_FORWARD_DECLARE_CLASS(QMenu) -class ConnectionWidget: public QWidget +class ConnectionWidget : public QWidget { Q_OBJECT public: ConnectionWidget(QWidget *parent = nullptr); - virtual ~ConnectionWidget(); + ~ConnectionWidget(); QSqlDatabase currentDatabase() const; @@ -74,8 +26,8 @@ signals: public slots: void refresh(); void showMetaData(); - void on_tree_itemActivated(QTreeWidgetItem *item, int column); - void on_tree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); + void onItemActivated(QTreeWidgetItem *item); + void onCurrentItemChanged(QTreeWidgetItem *current); private: void setActive(QTreeWidgetItem *); diff --git a/examples/sql/sqlbrowser/main.cpp b/examples/sql/sqlbrowser/main.cpp index ea4649b68d..b3fa8af142 100644 --- a/examples/sql/sqlbrowser/main.cpp +++ b/examples/sql/sqlbrowser/main.cpp @@ -1,65 +1,23 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "browser.h" -#include <QtCore> -#include <QtWidgets> -#include <QtSql> +#include <QApplication> +#include <QMainWindow> +#include <QMenu> +#include <QMenuBar> +#include <QSqlError> +#include <QStatusBar> +#include <QUrl> void addConnectionsFromCommandline(const QStringList &args, Browser *browser) { - for (int i = 1; i < args.count(); ++i) { - QUrl url(args.at(i), QUrl::TolerantMode); + for (qsizetype i = 1; i < args.count(); ++i) { + const auto &arg = args.at(i); + const QUrl url(arg, QUrl::TolerantMode); if (!url.isValid()) { - qWarning("Invalid URL: %s", qPrintable(args.at(i))); + qWarning("Invalid URL: %s", qPrintable(arg)); continue; } QSqlError err = browser->addConnection(url.scheme(), url.path().mid(1), url.host(), @@ -74,29 +32,32 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); QMainWindow mainWin; - mainWin.setWindowTitle(QObject::tr("Qt SQL Browser")); + mainWin.setWindowTitle(QApplication::translate("MainWindow", "Qt SQL Browser")); Browser browser(&mainWin); mainWin.setCentralWidget(&browser); QMenu *fileMenu = mainWin.menuBar()->addMenu(QObject::tr("&File")); - fileMenu->addAction(QObject::tr("Add &Connection..."), - [&]() { browser.addConnection(); }); + fileMenu->addAction(QApplication::translate("MainWindow", "Add &Connection..."), + &browser, &Browser::openNewConnectionDialog); fileMenu->addSeparator(); - fileMenu->addAction(QObject::tr("&Quit"), []() { qApp->quit(); }); + fileMenu->addAction(QApplication::translate("MainWindow", "&Quit"), + qApp, &QApplication::quit); QMenu *helpMenu = mainWin.menuBar()->addMenu(QObject::tr("&Help")); - helpMenu->addAction(QObject::tr("About"), [&]() { browser.about(); }); - helpMenu->addAction(QObject::tr("About Qt"), []() { qApp->aboutQt(); }); + helpMenu->addAction(QApplication::translate("MainWindow", "About"), + &browser, &Browser::about); + helpMenu->addAction(QApplication::translate("MainWindow", "About Qt"), + qApp, &QApplication::aboutQt); - QObject::connect(&browser, &Browser::statusMessage, [&mainWin](const QString &text) { - mainWin.statusBar()->showMessage(text); - }); + QObject::connect(&browser, &Browser::statusMessage, + &mainWin, [&mainWin](const QString &text) { mainWin.statusBar()->showMessage(text); }); addConnectionsFromCommandline(app.arguments(), &browser); mainWin.show(); if (QSqlDatabase::connectionNames().isEmpty()) - QMetaObject::invokeMethod(&browser, "addConnection", Qt::QueuedConnection); + QMetaObject::invokeMethod(&browser, &Browser::openNewConnectionDialog, + Qt::QueuedConnection); return app.exec(); } diff --git a/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp b/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp index 482048ab98..2b85e95805 100644 --- a/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp +++ b/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp @@ -1,117 +1,87 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #include "qsqlconnectiondialog.h" -#include "ui_qsqlconnectiondialog.h" +#include <ui_qsqlconnectiondialog.h> +#include <QCheckBox> +#include <QMessageBox> +#include <QPushButton> #include <QSqlDatabase> QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent) : QDialog(parent) + , m_ui(new Ui::QSqlConnectionDialogUi) { - ui.setupUi(this); + m_ui->setupUi(this); QStringList drivers = QSqlDatabase::drivers(); if (!drivers.contains("QSQLITE")) - ui.dbCheckBox->setEnabled(false); + m_ui->dbCheckBox->setEnabled(false); - ui.comboDriver->addItems(drivers); + m_ui->comboDriver->addItems(drivers); + + connect(m_ui->okButton, &QPushButton::clicked, + this, &QSqlConnectionDialog::onOkButton); + connect(m_ui->cancelButton, &QPushButton::clicked, + this, &QSqlConnectionDialog::reject); + connect(m_ui->dbCheckBox, &QCheckBox::stateChanged, + this, &QSqlConnectionDialog::onDbCheckBox); } QSqlConnectionDialog::~QSqlConnectionDialog() { + delete m_ui; } QString QSqlConnectionDialog::driverName() const { - return ui.comboDriver->currentText(); + return m_ui->comboDriver->currentText(); } QString QSqlConnectionDialog::databaseName() const { - return ui.editDatabase->text(); + return m_ui->editDatabase->text(); } QString QSqlConnectionDialog::userName() const { - return ui.editUsername->text(); + return m_ui->editUsername->text(); } QString QSqlConnectionDialog::password() const { - return ui.editPassword->text(); + return m_ui->editPassword->text(); } QString QSqlConnectionDialog::hostName() const { - return ui.editHostname->text(); + return m_ui->editHostname->text(); } int QSqlConnectionDialog::port() const { - return ui.portSpinBox->value(); + return m_ui->portSpinBox->value(); } bool QSqlConnectionDialog::useInMemoryDatabase() const { - return ui.dbCheckBox->isChecked(); + return m_ui->dbCheckBox->isChecked(); } -void QSqlConnectionDialog::on_okButton_clicked() +void QSqlConnectionDialog::onOkButton() { - if (ui.comboDriver->currentText().isEmpty()) { + if (m_ui->comboDriver->currentText().isEmpty()) { QMessageBox::information(this, tr("No database driver selected"), tr("Please select a database driver")); - ui.comboDriver->setFocus(); + m_ui->comboDriver->setFocus(); } else { accept(); } } + +void QSqlConnectionDialog::onDbCheckBox() +{ + m_ui->connGroupBox->setEnabled(!m_ui->dbCheckBox->isChecked()); +} diff --git a/examples/sql/sqlbrowser/qsqlconnectiondialog.h b/examples/sql/sqlbrowser/qsqlconnectiondialog.h index fc998d7ee9..a6384bb5b7 100644 --- a/examples/sql/sqlbrowser/qsqlconnectiondialog.h +++ b/examples/sql/sqlbrowser/qsqlconnectiondialog.h @@ -1,62 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the demonstration applications of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** BSD License Usage -** Alternatively, you may use this file under the terms of the BSD license -** as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause #ifndef QSQLCONNECTIONDIALOG_H #define QSQLCONNECTIONDIALOG_H #include <QDialog> -#include <QMessageBox> -#include "ui_qsqlconnectiondialog.h" +QT_BEGIN_NAMESPACE +namespace Ui +{ +class QSqlConnectionDialogUi; +} +QT_END_NAMESPACE -class QSqlConnectionDialog: public QDialog +class QSqlConnectionDialog : public QDialog { Q_OBJECT public: @@ -72,12 +29,11 @@ public: bool useInMemoryDatabase() const; private slots: - void on_okButton_clicked(); - void on_cancelButton_clicked() { reject(); } - void on_dbCheckBox_clicked() { ui.connGroupBox->setEnabled(!ui.dbCheckBox->isChecked()); } + void onOkButton(); + void onDbCheckBox(); private: - Ui::QSqlConnectionDialogUi ui; + Ui::QSqlConnectionDialogUi *m_ui; }; #endif |