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 <volker.hilsheimer@qt.io>
(cherry picked from commit 3419c299369ac1da94ba5710aaf5f5f65c38c33c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-12-31 10:03:41 +01:00 committed by Qt Cherry-pick Bot
parent f2b88b3225
commit 8dbd0828e9
3 changed files with 39 additions and 51 deletions

View File

@ -21,11 +21,6 @@ QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent)
m_ui->dbCheckBox->setEnabled(false); m_ui->dbCheckBox->setEnabled(false);
m_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);
} }
QSqlConnectionDialog::~QSqlConnectionDialog() QSqlConnectionDialog::~QSqlConnectionDialog()
@ -68,13 +63,13 @@ bool QSqlConnectionDialog::useInMemoryDatabase() const
return m_ui->dbCheckBox->isChecked(); return m_ui->dbCheckBox->isChecked();
} }
void QSqlConnectionDialog::onOkButton() void QSqlConnectionDialog::accept()
{ {
if (m_ui->comboDriver->currentText().isEmpty()) { if (m_ui->comboDriver->currentText().isEmpty()) {
QMessageBox::information(this, tr("No database driver selected"), QMessageBox::information(this, tr("No database driver selected"),
tr("Please select a database driver")); tr("Please select a database driver"));
m_ui->comboDriver->setFocus(); m_ui->comboDriver->setFocus();
} else { } else {
accept(); QDialog::accept();
} }
} }

View File

@ -28,8 +28,7 @@ public:
int port() const; int port() const;
bool useInMemoryDatabase() const; bool useInMemoryDatabase() const;
private slots: void accept() override;
void onOkButton();
private: private:
Ui::QSqlConnectionDialogUi *m_ui; Ui::QSqlConnectionDialogUi *m_ui;

View File

@ -163,48 +163,12 @@
</layout> </layout>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" > <widget class="QDialogButtonBox" name="buttonBox">
<property name="margin" > <property name="standardButtons">
<number>0</number> <set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
<property name="spacing" >
<number>6</number>
</property>
<item>
<spacer>
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType" >
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" >
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="okButton" >
<property name="text" >
<string>&amp;OK</string>
</property>
<property name="default" >
<bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="cancelButton" >
<property name="text" >
<string>&amp;Cancel</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<pixmapfunction></pixmapfunction> <pixmapfunction></pixmapfunction>
@ -216,8 +180,6 @@
<tabstop>editHostname</tabstop> <tabstop>editHostname</tabstop>
<tabstop>portSpinBox</tabstop> <tabstop>portSpinBox</tabstop>
<tabstop>dbCheckBox</tabstop> <tabstop>dbCheckBox</tabstop>
<tabstop>okButton</tabstop>
<tabstop>cancelButton</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>
<connections> <connections>
@ -237,5 +199,37 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>QSqlConnectionDialogUi</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>19</x>
<y>278</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>244</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>QSqlConnectionDialogUi</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>58</x>
<y>276</y>
</hint>
<hint type="destinationlabel">
<x>58</x>
<y>258</y>
</hint>
</hints>
</connection>
</connections> </connections>
</ui> </ui>