sqlbrowser example: use unique_ptr to hold m_ui

The old code used manual memory mangement (raw new/delete) to
(de)allocate the Ui struct. This is so 80s.

Use an owning smart pointer to manage the memory. Ordinarily, this
would have been QScopedPointer, but seeing as that doesn't have a
create() method to hide the raw new, use std::unique_ptr and
std::make_unique() instead.

Amends 2690822428deec4f0c08f4d118d69a7c6036369e.

Change-Id: Icabb9154eb38630855e14094b958af0214516f6b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 0da2c2c4ef2219967db87021eece2a60b6e207af)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 88600c30964bc1d11ed09e3dddd4cbe4477f02d3)
This commit is contained in:
Marc Mutz 2024-12-31 10:03:41 +01:00 committed by Qt Cherry-pick Bot
parent ef77a08f76
commit 9be434b3b1
4 changed files with 10 additions and 10 deletions

View File

@ -18,7 +18,7 @@
Browser::Browser(QWidget *parent)
: QWidget(parent)
, m_ui(new Ui::Browser)
, m_ui{std::make_unique<Ui::Browser>()}
{
m_ui->setupUi(this);
@ -60,9 +60,7 @@ Browser::Browser(QWidget *parent)
}
Browser::~Browser()
{
delete m_ui;
}
= default;
void Browser::exec()
{

View File

@ -7,6 +7,8 @@
#include <QWidget>
#include <QSqlTableModel>
#include <memory>
QT_FORWARD_DECLARE_CLASS(QSqlError)
QT_BEGIN_NAMESPACE
@ -50,7 +52,7 @@ signals:
void statusMessage(const QString &message);
private:
Ui::Browser *m_ui;
const std::unique_ptr<Ui::Browser> m_ui;
};
class CustomModel : public QSqlTableModel

View File

@ -11,7 +11,7 @@
QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent)
: QDialog(parent)
, m_ui(new Ui::QSqlConnectionDialogUi)
, m_ui{std::make_unique<Ui::QSqlConnectionDialogUi>()}
{
m_ui->setupUi(this);
@ -24,9 +24,7 @@ QSqlConnectionDialog::QSqlConnectionDialog(QWidget *parent)
}
QSqlConnectionDialog::~QSqlConnectionDialog()
{
delete m_ui;
}
= default;
QString QSqlConnectionDialog::driverName() const
{

View File

@ -6,6 +6,8 @@
#include <QDialog>
#include <memory>
QT_BEGIN_NAMESPACE
namespace Ui
{
@ -31,7 +33,7 @@ public:
void accept() override;
private:
Ui::QSqlConnectionDialogUi *m_ui;
const std::unique_ptr<Ui::QSqlConnectionDialogUi> m_ui;
};
#endif