Cleanup Itemviews examples

Cleanup the Itemviews examples
 - use nullptr instead 0
 - use for loop instead foreach
 - include own header first
 - remove uselss includes

Change-Id: I32e9f64356e42038707d063dcad977239ce1fe9e
Reviewed-by: Luca Beldi <v.ronin@yahoo.it>
Reviewed-by: Sze Howe Koh <szehowe.koh@gmail.com>
This commit is contained in:
Christian Ehrlicher 2018-11-24 17:29:15 +01:00
parent b8720e3e7a
commit af84f12298
24 changed files with 79 additions and 92 deletions

View File

@ -48,10 +48,10 @@
**
****************************************************************************/
#include <QtWidgets>
#include "colorlisteditor.h"
#include <QtWidgets>
ColorListEditor::ColorListEditor(QWidget *widget) : QComboBox(widget)
{
populateList();
@ -65,16 +65,16 @@ QColor ColorListEditor::color() const
//! [0]
//! [1]
void ColorListEditor::setColor(QColor color)
void ColorListEditor::setColor(const QColor &color)
{
setCurrentIndex(findData(color, int(Qt::DecorationRole)));
setCurrentIndex(findData(color, Qt::DecorationRole));
}
//! [1]
//! [2]
void ColorListEditor::populateList()
{
QStringList colorNames = QColor::colorNames();
const QStringList colorNames = QColor::colorNames();
for (int i = 0; i < colorNames.size(); ++i) {
QColor color(colorNames[i]);

View File

@ -65,11 +65,11 @@ class ColorListEditor : public QComboBox
Q_PROPERTY(QColor color READ color WRITE setColor USER true)
public:
ColorListEditor(QWidget *widget = 0);
ColorListEditor(QWidget *widget = nullptr);
public:
QColor color() const;
void setColor(QColor c);
void setColor(const QColor &color);
private:
void populateList();

View File

@ -48,11 +48,11 @@
**
****************************************************************************/
#include <QtWidgets>
#include "window.h"
#include "colorlisteditor.h"
#include <QtWidgets>
//! [0]
Window::Window()
{
@ -71,19 +71,18 @@ Window::Window()
void Window::createGUI()
{
QList<QPair<QString, QColor> > list;
list << QPair<QString, QColor>(tr("Alice"), QColor("aliceblue")) <<
QPair<QString, QColor>(tr("Neptun"), QColor("aquamarine")) <<
QPair<QString, QColor>(tr("Ferdinand"), QColor("springgreen"));
const QVector<QPair<QString, QColor> > list =
{{ tr("Alice"), QColor("aliceblue") },
{ tr("Neptun"), QColor("aquamarine") },
{ tr("Ferdinand"), QColor("springgreen") }};
QTableWidget *table = new QTableWidget(3, 2);
table->setHorizontalHeaderLabels(QStringList() << tr("Name")
<< tr("Hair Color"));
table->setHorizontalHeaderLabels({ tr("Name"), tr("Hair Color") });
table->verticalHeader()->setVisible(false);
table->resize(150, 50);
for (int i = 0; i < 3; ++i) {
QPair<QString, QColor> pair = list.at(i);
const QPair<QString, QColor> &pair = list.at(i);
QTableWidgetItem *nameItem = new QTableWidgetItem(pair.first);
QTableWidgetItem *colorItem = new QTableWidgetItem;

View File

@ -52,13 +52,11 @@
#include <QIcon>
#include <QPixmap>
#include <QImage>
#include <QMenu>
#include <QAction>
#include <QActionGroup>
#include <QToolButton>
#include <QWidgetAction>
#include <QDebug>
FilterWidget::FilterWidget(QWidget *parent)
: QLineEdit(parent)
@ -127,7 +125,8 @@ QRegExp::PatternSyntax FilterWidget::patternSyntax() const
void FilterWidget::setPatternSyntax(QRegExp::PatternSyntax s)
{
foreach (QAction *a, m_patternGroup->actions()) {
const QList<QAction*> actions = m_patternGroup->actions();
for (QAction *a : actions) {
if (patternSyntaxFromAction(a) == s) {
a->setChecked(true);
break;

View File

@ -67,7 +67,7 @@ class FilterWidget : public QLineEdit
Q_PROPERTY(Qt::CaseSensitivity caseSensitivity READ caseSensitivity WRITE setCaseSensitivity)
Q_PROPERTY(QRegExp::PatternSyntax patternSyntax READ patternSyntax WRITE setPatternSyntax)
public:
explicit FilterWidget(QWidget *parent = 0);
explicit FilterWidget(QWidget *parent = nullptr);
Qt::CaseSensitivity caseSensitivity() const;
void setCaseSensitivity(Qt::CaseSensitivity);

View File

@ -48,10 +48,10 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mysortfilterproxymodel.h"
#include <QtWidgets>
//! [0]
MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
@ -77,7 +77,7 @@ void MySortFilterProxyModel::setFilterMaximumDate(const QDate &date)
//! [3]
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
const QModelIndex &sourceParent) const
const QModelIndex &sourceParent) const
{
QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);

View File

@ -48,12 +48,12 @@
**
****************************************************************************/
#include <QtWidgets>
#include "mysortfilterproxymodel.h"
#include "window.h"
#include "mysortfilterproxymodel.h"
#include "filterwidget.h"
#include <QtWidgets>
//! [0]
Window::Window()
{
@ -75,7 +75,7 @@ Window::Window()
//! [3]
filterWidget = new FilterWidget;
filterWidget->setText("Grace|Sports");
filterWidget->setText(tr("Grace|Sports"));
connect(filterWidget, &FilterWidget::filterChanged, this, &Window::textFilterChanged);
filterPatternLabel = new QLabel(tr("&Filter pattern:"));
@ -137,6 +137,11 @@ void Window::setSourceModel(QAbstractItemModel *model)
{
proxyModel->setSourceModel(model);
sourceView->setModel(model);
for (int i = 0; i < proxyModel->columnCount(); ++i)
proxyView->resizeColumnToContents(i);
for (int i = 0; i < model->columnCount(); ++i)
sourceView->resizeColumnToContents(i);
}
//! [7]

View File

@ -50,20 +50,18 @@
#include "filelistmodel.h"
#include <QApplication>
#include <QBrush>
#include <QGuiApplication>
#include <QDir>
#include <QPalette>
FileListModel::FileListModel(QObject *parent)
: QAbstractListModel(parent)
{
}
: QAbstractListModel(parent), fileCount(0)
{}
//![4]
int FileListModel::rowCount(const QModelIndex & /* parent */) const
int FileListModel::rowCount(const QModelIndex &parent) const
{
return fileCount;
return parent.isValid() ? 0 : fileCount;
}
QVariant FileListModel::data(const QModelIndex &index, int role) const
@ -88,25 +86,26 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
//![4]
//![1]
bool FileListModel::canFetchMore(const QModelIndex & /* index */) const
bool FileListModel::canFetchMore(const QModelIndex &parent) const
{
if (fileCount < fileList.size())
return true;
else
if (parent.isValid())
return false;
return (fileCount < fileList.size());
}
//![1]
//![2]
void FileListModel::fetchMore(const QModelIndex & /* index */)
void FileListModel::fetchMore(const QModelIndex &parent)
{
if (parent.isValid())
return;
int remainder = fileList.size() - fileCount;
int itemsToFetch = qMin(100, remainder);
if (itemsToFetch <= 0)
return;
beginInsertRows(QModelIndex(), fileCount, fileCount+itemsToFetch-1);
beginInsertRows(QModelIndex(), fileCount, fileCount + itemsToFetch - 1);
fileCount += itemsToFetch;

View File

@ -52,7 +52,6 @@
#define FILELISTMODEL_H
#include <QAbstractListModel>
#include <QList>
#include <QStringList>
//![0]
@ -61,7 +60,7 @@ class FileListModel : public QAbstractListModel
Q_OBJECT
public:
FileListModel(QObject *parent = 0);
FileListModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

View File

@ -48,8 +48,8 @@
**
****************************************************************************/
#include "filelistmodel.h"
#include "window.h"
#include "filelistmodel.h"
#include <QtWidgets>
@ -66,7 +66,7 @@ Window::Window(QWidget *parent)
QListView *view = new QListView;
view->setModel(model);
logViewer = new QTextBrowser;
logViewer = new QTextBrowser(this);
logViewer->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
connect(lineEdit, &QLineEdit::textChanged,

View File

@ -62,7 +62,7 @@ class Window : public QWidget
Q_OBJECT
public:
Window(QWidget *parent = 0);
Window(QWidget *parent = nullptr);
public slots:
void updateLog(int number);

View File

@ -50,14 +50,13 @@
#include "model.h"
#include <QIcon>
#include <QPixmap>
Model::Model(int rows, int columns, QObject *parent)
: QAbstractItemModel(parent),
services(QPixmap(":/images/services.png")),
rc(rows), cc(columns),
tree(new QVector<Node>(rows, Node(0)))
tree(new QVector<Node>(rows, Node()))
{
}

View File

@ -61,7 +61,7 @@ class Model : public QAbstractItemModel
Q_OBJECT
public:
Model(int rows, int columns, QObject *parent = 0);
Model(int rows, int columns, QObject *parent = nullptr);
~Model();
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
@ -80,7 +80,7 @@ private:
struct Node
{
Node(Node *parent = 0) : parent(parent), children(0) {}
Node(Node *parent = nullptr) : parent(parent), children(nullptr) {}
~Node() { delete children; }
Node *parent;
QVector<Node> *children;

View File

@ -48,8 +48,8 @@
**
****************************************************************************/
#include "imagemodel.h"
#include "mainwindow.h"
#include "imagemodel.h"
#include "pixeldelegate.h"
#include <QtWidgets>

View File

@ -54,10 +54,8 @@
//! [0]
PixelDelegate::PixelDelegate(QObject *parent)
: QAbstractItemDelegate(parent)
{
pixelSize = 12;
}
: QAbstractItemDelegate(parent), pixelSize(12)
{}
//! [0]
//! [1]
@ -70,11 +68,11 @@ void PixelDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
//! [1]
//! [3]
int size = qMin(option.rect.width(), option.rect.height());
const int size = qMin(option.rect.width(), option.rect.height());
//! [3] //! [4]
int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
double radius = (size / 2.0) - (brightness / 255.0 * size / 2.0);
if (radius == 0.0)
const int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
const double radius = (size / 2.0) - (brightness / 255.0 * size / 2.0);
if (qFuzzyIsNull(radius))
return;
//! [4]

View File

@ -61,7 +61,7 @@ class QObject;
class QPainter;
QT_END_NAMESPACE
static const int ItemSize = 256;
static constexpr int ItemSize = 256;
//! [0]
class PixelDelegate : public QAbstractItemDelegate
@ -69,13 +69,13 @@ class PixelDelegate : public QAbstractItemDelegate
Q_OBJECT
public:
PixelDelegate(QObject *parent = 0);
PixelDelegate(QObject *parent = nullptr);
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index ) const override;
const QModelIndex &index) const override;
public slots:
void setPixelSize(int size);

View File

@ -134,7 +134,7 @@ QMimeData *PiecesModel::mimeData(const QModelIndexList &indexes) const
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (QModelIndex index, indexes) {
for (const QModelIndex &index : indexes) {
if (index.isValid()) {
QPixmap pixmap = qvariant_cast<QPixmap>(data(index, Qt::UserRole));
QPoint location = data(index, Qt::UserRole+1).toPoint();
@ -190,10 +190,7 @@ bool PiecesModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
int PiecesModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
else
return pixmaps.size();
return parent.isValid() ? 0 : pixmaps.size();
}
Qt::DropActions PiecesModel::supportedDropActions() const
@ -201,7 +198,7 @@ Qt::DropActions PiecesModel::supportedDropActions() const
return Qt::CopyAction | Qt::MoveAction;
}
void PiecesModel::addPieces(const QPixmap& pixmap)
void PiecesModel::addPieces(const QPixmap &pixmap)
{
if (!pixmaps.isEmpty()) {
beginRemoveRows(QModelIndex(), 0, pixmaps.size() - 1);

View File

@ -52,10 +52,10 @@
#define PIECESLIST_H
#include <QAbstractListModel>
#include <QList>
#include <QPixmap>
#include <QPoint>
#include <QStringList>
#include <QVector>
QT_BEGIN_NAMESPACE
class QMimeData;
@ -66,7 +66,7 @@ class PiecesModel : public QAbstractListModel
Q_OBJECT
public:
explicit PiecesModel(int pieceSize, QObject *parent = 0);
explicit PiecesModel(int pieceSize, QObject *parent = nullptr);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
@ -80,11 +80,11 @@ public:
Qt::DropActions supportedDropActions() const override;
void addPiece(const QPixmap &pixmap, const QPoint &location);
void addPieces(const QPixmap& pixmap);
void addPieces(const QPixmap &pixmap);
private:
QList<QPoint> locations;
QList<QPixmap> pixmaps;
QVector<QPoint> locations;
QVector<QPixmap> pixmaps;
int m_PieceSize;
};

View File

@ -69,7 +69,7 @@ class Window : public QWidget
Q_OBJECT
public:
Window(QWidget *parent = 0);
Window(QWidget *parent = nullptr);
private slots:
void updateButtons(int row);

View File

@ -68,8 +68,8 @@ SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
//! [1]
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setFrame(false);
@ -105,7 +105,8 @@ void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
//! [4]
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
const QStyleOptionViewItem &option,
const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

View File

@ -59,7 +59,7 @@ class SpinBoxDelegate : public QStyledItemDelegate
Q_OBJECT
public:
SpinBoxDelegate(QObject *parent = 0);
SpinBoxDelegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
@ -68,8 +68,8 @@ public:
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
};
//! [0]

View File

@ -49,10 +49,9 @@
**
****************************************************************************/
#include <QtWidgets/QApplication>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QShortcut>
#include <QtWidgets/QTreeView>
#include <QApplication>
#include <QShortcut>
#include <QTreeView>
#include "storagemodel.h"

View File

@ -53,14 +53,6 @@
#include <QDir>
#include <QLocale>
#include <qmath.h>
#include <algorithm>
#include <cmath>
StorageModel::StorageModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
void StorageModel::refresh()
{

View File

@ -74,7 +74,7 @@ public:
ColumnCount
};
explicit StorageModel(QObject *parent = nullptr);
using QAbstractTableModel::QAbstractTableModel;
int columnCount(const QModelIndex &parent) const override;
int rowCount(const QModelIndex &parent) const override;