From 8dbd0828e94497ab6ee6c5d924e51b4778e5bccc Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 31 Dec 2024 10:03:41 +0100 Subject: [PATCH] sqlbrowser example: use idiomatic Qt [2/3]: use button-box / override accept() - The old code used two QPushButtons in a QHBoxLayout to provide Ok/Cancel buttons. This hard-codes the positions and text (and icons) of these buttons, instead of adapting to the platform style. The new code simply uses QDialogButtonBox, which is designed for this purpose. - Also, the old code connected the Ok button's clicked() signal to a custom slot that then called QDialog::accept(). This means that the code in the custom slot is not executed when the dialog is accepted by other means (e.g. return press in one of the line edits ("auto-default"), though I'm not sure here). The new code uses the idiomatic Qt way of overriding QDialog::accept() instead, and connects the button-box's accepted() signal to it. This is done in the .ui file, so it already works in Designer preview. - Finally, the old code made a manual connection from the Cancel button to QDialog::reject(). The new code uses the Qt idiom of connecting in the .ui file directly, using QDialogButtonBox::rejected() as the signal. Amends 2690822428deec4f0c08f4d118d69a7c6036369e, which, however, inherited all of the above from even older code. Pick-to: 6.8 Change-Id: I83afd6156a0811e0c0f99f2480625ea6b69ff78b Reviewed-by: Volker Hilsheimer (cherry picked from commit 3419c299369ac1da94ba5710aaf5f5f65c38c33c) Reviewed-by: Qt Cherry-pick Bot --- .../sql/sqlbrowser/qsqlconnectiondialog.cpp | 9 +-- .../sql/sqlbrowser/qsqlconnectiondialog.h | 3 +- .../sql/sqlbrowser/qsqlconnectiondialog.ui | 78 +++++++++---------- 3 files changed, 39 insertions(+), 51 deletions(-) diff --git a/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp b/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp index eaaacd8e167..21c22b84d9b 100644 --- a/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp +++ b/examples/sql/sqlbrowser/qsqlconnectiondialog.cpp @@ -21,11 +21,6 @@ QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent) m_ui->dbCheckBox->setEnabled(false); m_ui->comboDriver->addItems(drivers); - - connect(m_ui->okButton, &QPushButton::clicked, - this, &QSqlConnectionDialog::onOkButton); - connect(m_ui->cancelButton, &QPushButton::clicked, - this, &QSqlConnectionDialog::reject); } QSqlConnectionDialog::~QSqlConnectionDialog() @@ -68,13 +63,13 @@ bool QSqlConnectionDialog::useInMemoryDatabase() const return m_ui->dbCheckBox->isChecked(); } -void QSqlConnectionDialog::onOkButton() +void QSqlConnectionDialog::accept() { if (m_ui->comboDriver->currentText().isEmpty()) { QMessageBox::information(this, tr("No database driver selected"), tr("Please select a database driver")); m_ui->comboDriver->setFocus(); } else { - accept(); + QDialog::accept(); } } diff --git a/examples/sql/sqlbrowser/qsqlconnectiondialog.h b/examples/sql/sqlbrowser/qsqlconnectiondialog.h index 605fe376c2c..b5654ea9133 100644 --- a/examples/sql/sqlbrowser/qsqlconnectiondialog.h +++ b/examples/sql/sqlbrowser/qsqlconnectiondialog.h @@ -28,8 +28,7 @@ public: int port() const; bool useInMemoryDatabase() const; -private slots: - void onOkButton(); + void accept() override; private: Ui::QSqlConnectionDialogUi *m_ui; diff --git a/examples/sql/sqlbrowser/qsqlconnectiondialog.ui b/examples/sql/sqlbrowser/qsqlconnectiondialog.ui index b99897aa3ba..ae562316f3c 100644 --- a/examples/sql/sqlbrowser/qsqlconnectiondialog.ui +++ b/examples/sql/sqlbrowser/qsqlconnectiondialog.ui @@ -163,47 +163,11 @@ - - - 0 + + + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok - - 6 - - - - - Qt::Horizontal - - - QSizePolicy::Expanding - - - - 20 - 20 - - - - - - - - &OK - - - true - - - - - - - &Cancel - - - - + @@ -216,8 +180,6 @@ editHostname portSpinBox dbCheckBox - okButton - cancelButton @@ -237,5 +199,37 @@ + + buttonBox + accepted() + QSqlConnectionDialogUi + accept() + + + 19 + 278 + + + 20 + 244 + + + + + buttonBox + rejected() + QSqlConnectionDialogUi + reject() + + + 58 + 276 + + + 58 + 258 + + +