From 6f4bc3942dda076eedf38d8c8604eb4fa5d7bd3b Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 1 Sep 2019 17:12:01 +0200 Subject: Widgets/GraphicsView examples: cleanup Cleanup GraphicsView examples with the help of clang-tidy - modernize-use-nullptr - modernize-use-default-member-init - modernize-use-override.IgnoreDestructors - Some QList -> QVector changes - use nullptr - use normalized includes, remove unused includes - fix style Change-Id: I79347e55bfde52f6ae7749cc7093fbd442044020 Reviewed-by: Friedemann Kleint --- .../widgets/graphicsview/flowlayout/flowlayout.cpp | 19 +++++++------------ examples/widgets/graphicsview/flowlayout/flowlayout.h | 6 +++--- examples/widgets/graphicsview/flowlayout/main.cpp | 6 +++--- examples/widgets/graphicsview/flowlayout/window.cpp | 16 +++++++--------- examples/widgets/graphicsview/flowlayout/window.h | 3 +-- 5 files changed, 21 insertions(+), 29 deletions(-) (limited to 'examples/widgets/graphicsview/flowlayout') diff --git a/examples/widgets/graphicsview/flowlayout/flowlayout.cpp b/examples/widgets/graphicsview/flowlayout/flowlayout.cpp index ab6bbb7aa4..54914b3746 100644 --- a/examples/widgets/graphicsview/flowlayout/flowlayout.cpp +++ b/examples/widgets/graphicsview/flowlayout/flowlayout.cpp @@ -50,14 +50,10 @@ #include "flowlayout.h" -#include +#include -#include - -FlowLayout::FlowLayout() +FlowLayout::FlowLayout(QGraphicsLayoutItem *parent) : QGraphicsLayout(parent) { - m_spacing[0] = 6; - m_spacing[1] = 6; QSizePolicy sp = sizePolicy(); sp.setHeightForWidth(true); setSizePolicy(sp); @@ -66,7 +62,7 @@ FlowLayout::FlowLayout() void FlowLayout::insertItem(int index, QGraphicsLayoutItem *item) { item->setParentLayoutItem(this); - if (uint(index) > uint(m_items.count())) + if (index > m_items.count()) index = m_items.count(); m_items.insert(index, item); invalidate(); @@ -117,15 +113,14 @@ qreal FlowLayout::doLayout(const QRectF &geom, bool applyNewGeometry) const qreal y = 0; qreal maxRowHeight = 0; QSizeF pref; - for (int i = 0; i < m_items.count(); ++i) { - QGraphicsLayoutItem *item = m_items.at(i); + for (QGraphicsLayoutItem *item : m_items) { pref = item->effectiveSizeHint(Qt::PreferredSize); maxRowHeight = qMax(maxRowHeight, pref.height()); qreal next_x; next_x = x + pref.width(); if (next_x > maxw) { - if (x == 0) { + if (qFuzzyIsNull(x)) { pref.setWidth(maxw); } else { x = 0; @@ -156,7 +151,7 @@ QSizeF FlowLayout::minSize(const QSizeF &constraint) const } else { for (const QGraphicsLayoutItem *item : qAsConst(m_items)) size = size.expandedTo(item->effectiveSizeHint(Qt::MinimumSize)); - size += QSize(left + right, top + bottom); + size += QSizeF(left + right, top + bottom); } return size; } @@ -164,7 +159,7 @@ QSizeF FlowLayout::minSize(const QSizeF &constraint) const QSizeF FlowLayout::prefSize() const { qreal left, right; - getContentsMargins(&left, 0, &right, 0); + getContentsMargins(&left, nullptr, &right, nullptr); qreal maxh = 0; qreal totalWidth = 0; diff --git a/examples/widgets/graphicsview/flowlayout/flowlayout.h b/examples/widgets/graphicsview/flowlayout/flowlayout.h index 808f5d2c77..c6758414d6 100644 --- a/examples/widgets/graphicsview/flowlayout/flowlayout.h +++ b/examples/widgets/graphicsview/flowlayout/flowlayout.h @@ -53,7 +53,7 @@ class FlowLayout : public QGraphicsLayout { public: - FlowLayout(); + FlowLayout(QGraphicsLayoutItem *parent = nullptr); inline void addItem(QGraphicsLayoutItem *item); void insertItem(int index, QGraphicsLayoutItem *item); void setSpacing(Qt::Orientations o, qreal spacing); @@ -75,8 +75,8 @@ private: QSizeF prefSize() const; QSizeF maxSize() const; - QList m_items; - qreal m_spacing[2]; + QVector m_items; + qreal m_spacing[2] = {6, 6}; }; diff --git a/examples/widgets/graphicsview/flowlayout/main.cpp b/examples/widgets/graphicsview/flowlayout/main.cpp index 74c03b9bce..dbfed51bb3 100644 --- a/examples/widgets/graphicsview/flowlayout/main.cpp +++ b/examples/widgets/graphicsview/flowlayout/main.cpp @@ -59,12 +59,12 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); QGraphicsScene scene; - QGraphicsView *view = new QGraphicsView(&scene); + QGraphicsView view(&scene); Window *w = new Window; scene.addItem(w); - view->resize(400, 300); - view->show(); + view.resize(400, 300); + view.show(); return app.exec(); } diff --git a/examples/widgets/graphicsview/flowlayout/window.cpp b/examples/widgets/graphicsview/flowlayout/window.cpp index 34d0d71b44..8fe06d0e11 100644 --- a/examples/widgets/graphicsview/flowlayout/window.cpp +++ b/examples/widgets/graphicsview/flowlayout/window.cpp @@ -48,23 +48,21 @@ ** ****************************************************************************/ -#include "flowlayout.h" #include "window.h" +#include "flowlayout.h" #include #include -Window::Window() -: QGraphicsWidget(0, Qt::Window) +Window::Window(QGraphicsItem *parent) : QGraphicsWidget(parent, Qt::Window) { FlowLayout *lay = new FlowLayout; - QLatin1String wiseWords("I am not bothered by the fact that I am unknown." - " I am bothered when I do not know others. (Confucius)"); - QString sentence(wiseWords); - QStringList words = sentence.split(QLatin1Char(' '), QString::SkipEmptyParts); - for (int i = 0; i < words.count(); ++i) { + const QString sentence(QLatin1String("I am not bothered by the fact that I am unknown." + " I am bothered when I do not know others. (Confucius)")); + const QVector words = sentence.splitRef(QLatin1Char(' '), QString::SkipEmptyParts); + for (const QStringRef &word : words) { QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget(this); - QLabel *label = new QLabel(words.at(i)); + QLabel *label = new QLabel(word.toString()); label->setFrameStyle(QFrame::Box | QFrame::Plain); proxy->setWidget(label); lay->addItem(proxy); diff --git a/examples/widgets/graphicsview/flowlayout/window.h b/examples/widgets/graphicsview/flowlayout/window.h index 7f37c447d1..24a7cf908b 100644 --- a/examples/widgets/graphicsview/flowlayout/window.h +++ b/examples/widgets/graphicsview/flowlayout/window.h @@ -53,7 +53,6 @@ class Window : public QGraphicsWidget { Q_OBJECT - public: - Window(); + Window(QGraphicsItem *parent = nullptr); }; -- cgit v1.2.3