Cleanup QtWidgets (widgets) examples

Cleanup QtWidgets widgets examples:
 - use member-init (clang-tidy)
 - fix includes/don't include QtWidgets globally
 - include own header first
 - use nullptr (clang-tidy)
 - avoid c-style casts
 - use QVector instead QList

Change-Id: Ib56bb507eb2ef885f1ddc664050d3c7af92adb70
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-09-06 22:38:45 +02:00
parent 7db335a77e
commit cb54c16584
52 changed files with 330 additions and 197 deletions

View File

@ -142,6 +142,9 @@
pendingAdditiveOperator and \c pendingMultiplicativeOperator
variables don't need to be initialized explicitly, because the
QString constructor initializes them to empty strings.
It is also possible to initialize those variable directly in the
header. This is called \c member-initializaton and avoids a long
initialization list.
\snippet widgets/calculator/calculator.cpp 1
\snippet widgets/calculator/calculator.cpp 2

View File

@ -95,7 +95,7 @@
\snippet widgets/tooltips/sortingbox.h 2
We keep all the shape items in a QList, and we keep three
We keep all the shape items in a QVector, and we keep three
QPainterPath objects holding the shapes of a circle, a square and
a triangle. We also need to have a pointer to an item when it is
moving, and we need to know its previous position.

View File

@ -50,7 +50,9 @@
#include "analogclock.h"
#include <QtWidgets>
#include <QPainter>
#include <QTime>
#include <QTimer>
//! [0] //! [1]
AnalogClock::AnalogClock(QWidget *parent)

View File

@ -50,8 +50,6 @@
#include "button.h"
#include <QtWidgets>
//! [0]
Button::Button(const QString &text, QWidget *parent)
: QToolButton(parent)

View File

@ -48,21 +48,18 @@
**
****************************************************************************/
#include "button.h"
#include "calculator.h"
#include "button.h"
#include <QtWidgets>
#include <cmath>
#include <QGridLayout>
#include <QLineEdit>
#include <QtMath>
//! [0]
Calculator::Calculator(QWidget *parent)
: QWidget(parent)
: QWidget(parent), sumInMemory(0.0), sumSoFar(0.0)
, factorSoFar(0.0), waitingForOperand(true)
{
sumInMemory = 0.0;
sumSoFar = 0.0;
factorSoFar = 0.0;
waitingForOperand = true;
//! [0]
//! [1]
@ -78,9 +75,8 @@ Calculator::Calculator(QWidget *parent)
//! [2]
//! [4]
for (int i = 0; i < NumDigitButtons; ++i) {
for (int i = 0; i < NumDigitButtons; ++i)
digitButtons[i] = createButton(QString::number(i), SLOT(digitClicked()));
}
Button *pointButton = createButton(tr("."), SLOT(pointClicked()));
Button *changeSignButton = createButton(tr("\302\261"), SLOT(changeSignClicked()));
@ -194,6 +190,8 @@ void Calculator::additiveOperatorClicked()
//! [10] //! [11]
{
Button *clickedButton = qobject_cast<Button *>(sender());
if (!clickedButton)
return;
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();
@ -233,6 +231,8 @@ void Calculator::additiveOperatorClicked()
void Calculator::multiplicativeOperatorClicked()
{
Button *clickedButton = qobject_cast<Button *>(sender());
if (!clickedButton)
return;
QString clickedOperator = clickedButton->text();
double operand = display->text().toDouble();

View File

@ -48,7 +48,7 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QApplication>
#include "window.h"

View File

@ -48,10 +48,18 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include <QCalendarWidget>
#include <QCheckBox>
#include <QComboBox>
#include <QDateEdit>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLocale>
#include <QTextCharFormat>
//! [0]
Window::Window(QWidget *parent)
: QWidget(parent)
@ -166,13 +174,12 @@ void Window::reformatHeaders()
QString text = headerTextFormatCombo->currentText();
QTextCharFormat format;
if (text == tr("Bold")) {
if (text == tr("Bold"))
format.setFontWeight(QFont::Bold);
} else if (text == tr("Italic")) {
else if (text == tr("Italic"))
format.setFontItalic(true);
} else if (text == tr("Green")) {
else if (text == tr("Green"))
format.setForeground(Qt::green);
}
calendar->setHeaderTextFormat(format);
}
//! [7]

View File

@ -50,11 +50,14 @@
#include "characterwidget.h"
#include <QtWidgets>
#include <QFontDatabase>
#include <QMouseEvent>
#include <QPainter>
#include <QToolTip>
//! [0]
CharacterWidget::CharacterWidget(QWidget *parent)
: QWidget(parent), columns(16), lastKey(-1)
: QWidget(parent)
{
calculateSquareSize();
setMouseTracking(true);
@ -110,7 +113,7 @@ void CharacterWidget::calculateSquareSize()
//! [3]
QSize CharacterWidget::sizeHint() const
{
return QSize(columns*squareSize, (65536/columns)*squareSize);
return QSize(columns*squareSize, (65536 / columns) * squareSize);
}
//! [3]
@ -118,7 +121,7 @@ QSize CharacterWidget::sizeHint() const
void CharacterWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint widgetPosition = mapFromGlobal(event->globalPos());
uint key = (widgetPosition.y()/squareSize)*columns + widgetPosition.x()/squareSize;
uint key = (widgetPosition.y() / squareSize) * columns + widgetPosition.x() / squareSize;
QString text = QString::fromLatin1("<p>Character: <span style=\"font-size: 24pt; font-family: %1\">").arg(displayFont.family())
+ QChar(key)
@ -132,7 +135,7 @@ void CharacterWidget::mouseMoveEvent(QMouseEvent *event)
void CharacterWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
lastKey = (event->y()/squareSize)*columns + event->x()/squareSize;
lastKey = (event->y() / squareSize) * columns + event->x() / squareSize;
if (QChar(lastKey).category() != QChar::Other_NotAssigned)
emit characterSelected(QString(QChar(lastKey)));
update();
@ -152,17 +155,17 @@ void CharacterWidget::paintEvent(QPaintEvent *event)
//! [7]
QRect redrawRect = event->rect();
int beginRow = redrawRect.top()/squareSize;
int endRow = redrawRect.bottom()/squareSize;
int beginColumn = redrawRect.left()/squareSize;
int endColumn = redrawRect.right()/squareSize;
int beginRow = redrawRect.top() / squareSize;
int endRow = redrawRect.bottom() / squareSize;
int beginColumn = redrawRect.left() / squareSize;
int endColumn = redrawRect.right() / squareSize;
//! [7]
//! [8]
painter.setPen(QPen(Qt::gray));
for (int row = beginRow; row <= endRow; ++row) {
for (int column = beginColumn; column <= endColumn; ++column) {
painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize);
painter.drawRect(column * squareSize, row * squareSize, squareSize, squareSize);
}
//! [8] //! [9]
}
@ -172,17 +175,17 @@ void CharacterWidget::paintEvent(QPaintEvent *event)
QFontMetrics fontMetrics(displayFont);
painter.setPen(QPen(Qt::black));
for (int row = beginRow; row <= endRow; ++row) {
for (int column = beginColumn; column <= endColumn; ++column) {
int key = row*columns + column;
painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
int key = row * columns + column;
painter.setClipRect(column * squareSize, row * squareSize, squareSize, squareSize);
if (key == lastKey)
painter.fillRect(column*squareSize + 1, row*squareSize + 1, squareSize, squareSize, QBrush(Qt::red));
painter.fillRect(column * squareSize + 1, row * squareSize + 1,
squareSize, squareSize, QBrush(Qt::red));
painter.drawText(column*squareSize + (squareSize / 2) - fontMetrics.horizontalAdvance(QChar(key))/2,
row*squareSize + 4 + fontMetrics.ascent(),
painter.drawText(column * squareSize + (squareSize / 2) -
fontMetrics.horizontalAdvance(QChar(key)) / 2,
row * squareSize + 4 + fontMetrics.ascent(),
QString(QChar(key)));
}
}

View File

@ -88,9 +88,9 @@ private:
void calculateSquareSize();
QFont displayFont;
int columns;
int lastKey;
int squareSize;
int columns = 16;
int lastKey = -1;
int squareSize = 0;
};
//! [0]

View File

@ -48,13 +48,27 @@
**
****************************************************************************/
#include <QtWidgets>
#include "characterwidget.h"
#include "mainwindow.h"
#include "characterwidget.h"
#include <QApplication>
#include <QBoxLayout>
#include <QCheckBox>
#include <QClipboard>
#include <QDesktopWidget>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFontComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QMenuBar>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QScrollArea>
#include <QStatusBar>
#include <QTextStream>
//! [0]
Q_DECLARE_METATYPE(QFontComboBox::FontFilter)
MainWindow::MainWindow(QWidget *parent)

View File

@ -48,10 +48,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include "codeeditor.h"
#include <QPainter>
#include <QTextBlock>
//![constructor]
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
@ -157,8 +158,8 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
//![extraAreaPaintEvent_1]
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
int top = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
int bottom = top + qRound(blockBoundingRect(block).height());
//![extraAreaPaintEvent_1]
//![extraAreaPaintEvent_2]
@ -172,7 +173,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
bottom = top + qRound(blockBoundingRect(block).height());
++blockNumber;
}
}

View File

@ -80,7 +80,7 @@ protected:
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect &, int);
void updateLineNumberArea(const QRect &rect, int dy);
private:
QWidget *lineNumberArea;
@ -92,16 +92,17 @@ private:
class LineNumberArea : public QWidget
{
public:
LineNumberArea(CodeEditor *editor) : QWidget(editor) {
codeEditor = editor;
}
LineNumberArea(CodeEditor *editor) : QWidget(editor), codeEditor(editor)
{}
QSize sizeHint() const override {
QSize sizeHint() const override
{
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) override {
void paintEvent(QPaintEvent *event) override
{
codeEditor->lineNumberAreaPaintEvent(event);
}

View File

@ -48,7 +48,7 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QApplication>
#include "codeeditor.h"

View File

@ -50,7 +50,8 @@
#include "digitalclock.h"
#include <QtWidgets>
#include <QTime>
#include <QTimer>
//! [0]
DigitalClock::DigitalClock(QWidget *parent)

View File

@ -48,10 +48,15 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include <QCheckBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QMenu>
#include <QPushButton>
#include <QRadioButton>
//! [0]
Window::Window(QWidget *parent)
: QWidget(parent)

View File

@ -50,7 +50,8 @@
#include "iconpreviewarea.h"
#include <QtWidgets>
#include <QGridLayout>
#include <QLabel>
//! [0]
IconPreviewArea::IconPreviewArea(QWidget *parent)

View File

@ -50,7 +50,7 @@
#include "iconsizespinbox.h"
#include <QtWidgets>
#include <QRegularExpression>
//! [0]
IconSizeSpinBox::IconSizeSpinBox(QWidget *parent)

View File

@ -51,7 +51,7 @@
#include "imagedelegate.h"
#include "iconpreviewarea.h"
#include <QtWidgets>
#include <QComboBox>
//! [0]
ImageDelegate::ImageDelegate(QObject *parent)

View File

@ -48,14 +48,29 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h"
#include "iconpreviewarea.h"
#include "iconsizespinbox.h"
#include "imagedelegate.h"
#include "mainwindow.h"
#include <memory>
#include <QApplication>
#include <QButtonGroup>
#include <QCheckBox>
#include <QFileDialog>
#include <QHeaderView>
#include <QFormLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QImageReader>
#include <QLabel>
#include <QMenuBar>
#include <QMessageBox>
#include <QRadioButton>
#include <QScreen>
#include <QStandardPaths>
#include <QStyleFactory>
#include <QTableWidget>
#include <QWindow>
//! [40]
enum { OtherSize = QStyle::PM_CustomBase };

View File

@ -50,7 +50,24 @@
#include "imageviewer.h"
#include <QtWidgets>
#include <QApplication>
#include <QClipboard>
#include <QColorSpace>
#include <QDir>
#include <QFileDialog>
#include <QImageReader>
#include <QImageWriter>
#include <QLabel>
#include <QMenuBar>
#include <QMessageBox>
#include <QMimeData>
#include <QPainter>
#include <QScreen>
#include <QScrollArea>
#include <QScrollBar>
#include <QStandardPaths>
#include <QStatusBar>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
@ -60,8 +77,8 @@
//! [0]
ImageViewer::ImageViewer(QWidget *parent)
: QMainWindow(parent), imageLabel(new QLabel),
scrollArea(new QScrollArea), scaleFactor(1)
: QMainWindow(parent), imageLabel(new QLabel)
, scrollArea(new QScrollArea)
{
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

View File

@ -98,7 +98,7 @@ private:
QImage image;
QLabel *imageLabel;
QScrollArea *scrollArea;
double scaleFactor;
double scaleFactor = 1;
#ifndef QT_NO_PRINTER
QPrinter printer;

View File

@ -48,10 +48,14 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include <QComboBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
//! [0]
Window::Window(QWidget *parent)
: QWidget(parent)
@ -197,6 +201,7 @@ void Window::echoChanged(int index)
break;
case 3:
echoLineEdit->setEchoMode(QLineEdit::NoEcho);
break;
}
}
//! [9]
@ -215,6 +220,7 @@ void Window::validatorChanged(int index)
case 2:
validatorLineEdit->setValidator(new QDoubleValidator(-999.0,
999.0, 2, validatorLineEdit));
break;
}
validatorLineEdit->clear();
@ -233,6 +239,7 @@ void Window::alignmentChanged(int index)
break;
case 2:
alignmentLineEdit->setAlignment(Qt::AlignRight);
break;
}
}
//! [11]
@ -254,6 +261,7 @@ void Window::inputMaskChanged(int index)
break;
case 3:
inputMaskLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");
break;
}
}
//! [12]
@ -267,6 +275,7 @@ void Window::accessChanged(int index)
break;
case 1:
accessLineEdit->setReadOnly(true);
break;
}
}
//! [13]

View File

@ -50,7 +50,15 @@
#include "movieplayer.h"
#include <QtWidgets>
#include <QCheckBox>
#include <QFileDialog>
#include <QLabel>
#include <QMovie>
#include <QSlider>
#include <QSpinBox>
#include <QStyle>
#include <QToolButton>
#include <QVBoxLayout>
MoviePlayer::MoviePlayer(QWidget *parent)
: QWidget(parent)

View File

@ -48,11 +48,17 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h"
#include "scribblearea.h"
#include <QApplication>
#include <QColorDialog>
#include <QFileDialog>
#include <QImageWriter>
#include <QInputDialog>
#include <QMenuBar>
#include <QMessageBox>
//! [0]
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), scribbleArea(new ScribbleArea(this))
@ -71,11 +77,10 @@ MainWindow::MainWindow(QWidget *parent)
void MainWindow::closeEvent(QCloseEvent *event)
//! [1] //! [2]
{
if (maybeSave()) {
if (maybeSave())
event->accept();
} else {
else
event->ignore();
}
}
//! [2]
@ -231,11 +236,10 @@ bool MainWindow::maybeSave()
"Do you want to save your changes?"),
QMessageBox::Save | QMessageBox::Discard
| QMessageBox::Cancel);
if (ret == QMessageBox::Save) {
if (ret == QMessageBox::Save)
return saveFile("png");
} else if (ret == QMessageBox::Cancel) {
else if (ret == QMessageBox::Cancel)
return false;
}
}
return true;
}
@ -252,10 +256,8 @@ bool MainWindow::saveFile(const QByteArray &fileFormat)
tr("%1 Files (*.%2);;All Files (*)")
.arg(QString::fromLatin1(fileFormat.toUpper()))
.arg(QString::fromLatin1(fileFormat)));
if (fileName.isEmpty()) {
if (fileName.isEmpty())
return false;
} else {
return scribbleArea->saveImage(fileName, fileFormat.constData());
}
return scribbleArea->saveImage(fileName, fileFormat.constData());
}
//! [20]

View File

@ -50,7 +50,9 @@
#include "scribblearea.h"
#include <QtWidgets>
#include <QMouseEvent>
#include <QPainter>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog)
@ -64,10 +66,6 @@ ScribbleArea::ScribbleArea(QWidget *parent)
: QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
modified = false;
scribbling = false;
myPenWidth = 1;
myPenColor = Qt::blue;
}
//! [0]
@ -98,9 +96,8 @@ bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat)
if (visibleImage.save(fileName, fileFormat)) {
modified = false;
return true;
} else {
return false;
}
return false;
}
//! [4]

View File

@ -88,10 +88,10 @@ private:
void drawLineTo(const QPoint &endPoint);
void resizeImage(QImage *image, const QSize &newSize);
bool modified;
bool scribbling;
int myPenWidth;
QColor myPenColor;
bool modified = false;
bool scribbling = false;
int myPenWidth = 1;
QColor myPenColor = Qt::blue;
QImage image;
QPoint lastPoint;
};

View File

@ -48,10 +48,15 @@
**
****************************************************************************/
#include <QtWidgets>
#include "shapedclock.h"
#include <QAction>
#include <QCoreApplication>
#include <QMouseEvent>
#include <QPainter>
#include <QTime>
#include <QTimer>
//! [0]
ShapedClock::ShapedClock(QWidget *parent)
: QWidget(parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint)

View File

@ -50,7 +50,10 @@
#include "slidersgroup.h"
#include <QtWidgets>
#include <QBoxLayout>
#include <QDial>
#include <QScrollBar>
#include <QSlider>
//! [0]
SlidersGroup::SlidersGroup(Qt::Orientation orientation, const QString &title,

View File

@ -48,11 +48,16 @@
**
****************************************************************************/
#include <QtWidgets>
#include "slidersgroup.h"
#include "window.h"
#include <QCheckBox>
#include <QComboBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
#include <QStackedWidget>
//! [0]
Window::Window(QWidget *parent)
: QWidget(parent)

View File

@ -48,10 +48,16 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include <QCheckBox>
#include <QComboBox>
#include <QDateTimeEdit>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QSpinBox>
//! [0]
Window::Window(QWidget *parent)
: QWidget(parent)

View File

@ -50,7 +50,10 @@
#include "norwegianwoodstyle.h"
#include <QtWidgets>
#include <QComboBox>
#include <QPainter>
#include <QPushButton>
#include <QStyleFactory>
NorwegianWoodStyle::NorwegianWoodStyle() :
QProxyStyle(QStyleFactory::create("windows"))

View File

@ -51,7 +51,24 @@
#include "widgetgallery.h"
#include "norwegianwoodstyle.h"
#include <QtWidgets>
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDateTimeEdit>
#include <QDial>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QProgressBar>
#include <QPushButton>
#include <QRadioButton>
#include <QScrollBar>
#include <QSpinBox>
#include <QStyleFactory>
#include <QTableWidget>
#include <QTextEdit>
#include <QTimer>
//! [0]
WidgetGallery::WidgetGallery(QWidget *parent)

View File

@ -48,7 +48,7 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QApplication>
#include "mainwindow.h"

View File

@ -48,11 +48,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h"
#include "stylesheeteditor.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{

View File

@ -51,7 +51,7 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets>
#include <QMainWindow>
#include "ui_mainwindow.h"

View File

@ -50,7 +50,9 @@
#include "stylesheeteditor.h"
#include <QtWidgets>
#include <QFile>
#include <QRegularExpression>
#include <QStyleFactory>
StyleSheetEditor::StyleSheetEditor(QWidget *parent)
: QDialog(parent)

View File

@ -48,8 +48,6 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h"
#include "tabletapplication.h"
#include "tabletcanvas.h"

View File

@ -48,14 +48,19 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h"
#include "tabletcanvas.h"
#include <QApplication>
#include <QColorDialog>
#include <QDir>
#include <QFileDialog>
#include <QMenuBar>
#include <QMessageBox>
//! [0]
MainWindow::MainWindow(TabletCanvas *canvas)
: m_canvas(canvas), m_colorDialog(nullptr)
: m_canvas(canvas)
{
createMenus();
setWindowTitle(tr("Tablet Example"));

View File

@ -81,7 +81,7 @@ private:
void createMenus();
TabletCanvas *m_canvas;
QColorDialog *m_colorDialog;
QColorDialog *m_colorDialog = nullptr;
};
//! [0]

View File

@ -50,8 +50,6 @@
#include "tabletapplication.h"
#include <QtWidgets>
//! [0]
bool TabletApplication::event(QEvent *event)
{

View File

@ -48,21 +48,16 @@
**
****************************************************************************/
#include <QtWidgets>
#include <math.h>
#include "tabletcanvas.h"
#include <QCoreApplication>
#include <QPainter>
#include <QtMath>
//! [0]
TabletCanvas::TabletCanvas()
: QWidget(nullptr)
, m_alphaChannelValuator(TangentialPressureValuator)
, m_colorSaturationValuator(NoValuator)
, m_lineWidthValuator(PressureValuator)
, m_color(Qt::red)
, m_brush(m_color)
, m_pen(m_brush, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)
, m_deviceDown(false)
: QWidget(nullptr), m_brush(m_color)
, m_pen(m_brush, 1.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)
{
resize(500, 500);
setAutoFillBackground(true);
@ -138,7 +133,7 @@ void TabletCanvas::tabletEvent(QTabletEvent *event)
void TabletCanvas::initPixmap()
{
qreal dpr = devicePixelRatioF();
QPixmap newPixmap = QPixmap(width() * dpr, height() * dpr);
QPixmap newPixmap = QPixmap(qRound(width() * dpr), qRound(height() * dpr));
newPixmap.setDevicePixelRatio(dpr);
newPixmap.fill(Qt::white);
QPainter painter(&newPixmap);
@ -208,7 +203,7 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event)
const QString error(tr("This input device is not supported by the example."));
#if QT_CONFIG(statustip)
QStatusTipEvent status(error);
QApplication::sendEvent(this, &status);
QCoreApplication::sendEvent(this, &status);
#else
qWarning() << error;
#endif
@ -219,7 +214,7 @@ void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event)
const QString error(tr("Unknown tablet device - treating as stylus"));
#if QT_CONFIG(statustip)
QStatusTipEvent status(error);
QApplication::sendEvent(this, &status);
QCoreApplication::sendEvent(this, &status);
#else
qWarning() << error;
#endif
@ -261,7 +256,8 @@ void TabletCanvas::updateBrush(const QTabletEvent *event)
m_color.setAlpha(255);
break;
case TiltValuator:
m_color.setAlpha(maximum(abs(vValue - 127), abs(hValue - 127)));
m_color.setAlpha(std::max(std::abs(vValue - 127),
std::abs(hValue - 127)));
break;
default:
m_color.setAlpha(255);
@ -288,7 +284,8 @@ void TabletCanvas::updateBrush(const QTabletEvent *event)
m_pen.setWidthF(pressureToWidth(event->pressure()));
break;
case TiltValuator:
m_pen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12);
m_pen.setWidthF(std::max(std::abs(vValue - 127),
std::abs(hValue - 127)) / 12);
break;
default:
m_pen.setWidthF(1);

View File

@ -51,14 +51,13 @@
#ifndef TABLETCANVAS_H
#define TABLETCANVAS_H
#include <QWidget>
#include <QBrush>
#include <QColor>
#include <QPen>
#include <QPixmap>
#include <QPoint>
#include <QTabletEvent>
#include <QColor>
#include <QBrush>
#include <QPen>
#include <QPoint>
#include <QWidget>
QT_BEGIN_NAMESPACE
class QPaintEvent;
@ -92,8 +91,6 @@ public:
{ return m_color; }
void setTabletDevice(QTabletEvent *event)
{ updateCursor(event); }
int maximum(int a, int b)
{ return a > b ? a : b; }
protected:
void tabletEvent(QTabletEvent *event) override;
@ -108,19 +105,19 @@ private:
void updateBrush(const QTabletEvent *event);
void updateCursor(const QTabletEvent *event);
Valuator m_alphaChannelValuator;
Valuator m_colorSaturationValuator;
Valuator m_lineWidthValuator;
QColor m_color;
Valuator m_alphaChannelValuator = TangentialPressureValuator;
Valuator m_colorSaturationValuator = NoValuator;
Valuator m_lineWidthValuator = PressureValuator;
QColor m_color = Qt::red;
QPixmap m_pixmap;
QBrush m_brush;
QPen m_pen;
bool m_deviceDown;
bool m_deviceDown = false;
struct Point {
QPointF pos;
qreal pressure;
qreal rotation;
qreal pressure = 0;
qreal rotation = 0;
} lastPoint;
};
//! [0]

View File

@ -50,16 +50,16 @@
#include "tetrixboard.h"
#include <QtWidgets>
#include <QKeyEvent>
#include <QLabel>
#include <QPainter>
//! [0]
TetrixBoard::TetrixBoard(QWidget *parent)
: QFrame(parent)
: QFrame(parent), isStarted(false), isPaused(false)
{
setFrameStyle(QFrame::Panel | QFrame::Sunken);
setFocusPolicy(Qt::StrongFocus);
isStarted = false;
isPaused = false;
clearBoard();
nextPiece.setRandomShape();
@ -396,7 +396,7 @@ bool TetrixBoard::tryMove(const TetrixPiece &newPiece, int newX, int newY)
//! [36]
void TetrixBoard::drawSquare(QPainter &painter, int x, int y, TetrixShape shape)
{
static const QRgb colorTable[8] = {
static constexpr QRgb colorTable[8] = {
0x000000, 0xCC6666, 0x66CC66, 0x6666CC,
0xCCCC66, 0xCC66CC, 0x66CCCC, 0xDAAA00
};

View File

@ -62,7 +62,7 @@ void TetrixPiece::setRandomShape()
//! [1]
void TetrixPiece::setShape(TetrixShape shape)
{
static const int coordsTable[8][4][2] = {
static constexpr int coordsTable[8][4][2] = {
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } },
{ { 0, -1 }, { 0, 0 }, { -1, 0 }, { -1, 1 } },
{ { 0, -1 }, { 0, 0 }, { 1, 0 }, { 1, 1 } },

View File

@ -48,23 +48,24 @@
**
****************************************************************************/
#include <QtWidgets>
#include "tetrixboard.h"
#include "tetrixwindow.h"
#include <QCoreApplication>
#include <QGridLayout>
#include <QLabel>
#include <QLCDNumber>
#include <QPushButton>
//! [0]
TetrixWindow::TetrixWindow(QWidget *parent)
: QWidget(parent)
: QWidget(parent), board(new TetrixBoard)
{
board = new TetrixBoard;
//! [0]
nextPieceLabel = new QLabel;
nextPieceLabel->setFrameStyle(QFrame::Box | QFrame::Raised);
nextPieceLabel->setAlignment(Qt::AlignCenter);
board->setNextPieceLabel(nextPieceLabel);
//! [1]
scoreLcd = new QLCDNumber(5);
scoreLcd->setSegmentStyle(QLCDNumber::Filled);
@ -86,7 +87,7 @@ TetrixWindow::TetrixWindow(QWidget *parent)
connect(startButton, &QPushButton::clicked, board, &TetrixBoard::start);
//! [4] //! [5]
connect(quitButton , &QPushButton::clicked, qApp, &QApplication::quit);
connect(quitButton , &QPushButton::clicked, qApp, &QCoreApplication::quit);
connect(pauseButton, &QPushButton::clicked, board, &TetrixBoard::pause);
#if __cplusplus >= 201402L
connect(board, &TetrixBoard::scoreChanged,

View File

@ -48,7 +48,7 @@
**
****************************************************************************/
#include <QtWidgets>
#include <QApplication>
#include "sortingbox.h"

View File

@ -50,7 +50,13 @@
#include "sortingbox.h"
#include <QtWidgets>
#include <QMouseEvent>
#include <QIcon>
#include <QPainter>
#include <QRandomGenerator>
#include <QStyle>
#include <QToolButton>
#include <QToolTip>
//! [0]
SortingBox::SortingBox(QWidget *parent)
@ -277,12 +283,12 @@ QToolButton *SortingBox::createToolButton(const QString &toolTip,
QPoint SortingBox::initialItemPosition(const QPainterPath &path)
{
int x;
int y = (height() - (int)path.controlPointRect().height()) / 2;
int y = (height() - qRound(path.controlPointRect().height()) / 2);
if (shapeItems.size() == 0)
x = ((3 * width()) / 2 - (int)path.controlPointRect().width()) / 2;
x = ((3 * width()) / 2 - qRound(path.controlPointRect().width())) / 2;
else
x = (width() / shapeItems.size()
- (int)path.controlPointRect().width()) / 2;
- qRound(path.controlPointRect().width())) / 2;
return QPoint(x, y);
}

View File

@ -99,7 +99,7 @@ private:
const char *member);
//! [2]
QList<ShapeItem> shapeItems;
QVector<ShapeItem> shapeItems;
QPainterPath circlePath;
QPainterPath squarePath;
QPainterPath trianglePath;

View File

@ -50,7 +50,7 @@
#include "validatorwidget.h"
#include <QtWidgets>
#include <QIntValidator>
ValidatorWidget::ValidatorWidget(QWidget *parent)
: QWidget(parent)

View File

@ -50,11 +50,13 @@
#include "wigglywidget.h"
#include <QtWidgets>
#include <QFontMetrics>
#include <QPainter>
#include <QTimerEvent>
//! [0]
WigglyWidget::WigglyWidget(QWidget *parent)
: QWidget(parent)
: QWidget(parent), step(0)
{
setBackgroundRole(QPalette::Midlight);
setAutoFillBackground(true);
@ -63,7 +65,6 @@ WigglyWidget::WigglyWidget(QWidget *parent)
newFont.setPointSize(newFont.pointSize() + 20);
setFont(newFont);
step = 0;
timer.start(60, this);
}
//! [0]
@ -72,7 +73,7 @@ WigglyWidget::WigglyWidget(QWidget *parent)
void WigglyWidget::paintEvent(QPaintEvent * /* event */)
//! [1] //! [2]
{
static const int sineTable[16] = {
static constexpr int sineTable[16] = {
0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38
};

View File

@ -48,10 +48,15 @@
**
****************************************************************************/
#include <QtWidgets>
#include "controllerwindow.h"
#include <QCheckBox>
#include <QCoreApplication>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
//! [0]
ControllerWindow::ControllerWindow(QWidget *parent)
: QWidget(parent)
@ -63,7 +68,7 @@ ControllerWindow::ControllerWindow(QWidget *parent)
quitButton = new QPushButton(tr("&Quit"));
connect(quitButton, &QPushButton::clicked,
qApp, &QApplication::quit);
qApp, &QCoreApplication::quit);
QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addStretch();
@ -83,26 +88,25 @@ ControllerWindow::ControllerWindow(QWidget *parent)
//! [1]
void ControllerWindow::updatePreview()
{
Qt::WindowFlags flags = 0;
Qt::WindowFlags flags;
if (windowRadioButton->isChecked()) {
if (windowRadioButton->isChecked())
flags = Qt::Window;
} else if (dialogRadioButton->isChecked()) {
else if (dialogRadioButton->isChecked())
flags = Qt::Dialog;
} else if (sheetRadioButton->isChecked()) {
else if (sheetRadioButton->isChecked())
flags = Qt::Sheet;
} else if (drawerRadioButton->isChecked()) {
else if (drawerRadioButton->isChecked())
flags = Qt::Drawer;
} else if (popupRadioButton->isChecked()) {
else if (popupRadioButton->isChecked())
flags = Qt::Popup;
} else if (toolRadioButton->isChecked()) {
else if (toolRadioButton->isChecked())
flags = Qt::Tool;
} else if (toolTipRadioButton->isChecked()) {
else if (toolTipRadioButton->isChecked())
flags = Qt::ToolTip;
} else if (splashScreenRadioButton->isChecked()) {
else if (splashScreenRadioButton->isChecked())
flags = Qt::SplashScreen;
//! [1] //! [2]
}
//! [2] //! [3]
if (msWindowsFixedSizeDialogCheckBox->isChecked())

View File

@ -50,7 +50,9 @@
#include "previewwindow.h"
#include <QtWidgets>
#include <QPushButton>
#include <QTextEdit>
#include <QVBoxLayout>
//! [0]
PreviewWindow::PreviewWindow(QWidget *parent)
@ -81,23 +83,22 @@ void PreviewWindow::setWindowFlags(Qt::WindowFlags flags)
QString text;
Qt::WindowFlags type = (flags & Qt::WindowType_Mask);
if (type == Qt::Window) {
if (type == Qt::Window)
text = "Qt::Window";
} else if (type == Qt::Dialog) {
else if (type == Qt::Dialog)
text = "Qt::Dialog";
} else if (type == Qt::Sheet) {
else if (type == Qt::Sheet)
text = "Qt::Sheet";
} else if (type == Qt::Drawer) {
else if (type == Qt::Drawer)
text = "Qt::Drawer";
} else if (type == Qt::Popup) {
else if (type == Qt::Popup)
text = "Qt::Popup";
} else if (type == Qt::Tool) {
else if (type == Qt::Tool)
text = "Qt::Tool";
} else if (type == Qt::ToolTip) {
else if (type == Qt::ToolTip)
text = "Qt::ToolTip";
} else if (type == Qt::SplashScreen) {
else if (type == Qt::SplashScreen)
text = "Qt::SplashScreen";
}
if (flags & Qt::MSWindowsFixedSizeDialogHint)
text += "\n| Qt::MSWindowsFixedSizeDialogHint";