Standard dialogs example: Fix some clang-tidy warnings

- Use auto * when initializing with new
- Initialize variables
- Fix static invocations
- Use per-class includes
- Minor cleanups

Pick-to: 6.9 6.8
Change-Id: I137bc7dfad63bc55a1b1bbc3f42d758bbfdb86ba
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
This commit is contained in:
Friedemann Kleint 2025-01-20 19:26:05 +01:00
parent ef73a5f175
commit f388ca8841
2 changed files with 50 additions and 45 deletions

View File

@ -1,10 +1,25 @@
// Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include <QtWidgets>
#include "dialog.h" #include "dialog.h"
#include <QCheckBox>
#include <QColorDialog>
#include <QErrorMessage>
#include <QFileDialog>
#include <QFontDialog>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QToolBox>
#include <QGuiApplication>
#include <QStyleHints>
class DialogOptionsWidget : public QGroupBox class DialogOptionsWidget : public QGroupBox
{ {
public: public:
@ -15,7 +30,7 @@ public:
int value() const; int value() const;
private: private:
typedef QPair<QCheckBox *, int> CheckBoxEntry; using CheckBoxEntry = QPair<QCheckBox *, int>;
QVBoxLayout *layout; QVBoxLayout *layout;
QList<CheckBoxEntry> checkBoxEntries; QList<CheckBoxEntry> checkBoxEntries;
}; };
@ -29,7 +44,7 @@ DialogOptionsWidget::DialogOptionsWidget(QWidget *parent) :
void DialogOptionsWidget::addCheckBox(const QString &text, int value) void DialogOptionsWidget::addCheckBox(const QString &text, int value)
{ {
QCheckBox *checkBox = new QCheckBox(text); auto *checkBox = new QCheckBox(text);
layout->addWidget(checkBox); layout->addWidget(checkBox);
checkBoxEntries.append(CheckBoxEntry(checkBox, value)); checkBoxEntries.append(CheckBoxEntry(checkBox, value));
} }
@ -52,17 +67,17 @@ int DialogOptionsWidget::value() const
Dialog::Dialog(QWidget *parent) Dialog::Dialog(QWidget *parent)
: QWidget(parent) : QWidget(parent)
{ {
QVBoxLayout *verticalLayout; QVBoxLayout *verticalLayout{};
if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) { if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) {
QHBoxLayout *horizontalLayout = new QHBoxLayout(this); auto *horizontalLayout = new QHBoxLayout(this);
QGroupBox *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName(), this); auto *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName(), this);
horizontalLayout->addWidget(groupBox); horizontalLayout->addWidget(groupBox);
verticalLayout = new QVBoxLayout(groupBox); verticalLayout = new QVBoxLayout(groupBox);
} else { } else {
verticalLayout = new QVBoxLayout(this); verticalLayout = new QVBoxLayout(this);
} }
QToolBox *toolbox = new QToolBox; auto *toolbox = new QToolBox;
verticalLayout->addWidget(toolbox); verticalLayout->addWidget(toolbox);
errorMessageDialog = new QErrorMessage(this); errorMessageDialog = new QErrorMessage(this);
@ -71,58 +86,51 @@ Dialog::Dialog(QWidget *parent)
integerLabel = new QLabel; integerLabel = new QLabel;
integerLabel->setFrameStyle(frameStyle); integerLabel->setFrameStyle(frameStyle);
QPushButton *integerButton = auto *integerButton = new QPushButton(tr("QInputDialog::get&Int()"));
new QPushButton(tr("QInputDialog::get&Int()"));
doubleLabel = new QLabel; doubleLabel = new QLabel;
doubleLabel->setFrameStyle(frameStyle); doubleLabel->setFrameStyle(frameStyle);
QPushButton *doubleButton = auto *doubleButton = new QPushButton(tr("QInputDialog::get&Double()"));
new QPushButton(tr("QInputDialog::get&Double()"));
itemLabel = new QLabel; itemLabel = new QLabel;
itemLabel->setFrameStyle(frameStyle); itemLabel->setFrameStyle(frameStyle);
QPushButton *itemButton = new QPushButton(tr("QInputDialog::getIte&m()")); auto *itemButton = new QPushButton(tr("QInputDialog::getIte&m()"));
textLabel = new QLabel; textLabel = new QLabel;
textLabel->setFrameStyle(frameStyle); textLabel->setFrameStyle(frameStyle);
QPushButton *textButton = new QPushButton(tr("QInputDialog::get&Text()")); auto *textButton = new QPushButton(tr("QInputDialog::get&Text()"));
multiLineTextLabel = new QLabel; multiLineTextLabel = new QLabel;
multiLineTextLabel->setFrameStyle(frameStyle); multiLineTextLabel->setFrameStyle(frameStyle);
QPushButton *multiLineTextButton = new QPushButton(tr("QInputDialog::get&MultiLineText()")); auto *multiLineTextButton = new QPushButton(tr("QInputDialog::get&MultiLineText()"));
colorLabel = new QLabel; colorLabel = new QLabel;
colorLabel->setFrameStyle(frameStyle); colorLabel->setFrameStyle(frameStyle);
QPushButton *colorButton = new QPushButton(tr("QColorDialog::get&Color()")); auto *colorButton = new QPushButton(tr("QColorDialog::get&Color()"));
fontLabel = new QLabel; fontLabel = new QLabel;
fontLabel->setFrameStyle(frameStyle); fontLabel->setFrameStyle(frameStyle);
QPushButton *fontButton = new QPushButton(tr("QFontDialog::get&Font()")); auto *fontButton = new QPushButton(tr("QFontDialog::get&Font()"));
directoryLabel = new QLabel; directoryLabel = new QLabel;
directoryLabel->setFrameStyle(frameStyle); directoryLabel->setFrameStyle(frameStyle);
QPushButton *directoryButton = auto *directoryButton = new QPushButton(tr("QFileDialog::getE&xistingDirectory()"));
new QPushButton(tr("QFileDialog::getE&xistingDirectory()"));
openFileNameLabel = new QLabel; openFileNameLabel = new QLabel;
openFileNameLabel->setFrameStyle(frameStyle); openFileNameLabel->setFrameStyle(frameStyle);
QPushButton *openFileNameButton = auto *openFileNameButton = new QPushButton(tr("QFileDialog::get&OpenFileName()"));
new QPushButton(tr("QFileDialog::get&OpenFileName()"));
openFileNamesLabel = new QLabel; openFileNamesLabel = new QLabel;
openFileNamesLabel->setFrameStyle(frameStyle); openFileNamesLabel->setFrameStyle(frameStyle);
QPushButton *openFileNamesButton = auto *openFileNamesButton = new QPushButton(tr("QFileDialog::&getOpenFileNames()"));
new QPushButton(tr("QFileDialog::&getOpenFileNames()"));
saveFileNameLabel = new QLabel; saveFileNameLabel = new QLabel;
saveFileNameLabel->setFrameStyle(frameStyle); saveFileNameLabel->setFrameStyle(frameStyle);
QPushButton *saveFileNameButton = auto *saveFileNameButton = new QPushButton(tr("QFileDialog::get&SaveFileName()"));
new QPushButton(tr("QFileDialog::get&SaveFileName()"));
criticalLabel = new QLabel; criticalLabel = new QLabel;
criticalLabel->setFrameStyle(frameStyle); criticalLabel->setFrameStyle(frameStyle);
QPushButton *criticalButton = auto *criticalButton = new QPushButton(tr("QMessageBox::critica&l()"));
new QPushButton(tr("QMessageBox::critica&l()"));
informationLabel = new QLabel; informationLabel = new QLabel;
informationLabel->setFrameStyle(frameStyle); informationLabel->setFrameStyle(frameStyle);
@ -131,15 +139,13 @@ Dialog::Dialog(QWidget *parent)
questionLabel = new QLabel; questionLabel = new QLabel;
questionLabel->setFrameStyle(frameStyle); questionLabel->setFrameStyle(frameStyle);
QPushButton *questionButton = auto *questionButton = new QPushButton(tr("QMessageBox::&question()"));
new QPushButton(tr("QMessageBox::&question()"));
warningLabel = new QLabel; warningLabel = new QLabel;
warningLabel->setFrameStyle(frameStyle); warningLabel->setFrameStyle(frameStyle);
QPushButton *warningButton = new QPushButton(tr("QMessageBox::&warning()")); auto *warningButton = new QPushButton(tr("QMessageBox::&warning()"));
QPushButton *errorButton = auto *errorButton = new QPushButton(tr("QErrorMessage::showM&essage()"));
new QPushButton(tr("QErrorMessage::showM&essage()"));
connect(integerButton, &QAbstractButton::clicked, this, &Dialog::setInteger); connect(integerButton, &QAbstractButton::clicked, this, &Dialog::setInteger);
connect(doubleButton, &QAbstractButton::clicked, this, &Dialog::setDouble); connect(doubleButton, &QAbstractButton::clicked, this, &Dialog::setDouble);
@ -163,8 +169,8 @@ Dialog::Dialog(QWidget *parent)
connect(warningButton, &QAbstractButton::clicked, this, &Dialog::warningMessage); connect(warningButton, &QAbstractButton::clicked, this, &Dialog::warningMessage);
connect(errorButton, &QAbstractButton::clicked, this, &Dialog::errorMessage); connect(errorButton, &QAbstractButton::clicked, this, &Dialog::errorMessage);
QWidget *page = new QWidget; auto *page = new QWidget;
QGridLayout *layout = new QGridLayout(page); auto *layout = new QGridLayout(page);
layout->setColumnStretch(1, 1); layout->setColumnStretch(1, 1);
layout->setColumnMinimumWidth(1, 250); layout->setColumnMinimumWidth(1, 250);
layout->addWidget(integerButton, 0, 0); layout->addWidget(integerButton, 0, 0);
@ -268,7 +274,7 @@ void Dialog::setInteger()
void Dialog::setDouble() void Dialog::setDouble()
{ {
//! [1] //! [1]
bool ok; bool ok{};
double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"), double d = QInputDialog::getDouble(this, tr("QInputDialog::getDouble()"),
tr("Amount:"), 37.56, -10000, 10000, 2, &ok, tr("Amount:"), 37.56, -10000, 10000, 2, &ok,
Qt::WindowFlags(), 1); Qt::WindowFlags(), 1);
@ -280,10 +286,9 @@ void Dialog::setDouble()
void Dialog::setItem() void Dialog::setItem()
{ {
//! [2] //! [2]
QStringList items; const QStringList items{tr("Spring"), tr("Summer"), tr("Fall"), tr("Winter")};
items << tr("Spring") << tr("Summer") << tr("Fall") << tr("Winter");
bool ok; bool ok{};
QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"), QString item = QInputDialog::getItem(this, tr("QInputDialog::getItem()"),
tr("Season:"), items, 0, false, &ok); tr("Season:"), items, 0, false, &ok);
if (ok && !item.isEmpty()) if (ok && !item.isEmpty())
@ -294,7 +299,7 @@ void Dialog::setItem()
void Dialog::setText() void Dialog::setText()
{ {
//! [3] //! [3]
bool ok; bool ok{};
QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"), QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
tr("User name:"), QLineEdit::Normal, tr("User name:"), QLineEdit::Normal,
QDir::home().dirName(), &ok); QDir::home().dirName(), &ok);
@ -306,7 +311,7 @@ void Dialog::setText()
void Dialog::setMultiLineText() void Dialog::setMultiLineText()
{ {
//! [4] //! [4]
bool ok; bool ok{};
QString text = QInputDialog::getMultiLineText(this, tr("QInputDialog::getMultiLineText()"), QString text = QInputDialog::getMultiLineText(this, tr("QInputDialog::getMultiLineText()"),
tr("Address:"), "John Doe\nFreedom Street", &ok); tr("Address:"), "John Doe\nFreedom Street", &ok);
if (ok && !text.isEmpty()) if (ok && !text.isEmpty())
@ -335,7 +340,7 @@ void Dialog::setFont()
if (!description.isEmpty()) if (!description.isEmpty())
defaultFont.fromString(description); defaultFont.fromString(description);
bool ok; bool ok{};
QFont font = QFontDialog::getFont(&ok, defaultFont, this, "Select Font", options); QFont font = QFontDialog::getFont(&ok, defaultFont, this, "Select Font", options);
if (ok) { if (ok) {
fontLabel->setText(font.key()); fontLabel->setText(font.key());
@ -379,8 +384,8 @@ void Dialog::setOpenFileNames()
tr("All Files (*);;Text Files (*.txt)"), tr("All Files (*);;Text Files (*.txt)"),
&selectedFilter, &selectedFilter,
options); options);
if (files.count()) { if (!files.isEmpty()) {
openFilesPath = files[0]; openFilesPath = files.constFirst();
openFileNamesLabel->setText(QString("[%1]").arg(files.join(", "))); openFileNamesLabel->setText(QString("[%1]").arg(files.join(", ")));
} }
} }

View File

@ -20,7 +20,7 @@ int main(int argc, char *argv[])
QTranslator translator; QTranslator translator;
if (translator.load(QLocale::system(), u"qtbase"_s, u"_"_s, if (translator.load(QLocale::system(), u"qtbase"_s, u"_"_s,
QLibraryInfo::path(QLibraryInfo::TranslationsPath))) { QLibraryInfo::path(QLibraryInfo::TranslationsPath))) {
app.installTranslator(&translator); QCoreApplication::installTranslator(&translator);
} }
#endif #endif
@ -35,5 +35,5 @@ int main(int argc, char *argv[])
} }
dialog.show(); dialog.show();
return app.exec(); return QCoreApplication::exec();
} }