From 8c685b765bf4ceba3c4cf8fdd9c9d680f338b7a9 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 20 Oct 2018 21:48:28 +0200 Subject: Itemviews: Cleanup examples MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cleanup some minor issues in the chart example: - remove unused members - use initializer list for members - pass a proper role to dataChanged() - honor roles parameter in PieView::dataChanged() - use nullptr instead 0 - use new-style connect - fix indentation and other whitespaces Change-Id: Idb212b07c006fe3ae31bee9cd9b1ba4d03043b5e Reviewed-by: André Hartmann Reviewed-by: Paul Wicking --- .../widgets/itemviews/addressbook/tablemodel.cpp | 2 +- examples/widgets/itemviews/chart/mainwindow.cpp | 18 +++---- examples/widgets/itemviews/chart/mainwindow.h | 8 ++-- examples/widgets/itemviews/chart/pieview.cpp | 56 ++++++++++------------ examples/widgets/itemviews/chart/pieview.h | 14 +++--- .../itemviews/editabletreemodel/treemodel.cpp | 2 +- .../tutorials/modelview/1_readonly/main.cpp | 8 ++-- .../tutorials/modelview/1_readonly/mymodel.cpp | 5 +- .../tutorials/modelview/1_readonly/mymodel.h | 4 +- .../tutorials/modelview/2_formatting/main.cpp | 8 ++-- .../tutorials/modelview/2_formatting/mymodel.cpp | 24 +++------- .../tutorials/modelview/2_formatting/mymodel.h | 4 +- .../tutorials/modelview/3_changingmodel/main.cpp | 6 +-- .../modelview/3_changingmodel/mymodel.cpp | 23 ++++----- .../tutorials/modelview/3_changingmodel/mymodel.h | 7 ++- .../widgets/tutorials/modelview/4_headers/main.cpp | 10 ++-- .../tutorials/modelview/4_headers/mymodel.cpp | 25 ++++------ .../tutorials/modelview/4_headers/mymodel.h | 4 +- .../widgets/tutorials/modelview/5_edit/main.cpp | 2 +- .../tutorials/modelview/5_edit/mainwindow.cpp | 14 +++--- .../tutorials/modelview/5_edit/mainwindow.h | 8 ++-- .../widgets/tutorials/modelview/5_edit/mymodel.cpp | 30 +++++------- .../widgets/tutorials/modelview/5_edit/mymodel.h | 8 ++-- .../tutorials/modelview/6_treeview/main.cpp | 2 +- .../tutorials/modelview/6_treeview/mainwindow.cpp | 23 +++++---- .../tutorials/modelview/6_treeview/mainwindow.h | 12 ++--- .../tutorials/modelview/7_selections/main.cpp | 2 +- .../modelview/7_selections/mainwindow.cpp | 18 +++---- .../tutorials/modelview/7_selections/mainwindow.h | 6 +-- 29 files changed, 161 insertions(+), 192 deletions(-) diff --git a/examples/widgets/itemviews/addressbook/tablemodel.cpp b/examples/widgets/itemviews/addressbook/tablemodel.cpp index 674e312753..b3704f857e 100644 --- a/examples/widgets/itemviews/addressbook/tablemodel.cpp +++ b/examples/widgets/itemviews/addressbook/tablemodel.cpp @@ -164,7 +164,7 @@ bool TableModel::setData(const QModelIndex &index, const QVariant &value, int ro return false; contacts.replace(row, contact); - emit(dataChanged(index, index)); + emit dataChanged(index, index, {role}); return true; } diff --git a/examples/widgets/itemviews/chart/mainwindow.cpp b/examples/widgets/itemviews/chart/mainwindow.cpp index 91e535a006..53f57fbb49 100644 --- a/examples/widgets/itemviews/chart/mainwindow.cpp +++ b/examples/widgets/itemviews/chart/mainwindow.cpp @@ -48,12 +48,13 @@ ** ****************************************************************************/ -#include - #include "pieview.h" #include "mainwindow.h" -MainWindow::MainWindow() +#include + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) { QMenu *fileMenu = new QMenu(tr("&File"), this); QAction *openAction = fileMenu->addAction(tr("&Open...")); @@ -124,17 +125,18 @@ void MainWindow::loadFile(const QString &fileName) return; QTextStream stream(&file); - QString line; model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex()); int row = 0; - do { - line = stream.readLine(); + while (!stream.atEnd()) { + const QString line = stream.readLine(); if (!line.isEmpty()) { model->insertRows(row, 1, QModelIndex()); - QStringList pieces = line.split(',', QString::SkipEmptyParts); + const QStringList pieces = line.split(',', QString::SkipEmptyParts); + if (pieces.size() < 3) + continue; model->setData(model->index(row, 0, QModelIndex()), pieces.value(0)); model->setData(model->index(row, 1, QModelIndex()), @@ -143,7 +145,7 @@ void MainWindow::loadFile(const QString &fileName) QColor(pieces.value(2)), Qt::DecorationRole); row++; } - } while (!line.isEmpty()); + }; file.close(); statusBar()->showMessage(tr("Loaded %1").arg(fileName), 2000); diff --git a/examples/widgets/itemviews/chart/mainwindow.h b/examples/widgets/itemviews/chart/mainwindow.h index 058f5c7e90..51176d261b 100644 --- a/examples/widgets/itemviews/chart/mainwindow.h +++ b/examples/widgets/itemviews/chart/mainwindow.h @@ -56,7 +56,6 @@ QT_BEGIN_NAMESPACE class QAbstractItemModel; class QAbstractItemView; -class QItemSelectionModel; QT_END_NAMESPACE class MainWindow : public QMainWindow @@ -64,7 +63,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - MainWindow(); + MainWindow(QWidget *parent = nullptr); private slots: void openFile(); @@ -75,9 +74,8 @@ private: void setupViews(); void loadFile(const QString &path); - QAbstractItemModel *model; - QAbstractItemView *pieChart; - QItemSelectionModel *selectionModel; + QAbstractItemModel *model = nullptr; + QAbstractItemView *pieChart = nullptr; }; #endif // MAINWINDOW_H diff --git a/examples/widgets/itemviews/chart/pieview.cpp b/examples/widgets/itemviews/chart/pieview.cpp index 3f85e397ee..457ed8b4ec 100644 --- a/examples/widgets/itemviews/chart/pieview.cpp +++ b/examples/widgets/itemviews/chart/pieview.cpp @@ -48,30 +48,25 @@ ** ****************************************************************************/ -#include -#include -#include #include "pieview.h" +#include + PieView::PieView(QWidget *parent) : QAbstractItemView(parent) { horizontalScrollBar()->setRange(0, 0); verticalScrollBar()->setRange(0, 0); - - margin = 8; - totalSize = 300; - pieSize = totalSize - 2 * margin; - validItems = 0; - totalValue = 0.0; - rubberBand = 0; } void PieView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, - const QVector &) + const QVector &roles) { - QAbstractItemView::dataChanged(topLeft, bottomRight); + QAbstractItemView::dataChanged(topLeft, bottomRight, roles); + + if (!roles.contains(Qt::DisplayRole)) + return; validItems = 0; totalValue = 0.0; @@ -79,7 +74,7 @@ void PieView::dataChanged(const QModelIndex &topLeft, for (int row = 0; row < model()->rowCount(rootIndex()); ++row) { QModelIndex index = model()->index(row, 1, rootIndex()); - double value = model()->data(index).toDouble(); + double value = model()->data(index, Qt::DisplayRole).toDouble(); if (value > 0.0) { totalValue += value; @@ -197,15 +192,14 @@ QRect PieView::itemRect(const QModelIndex &index) const listItem++; } - double itemHeight; - switch (index.column()) { - case 0: - itemHeight = QFontMetrics(viewOptions().font).height(); + case 0: { + const qreal itemHeight = QFontMetricsF(viewOptions().font).height(); return QRect(totalSize, - int(margin + listItem*itemHeight), - totalSize - margin, int(itemHeight)); + qRound(margin + listItem * itemHeight), + totalSize - margin, qRound(itemHeight)); + } case 1: return viewport()->rect(); } @@ -235,7 +229,7 @@ QRegion PieView::itemRegion(const QModelIndex &index) const if (sliceIndex == index) { QPainterPath slicePath; slicePath.moveTo(totalSize / 2, totalSize / 2); - slicePath.arcTo(margin, margin, margin+pieSize, margin+pieSize, + slicePath.arcTo(margin, margin, margin + pieSize, margin + pieSize, startAngle, angle); slicePath.closeSubpath(); @@ -342,7 +336,7 @@ void PieView::paintEvent(QPaintEvent *event) double value = model()->data(index).toDouble(); if (value > 0.0) { - double angle = 360*value/totalValue; + double angle = 360 * value / totalValue; QModelIndex colorIndex = model()->index(row, 0, rootIndex()); QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString()); @@ -480,16 +474,16 @@ void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlag } if (indexes.size() > 0) { - int firstRow = indexes[0].row(); - int lastRow = indexes[0].row(); - int firstColumn = indexes[0].column(); - int lastColumn = indexes[0].column(); + int firstRow = indexes.at(0).row(); + int lastRow = firstRow; + int firstColumn = indexes.at(0).column(); + int lastColumn = firstColumn; for (int i = 1; i < indexes.size(); ++i) { - firstRow = qMin(firstRow, indexes[i].row()); - lastRow = qMax(lastRow, indexes[i].row()); - firstColumn = qMin(firstColumn, indexes[i].column()); - lastColumn = qMax(lastColumn, indexes[i].column()); + firstRow = qMin(firstRow, indexes.at(i).row()); + lastRow = qMax(lastRow, indexes.at(i).row()); + firstColumn = qMin(firstColumn, indexes.at(i).column()); + lastColumn = qMax(lastColumn, indexes.at(i).column()); } QItemSelection selection( @@ -508,7 +502,7 @@ void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlag void PieView::updateGeometries() { horizontalScrollBar()->setPageStep(viewport()->width()); - horizontalScrollBar()->setRange(0, qMax(0, 2*totalSize - viewport()->width())); + horizontalScrollBar()->setRange(0, qMax(0, 2 * totalSize - viewport()->width())); verticalScrollBar()->setPageStep(viewport()->height()); verticalScrollBar()->setRange(0, qMax(0, totalSize - viewport()->height())); } @@ -546,7 +540,7 @@ QRegion PieView::visualRegionForSelection(const QItemSelection &selection) const QRegion region; for (int i = 0; i < ranges; ++i) { - QItemSelectionRange range = selection.at(i); + const QItemSelectionRange &range = selection.at(i); for (int row = range.top(); row <= range.bottom(); ++row) { for (int col = range.left(); col <= range.right(); ++col) { QModelIndex index = model()->index(row, col, rootIndex()); diff --git a/examples/widgets/itemviews/chart/pieview.h b/examples/widgets/itemviews/chart/pieview.h index aa397e6d55..22c74dde81 100644 --- a/examples/widgets/itemviews/chart/pieview.h +++ b/examples/widgets/itemviews/chart/pieview.h @@ -59,7 +59,7 @@ class PieView : public QAbstractItemView Q_OBJECT public: - PieView(QWidget *parent = 0); + PieView(QWidget *parent = nullptr); QRect visualRect(const QModelIndex &index) const override; void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) override; @@ -100,13 +100,13 @@ private: int rows(const QModelIndex &index = QModelIndex()) const; void updateGeometries() override; - int margin; - int totalSize; - int pieSize; - int validItems; - double totalValue; + int margin = 0; + int totalSize = 300; + int pieSize = totalSize - 2 * margin; + int validItems = 0; + double totalValue = 0.0; + QRubberBand *rubberBand = nullptr; QPoint origin; - QRubberBand *rubberBand; }; //! [0] diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp index dbd53df1e6..72818372d7 100644 --- a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp +++ b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp @@ -225,7 +225,7 @@ bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int rol bool result = item->setData(index.column(), value); if (result) - emit dataChanged(index, index); + emit dataChanged(index, index, {role}); return result; } diff --git a/examples/widgets/tutorials/modelview/1_readonly/main.cpp b/examples/widgets/tutorials/modelview/1_readonly/main.cpp index 80383f8c94..ea571d3699 100644 --- a/examples/widgets/tutorials/modelview/1_readonly/main.cpp +++ b/examples/widgets/tutorials/modelview/1_readonly/main.cpp @@ -50,16 +50,16 @@ //! [Quoting ModelView Tutorial] // main.cpp -#include -#include +#include +#include #include "mymodel.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableView tableView; - MyModel myModel(0); - tableView.setModel( &myModel ); + MyModel myModel; + tableView.setModel(&myModel); tableView.show(); return a.exec(); } diff --git a/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp b/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp index 82ec53c665..2b36565bc6 100644 --- a/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp +++ b/examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp @@ -53,7 +53,7 @@ #include "mymodel.h" MyModel::MyModel(QObject *parent) - :QAbstractTableModel(parent) + : QAbstractTableModel(parent) { } @@ -70,11 +70,10 @@ int MyModel::columnCount(const QModelIndex & /*parent*/) const QVariant MyModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) - { return QString("Row%1, Column%2") .arg(index.row() + 1) .arg(index.column() +1); - } + return QVariant(); } //! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/1_readonly/mymodel.h b/examples/widgets/tutorials/modelview/1_readonly/mymodel.h index e675cc6bc2..8761322896 100644 --- a/examples/widgets/tutorials/modelview/1_readonly/mymodel.h +++ b/examples/widgets/tutorials/modelview/1_readonly/mymodel.h @@ -59,8 +59,8 @@ class MyModel : public QAbstractTableModel { Q_OBJECT public: - MyModel(QObject *parent); - int rowCount(const QModelIndex &parent = QModelIndex()) const override ; + MyModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; }; diff --git a/examples/widgets/tutorials/modelview/2_formatting/main.cpp b/examples/widgets/tutorials/modelview/2_formatting/main.cpp index 80383f8c94..ea571d3699 100644 --- a/examples/widgets/tutorials/modelview/2_formatting/main.cpp +++ b/examples/widgets/tutorials/modelview/2_formatting/main.cpp @@ -50,16 +50,16 @@ //! [Quoting ModelView Tutorial] // main.cpp -#include -#include +#include +#include #include "mymodel.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableView tableView; - MyModel myModel(0); - tableView.setModel( &myModel ); + MyModel myModel; + tableView.setModel(&myModel); tableView.show(); return a.exec(); } diff --git a/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp b/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp index 9511648392..938597a34e 100644 --- a/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp +++ b/examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp @@ -48,13 +48,14 @@ ** ****************************************************************************/ +#include "mymodel.h" + #include #include -#include "mymodel.h" #include MyModel::MyModel(QObject *parent) - :QAbstractTableModel(parent) + : QAbstractTableModel(parent) { } @@ -78,7 +79,7 @@ QVariant MyModel::data(const QModelIndex &index, int role) const qDebug() << QString("row %1, col%2, role %3") .arg(row).arg(col).arg(role); - switch(role){ + switch (role) { case Qt::DisplayRole: if (row == 0 && col == 1) return QString("<--left"); if (row == 1 && col == 1) return QString("right-->"); @@ -86,36 +87,25 @@ QVariant MyModel::data(const QModelIndex &index, int role) const return QString("Row%1, Column%2") .arg(row + 1) .arg(col +1); - break; case Qt::FontRole: - if (row == 0 && col == 0) //change font only for cell(0,0) - { + if (row == 0 && col == 0) { //change font only for cell(0,0) QFont boldFont; boldFont.setBold(true); return boldFont; } break; case Qt::BackgroundRole: - if (row == 1 && col == 2) //change background only for cell(1,2) - { - QBrush redBackground(Qt::red); - return redBackground; - } + return QBrush(Qt::red); break; case Qt::TextAlignmentRole: - if (row == 1 && col == 1) //change text alignment only for cell(1,1) - { return Qt::AlignRight + Qt::AlignVCenter; - } break; case Qt::CheckStateRole: - if (row == 1 && col == 0) //add a checkbox to cell(1,0) - { return Qt::Checked; - } + break; } return QVariant(); } diff --git a/examples/widgets/tutorials/modelview/2_formatting/mymodel.h b/examples/widgets/tutorials/modelview/2_formatting/mymodel.h index 9c12f98ba9..e8ae673f62 100644 --- a/examples/widgets/tutorials/modelview/2_formatting/mymodel.h +++ b/examples/widgets/tutorials/modelview/2_formatting/mymodel.h @@ -57,8 +57,8 @@ class MyModel : public QAbstractTableModel { Q_OBJECT public: - MyModel(QObject *parent); - int rowCount(const QModelIndex &parent = QModelIndex()) const override ; + MyModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; }; diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp b/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp index c03019a910..2330019f93 100644 --- a/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp +++ b/examples/widgets/tutorials/modelview/3_changingmodel/main.cpp @@ -48,8 +48,8 @@ ** ****************************************************************************/ -#include -#include +#include +#include #include "mymodel.h" int main(int argc, char *argv[]) @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); QTableView tableView; MyModel myModel(0); - tableView.setModel( &myModel ); + tableView.setModel(&myModel); tableView.show(); return a.exec(); } diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp index e4580a0e01..f289d9abda 100644 --- a/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp +++ b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp @@ -48,18 +48,17 @@ ** ****************************************************************************/ -#include -#include #include "mymodel.h" +#include + //! [quoting mymodel_a] MyModel::MyModel(QObject *parent) - :QAbstractTableModel(parent) + : QAbstractTableModel(parent) + , timer(new QTimer(this)) { -// selectedCell = 0; - timer = new QTimer(this); timer->setInterval(1000); - connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit())); + connect(timer, &QTimer::timeout , this, &MyModel::timerHit); timer->start(); } //! [quoting mymodel_a] @@ -82,13 +81,9 @@ QVariant MyModel::data(const QModelIndex &index, int role) const int row = index.row(); int col = index.column(); - if (role == Qt::DisplayRole) - { - if (row == 0 && col == 0) - { - return QTime::currentTime().toString(); - } - } + if (role == Qt::DisplayRole && row == 0 && col == 0) + return QTime::currentTime().toString(); + return QVariant(); } //! [quoting mymodel_QVariant ] @@ -99,6 +94,6 @@ void MyModel::timerHit() //we identify the top left cell QModelIndex topLeft = createIndex(0,0); //emit a signal to make the view reread identified data - emit dataChanged(topLeft, topLeft); + emit dataChanged(topLeft, topLeft, {Qt::DisplayRole}); } //! [quoting mymodel_b ] diff --git a/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h index dfcf0f6239..2ef0e480c2 100644 --- a/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h +++ b/examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h @@ -58,13 +58,12 @@ class MyModel : public QAbstractTableModel { Q_OBJECT public: - MyModel(QObject *parent); - int rowCount(const QModelIndex &parent = QModelIndex()) const override ; + MyModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - QTimer *timer; private: - int selectedCell; + QTimer *timer; private slots: void timerHit(); }; diff --git a/examples/widgets/tutorials/modelview/4_headers/main.cpp b/examples/widgets/tutorials/modelview/4_headers/main.cpp index 82d493345d..90a8c6e894 100644 --- a/examples/widgets/tutorials/modelview/4_headers/main.cpp +++ b/examples/widgets/tutorials/modelview/4_headers/main.cpp @@ -48,16 +48,16 @@ ** ****************************************************************************/ -#include -#include +#include +#include #include "mymodel.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableView tableView; - MyModel myModel(0); - tableView.setModel( &myModel ); + MyModel myModel; + tableView.setModel(&myModel); tableView.show(); return a.exec(); -} \ No newline at end of file +} diff --git a/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp b/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp index 35e3463b7f..0084475374 100644 --- a/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp +++ b/examples/widgets/tutorials/modelview/4_headers/mymodel.cpp @@ -51,7 +51,7 @@ #include "mymodel.h" MyModel::MyModel(QObject *parent) - :QAbstractTableModel(parent) + : QAbstractTableModel(parent) { } @@ -70,8 +70,7 @@ int MyModel::columnCount(const QModelIndex & /*parent*/) const //------------------------------------------------------- QVariant MyModel::data(const QModelIndex &index, int role) const { - if (role == Qt::DisplayRole) - { + if (role == Qt::DisplayRole) { return QString("Row%1, Column%2") .arg(index.row() + 1) .arg(index.column() +1); @@ -82,18 +81,14 @@ QVariant MyModel::data(const QModelIndex &index, int role) const //! [quoting mymodel_c] QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (role == Qt::DisplayRole) - { - if (orientation == Qt::Horizontal) { - switch (section) - { - case 0: - return QString("first"); - case 1: - return QString("second"); - case 2: - return QString("third"); - } + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + switch (section) { + case 0: + return QString("first"); + case 1: + return QString("second"); + case 2: + return QString("third"); } } return QVariant(); diff --git a/examples/widgets/tutorials/modelview/4_headers/mymodel.h b/examples/widgets/tutorials/modelview/4_headers/mymodel.h index fb2d7aa940..6d8477dae2 100644 --- a/examples/widgets/tutorials/modelview/4_headers/mymodel.h +++ b/examples/widgets/tutorials/modelview/4_headers/mymodel.h @@ -57,8 +57,8 @@ class MyModel : public QAbstractTableModel { Q_OBJECT public: - MyModel(QObject *parent); - int rowCount(const QModelIndex &parent = QModelIndex()) const override ; + MyModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role) const override; diff --git a/examples/widgets/tutorials/modelview/5_edit/main.cpp b/examples/widgets/tutorials/modelview/5_edit/main.cpp index e1b46339b1..315875a627 100644 --- a/examples/widgets/tutorials/modelview/5_edit/main.cpp +++ b/examples/widgets/tutorials/modelview/5_edit/main.cpp @@ -48,7 +48,7 @@ ** ****************************************************************************/ -#include +#include #include "mainwindow.h" int main(int argc, char *argv[]) diff --git a/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp b/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp index e0b6ea7b1f..d8dfc33212 100644 --- a/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp +++ b/examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp @@ -48,23 +48,25 @@ ** ****************************************************************************/ -#include #include "mainwindow.h" #include "mymodel.h" +#include + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) + , tableView(new QTableView(this)) { - tableView = new QTableView(this); setCentralWidget(tableView); - QAbstractTableModel *myModel = new MyModel(this); + MyModel *myModel = new MyModel(this); tableView->setModel(myModel); //transfer changes to the model to the window title - connect(myModel, SIGNAL(editCompleted(const QString &)), this, SLOT(setWindowTitle(const QString &))); + connect(myModel, &MyModel::editCompleted, + this, &MainWindow::showWindowTitle); } -void MainWindow::showWindowTitle(const QString & title) +void MainWindow::showWindowTitle(const QString &title) { -setWindowTitle(title); + setWindowTitle(title); } diff --git a/examples/widgets/tutorials/modelview/5_edit/mainwindow.h b/examples/widgets/tutorials/modelview/5_edit/mainwindow.h index 7915b29cdf..104f06f82e 100644 --- a/examples/widgets/tutorials/modelview/5_edit/mainwindow.h +++ b/examples/widgets/tutorials/modelview/5_edit/mainwindow.h @@ -51,9 +51,9 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include +#include -QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code +QT_BEGIN_NAMESPACE class QTableView; //forward declaration QT_END_NAMESPACE @@ -64,9 +64,9 @@ class MainWindow : public QMainWindow private: QTableView *tableView; public: - MainWindow(QWidget *parent = 0); + MainWindow(QWidget *parent = nullptr); public slots: - void showWindowTitle(const QString & title); + void showWindowTitle(const QString &title); }; #endif // MAINWINDOW_H diff --git a/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp b/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp index fb2954baf2..eeca2ce0a0 100644 --- a/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp +++ b/examples/widgets/tutorials/modelview/5_edit/mymodel.cpp @@ -48,12 +48,10 @@ ** ****************************************************************************/ - #include "mymodel.h" - MyModel::MyModel(QObject *parent) - :QAbstractTableModel(parent) + : QAbstractTableModel(parent) { } @@ -72,33 +70,31 @@ int MyModel::columnCount(const QModelIndex & /*parent*/) const //----------------------------------------------------------------- QVariant MyModel::data(const QModelIndex &index, int role) const { - if (role == Qt::DisplayRole) - { - return m_gridData[index.row()][index.column()]; - } + if (role == Qt::DisplayRole && checkIndex(index)) + return m_gridData[index.row()][index.column()]; + return QVariant(); } //----------------------------------------------------------------- //! [quoting mymodel_e] -bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role) +bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) { - if (role == Qt::EditRole) - { + if (role == Qt::EditRole) { + if (!checkIndex(index)) + return false; //save value from editor to member m_gridData m_gridData[index.row()][index.column()] = value.toString(); //for presentation purposes only: build and emit a joined string QString result; - for (int row= 0; row < ROWS; row++) - { - for(int col= 0; col < COLS; col++) - { + for (int row = 0; row < ROWS; row++) { + for (int col= 0; col < COLS; col++) result += m_gridData[row][col] + ' '; - } } - emit editCompleted( result ); + emit editCompleted(result); + return true; } - return true; + return false; } //! [quoting mymodel_e] diff --git a/examples/widgets/tutorials/modelview/5_edit/mymodel.h b/examples/widgets/tutorials/modelview/5_edit/mymodel.h index dd0d4f5066..ce462351af 100644 --- a/examples/widgets/tutorials/modelview/5_edit/mymodel.h +++ b/examples/widgets/tutorials/modelview/5_edit/mymodel.h @@ -64,12 +64,12 @@ class MyModel : public QAbstractTableModel { Q_OBJECT public: - MyModel(QObject *parent); - int rowCount(const QModelIndex &parent = QModelIndex()) const override ; + MyModel(QObject *parent = nullptr); + int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override; - Qt::ItemFlags flags(const QModelIndex & index) const override ; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + Qt::ItemFlags flags(const QModelIndex &index) const override; private: QString m_gridData[ROWS][COLS]; //holds text entered into QTableView signals: diff --git a/examples/widgets/tutorials/modelview/6_treeview/main.cpp b/examples/widgets/tutorials/modelview/6_treeview/main.cpp index e1b46339b1..315875a627 100644 --- a/examples/widgets/tutorials/modelview/6_treeview/main.cpp +++ b/examples/widgets/tutorials/modelview/6_treeview/main.cpp @@ -48,7 +48,7 @@ ** ****************************************************************************/ -#include +#include #include "mainwindow.h" int main(int argc, char *argv[]) diff --git a/examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp index 1016afba07..ae4a343680 100644 --- a/examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp +++ b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp @@ -50,24 +50,25 @@ //! [Quoting ModelView Tutorial] // modelview.cpp +#include "mainwindow.h" + #include #include #include -#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) + , treeView(new QTreeView(this)) + , standardModel(new QStandardItemModel(this)) { - treeView = new QTreeView(this); setCentralWidget(treeView); - standardModel = new QStandardItemModel ; - QList preparedRow =prepareRow("first", "second", "third"); + QList preparedRow = prepareRow("first", "second", "third"); QStandardItem *item = standardModel->invisibleRootItem(); // adding a row to the invisible root item produces a root element item->appendRow(preparedRow); - QList secondRow =prepareRow("111", "222", "333"); + QList secondRow = prepareRow("111", "222", "333"); // adding a row to an item starts a subtree preparedRow.first()->appendRow(secondRow); @@ -76,13 +77,11 @@ MainWindow::MainWindow(QWidget *parent) } QList MainWindow::prepareRow(const QString &first, - const QString &second, - const QString &third) + const QString &second, + const QString &third) const { - QList rowItems; - rowItems << new QStandardItem(first); - rowItems << new QStandardItem(second); - rowItems << new QStandardItem(third); - return rowItems; + return {new QStandardItem(first), + new QStandardItem(second), + new QStandardItem(third)}; } //! [Quoting ModelView Tutorial] diff --git a/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h index d625d5991d..65d9c18658 100644 --- a/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h +++ b/examples/widgets/tutorials/modelview/6_treeview/mainwindow.h @@ -51,9 +51,9 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include +#include -QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code +QT_BEGIN_NAMESPACE class QTreeView; //forward declarations class QStandardItemModel; class QStandardItem; @@ -66,11 +66,11 @@ class MainWindow : public QMainWindow private: QTreeView *treeView; QStandardItemModel *standardModel; - QList prepareRow( const QString &first, - const QString &second, - const QString &third ); + QList prepareRow(const QString &first, + const QString &second, + const QString &third) const; public: - MainWindow(QWidget *parent = 0); + MainWindow(QWidget *parent = nullptr); }; #endif // MAINWINDOW_H diff --git a/examples/widgets/tutorials/modelview/7_selections/main.cpp b/examples/widgets/tutorials/modelview/7_selections/main.cpp index e1b46339b1..315875a627 100644 --- a/examples/widgets/tutorials/modelview/7_selections/main.cpp +++ b/examples/widgets/tutorials/modelview/7_selections/main.cpp @@ -48,7 +48,7 @@ ** ****************************************************************************/ -#include +#include #include "mainwindow.h" int main(int argc, char *argv[]) diff --git a/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp b/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp index 200751ee6d..2b10071a68 100644 --- a/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp +++ b/examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp @@ -49,17 +49,18 @@ ****************************************************************************/ //! [quoting modelview_a] +#include "mainwindow.h" + #include #include #include -#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) + , treeView(new QTreeView(this)) + , standardModel(new QStandardItemModel(this)) { - treeView = new QTreeView(this); setCentralWidget(treeView); - standardModel = new QStandardItemModel ; QStandardItem *rootNode = standardModel->invisibleRootItem(); @@ -88,9 +89,9 @@ MainWindow::MainWindow(QWidget *parent) treeView->expandAll(); //selection changes shall trigger a slot - QItemSelectionModel *selectionModel= treeView->selectionModel(); - connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)), - this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &))); + QItemSelectionModel *selectionModel = treeView->selectionModel(); + connect(selectionModel, &QItemSelectionModel::selectionChanged, + this, &MainWindow::selectionChangedSlot); } //! [quoting modelview_a] @@ -103,10 +104,9 @@ void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, c const QModelIndex index = treeView->selectionModel()->currentIndex(); QString selectedText = index.data(Qt::DisplayRole).toString(); //find out the hierarchy level of the selected item - int hierarchyLevel=1; + int hierarchyLevel = 1; QModelIndex seekRoot = index; - while(seekRoot.parent() != QModelIndex()) - { + while (seekRoot.parent() != QModelIndex()) { seekRoot = seekRoot.parent(); hierarchyLevel++; } diff --git a/examples/widgets/tutorials/modelview/7_selections/mainwindow.h b/examples/widgets/tutorials/modelview/7_selections/mainwindow.h index 13ca7dbc30..c9761dd3d9 100644 --- a/examples/widgets/tutorials/modelview/7_selections/mainwindow.h +++ b/examples/widgets/tutorials/modelview/7_selections/mainwindow.h @@ -51,9 +51,9 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include +#include -QT_BEGIN_NAMESPACE // QT_BEGIN_NAMESPACE / QT_END_NAMESPACE are not needed in Qt user code +QT_BEGIN_NAMESPACE class QTreeView; //forward declarations class QStandardItemModel; class QItemSelection; @@ -67,7 +67,7 @@ private: QTreeView *treeView; QStandardItemModel *standardModel; private slots: - void selectionChangedSlot(const QItemSelection & newSelection, const QItemSelection & oldSelection); + void selectionChangedSlot(const QItemSelection &newSelection, const QItemSelection &oldSelection); public: MainWindow(QWidget *parent = 0); }; -- cgit v1.2.3