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 pendingAdditiveOperator and \c pendingMultiplicativeOperator
variables don't need to be initialized explicitly, because the variables don't need to be initialized explicitly, because the
QString constructor initializes them to empty strings. 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 1
\snippet widgets/calculator/calculator.cpp 2 \snippet widgets/calculator/calculator.cpp 2

View File

@ -95,7 +95,7 @@
\snippet widgets/tooltips/sortingbox.h 2 \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 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 a triangle. We also need to have a pointer to an item when it is
moving, and we need to know its previous position. moving, and we need to know its previous position.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -50,11 +50,14 @@
#include "characterwidget.h" #include "characterwidget.h"
#include <QtWidgets> #include <QFontDatabase>
#include <QMouseEvent>
#include <QPainter>
#include <QToolTip>
//! [0] //! [0]
CharacterWidget::CharacterWidget(QWidget *parent) CharacterWidget::CharacterWidget(QWidget *parent)
: QWidget(parent), columns(16), lastKey(-1) : QWidget(parent)
{ {
calculateSquareSize(); calculateSquareSize();
setMouseTracking(true); setMouseTracking(true);
@ -172,16 +175,16 @@ void CharacterWidget::paintEvent(QPaintEvent *event)
QFontMetrics fontMetrics(displayFont); QFontMetrics fontMetrics(displayFont);
painter.setPen(QPen(Qt::black)); painter.setPen(QPen(Qt::black));
for (int row = beginRow; row <= endRow; ++row) { for (int row = beginRow; row <= endRow; ++row) {
for (int column = beginColumn; column <= endColumn; ++column) { for (int column = beginColumn; column <= endColumn; ++column) {
int key = row * columns + column; int key = row * columns + column;
painter.setClipRect(column * squareSize, row * squareSize, squareSize, squareSize); painter.setClipRect(column * squareSize, row * squareSize, squareSize, squareSize);
if (key == lastKey) 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, painter.drawText(column * squareSize + (squareSize / 2) -
fontMetrics.horizontalAdvance(QChar(key)) / 2,
row * squareSize + 4 + fontMetrics.ascent(), row * squareSize + 4 + fontMetrics.ascent(),
QString(QChar(key))); QString(QChar(key)));
} }

View File

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

View File

@ -48,13 +48,27 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "characterwidget.h"
#include "mainwindow.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] //! [0]
Q_DECLARE_METATYPE(QFontComboBox::FontFilter) Q_DECLARE_METATYPE(QFontComboBox::FontFilter)
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -48,14 +48,29 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets> #include "mainwindow.h"
#include "iconpreviewarea.h" #include "iconpreviewarea.h"
#include "iconsizespinbox.h" #include "iconsizespinbox.h"
#include "imagedelegate.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] //! [40]
enum { OtherSize = QStyle::PM_CustomBase }; enum { OtherSize = QStyle::PM_CustomBase };

View File

@ -50,7 +50,24 @@
#include "imageviewer.h" #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) #if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h> #include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printdialog) #if QT_CONFIG(printdialog)
@ -60,8 +77,8 @@
//! [0] //! [0]
ImageViewer::ImageViewer(QWidget *parent) ImageViewer::ImageViewer(QWidget *parent)
: QMainWindow(parent), imageLabel(new QLabel), : QMainWindow(parent), imageLabel(new QLabel)
scrollArea(new QScrollArea), scaleFactor(1) , scrollArea(new QScrollArea)
{ {
imageLabel->setBackgroundRole(QPalette::Base); imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

View File

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

View File

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

View File

@ -50,7 +50,15 @@
#include "movieplayer.h" #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) MoviePlayer::MoviePlayer(QWidget *parent)
: QWidget(parent) : QWidget(parent)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -51,7 +51,24 @@
#include "widgetgallery.h" #include "widgetgallery.h"
#include "norwegianwoodstyle.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] //! [0]
WidgetGallery::WidgetGallery(QWidget *parent) WidgetGallery::WidgetGallery(QWidget *parent)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -50,11 +50,13 @@
#include "wigglywidget.h" #include "wigglywidget.h"
#include <QtWidgets> #include <QFontMetrics>
#include <QPainter>
#include <QTimerEvent>
//! [0] //! [0]
WigglyWidget::WigglyWidget(QWidget *parent) WigglyWidget::WigglyWidget(QWidget *parent)
: QWidget(parent) : QWidget(parent), step(0)
{ {
setBackgroundRole(QPalette::Midlight); setBackgroundRole(QPalette::Midlight);
setAutoFillBackground(true); setAutoFillBackground(true);
@ -63,7 +65,6 @@ WigglyWidget::WigglyWidget(QWidget *parent)
newFont.setPointSize(newFont.pointSize() + 20); newFont.setPointSize(newFont.pointSize() + 20);
setFont(newFont); setFont(newFont);
step = 0;
timer.start(60, this); timer.start(60, this);
} }
//! [0] //! [0]
@ -72,7 +73,7 @@ WigglyWidget::WigglyWidget(QWidget *parent)
void WigglyWidget::paintEvent(QPaintEvent * /* event */) void WigglyWidget::paintEvent(QPaintEvent * /* event */)
//! [1] //! [2] //! [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 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 "controllerwindow.h"
#include <QCheckBox>
#include <QCoreApplication>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
//! [0] //! [0]
ControllerWindow::ControllerWindow(QWidget *parent) ControllerWindow::ControllerWindow(QWidget *parent)
: QWidget(parent) : QWidget(parent)
@ -63,7 +68,7 @@ ControllerWindow::ControllerWindow(QWidget *parent)
quitButton = new QPushButton(tr("&Quit")); quitButton = new QPushButton(tr("&Quit"));
connect(quitButton, &QPushButton::clicked, connect(quitButton, &QPushButton::clicked,
qApp, &QApplication::quit); qApp, &QCoreApplication::quit);
QHBoxLayout *bottomLayout = new QHBoxLayout; QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addStretch(); bottomLayout->addStretch();
@ -83,26 +88,25 @@ ControllerWindow::ControllerWindow(QWidget *parent)
//! [1] //! [1]
void ControllerWindow::updatePreview() void ControllerWindow::updatePreview()
{ {
Qt::WindowFlags flags = 0; Qt::WindowFlags flags;
if (windowRadioButton->isChecked()) { if (windowRadioButton->isChecked())
flags = Qt::Window; flags = Qt::Window;
} else if (dialogRadioButton->isChecked()) { else if (dialogRadioButton->isChecked())
flags = Qt::Dialog; flags = Qt::Dialog;
} else if (sheetRadioButton->isChecked()) { else if (sheetRadioButton->isChecked())
flags = Qt::Sheet; flags = Qt::Sheet;
} else if (drawerRadioButton->isChecked()) { else if (drawerRadioButton->isChecked())
flags = Qt::Drawer; flags = Qt::Drawer;
} else if (popupRadioButton->isChecked()) { else if (popupRadioButton->isChecked())
flags = Qt::Popup; flags = Qt::Popup;
} else if (toolRadioButton->isChecked()) { else if (toolRadioButton->isChecked())
flags = Qt::Tool; flags = Qt::Tool;
} else if (toolTipRadioButton->isChecked()) { else if (toolTipRadioButton->isChecked())
flags = Qt::ToolTip; flags = Qt::ToolTip;
} else if (splashScreenRadioButton->isChecked()) { else if (splashScreenRadioButton->isChecked())
flags = Qt::SplashScreen; flags = Qt::SplashScreen;
//! [1] //! [2] //! [1] //! [2]
}
//! [2] //! [3] //! [2] //! [3]
if (msWindowsFixedSizeDialogCheckBox->isChecked()) if (msWindowsFixedSizeDialogCheckBox->isChecked())

View File

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