summaryrefslogtreecommitdiffstats
path: root/examples/widgets/tutorials/modelview
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2018-10-20 21:48:28 +0200
committerLiang Qi <liang.qi@qt.io>2018-11-12 13:01:30 +0000
commit8c685b765bf4ceba3c4cf8fdd9c9d680f338b7a9 (patch)
tree51c5eafe87638e79961c63898cc63bce87edcadd /examples/widgets/tutorials/modelview
parent1c957bb8e57adf7674716f40b67c5d6877b32f86 (diff)
Itemviews: Cleanup examples
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 <aha_1980@gmx.de> Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Diffstat (limited to 'examples/widgets/tutorials/modelview')
-rw-r--r--examples/widgets/tutorials/modelview/1_readonly/main.cpp8
-rw-r--r--examples/widgets/tutorials/modelview/1_readonly/mymodel.cpp5
-rw-r--r--examples/widgets/tutorials/modelview/1_readonly/mymodel.h4
-rw-r--r--examples/widgets/tutorials/modelview/2_formatting/main.cpp8
-rw-r--r--examples/widgets/tutorials/modelview/2_formatting/mymodel.cpp24
-rw-r--r--examples/widgets/tutorials/modelview/2_formatting/mymodel.h4
-rw-r--r--examples/widgets/tutorials/modelview/3_changingmodel/main.cpp6
-rw-r--r--examples/widgets/tutorials/modelview/3_changingmodel/mymodel.cpp23
-rw-r--r--examples/widgets/tutorials/modelview/3_changingmodel/mymodel.h7
-rw-r--r--examples/widgets/tutorials/modelview/4_headers/main.cpp10
-rw-r--r--examples/widgets/tutorials/modelview/4_headers/mymodel.cpp25
-rw-r--r--examples/widgets/tutorials/modelview/4_headers/mymodel.h4
-rw-r--r--examples/widgets/tutorials/modelview/5_edit/main.cpp2
-rw-r--r--examples/widgets/tutorials/modelview/5_edit/mainwindow.cpp14
-rw-r--r--examples/widgets/tutorials/modelview/5_edit/mainwindow.h8
-rw-r--r--examples/widgets/tutorials/modelview/5_edit/mymodel.cpp30
-rw-r--r--examples/widgets/tutorials/modelview/5_edit/mymodel.h8
-rw-r--r--examples/widgets/tutorials/modelview/6_treeview/main.cpp2
-rw-r--r--examples/widgets/tutorials/modelview/6_treeview/mainwindow.cpp23
-rw-r--r--examples/widgets/tutorials/modelview/6_treeview/mainwindow.h12
-rw-r--r--examples/widgets/tutorials/modelview/7_selections/main.cpp2
-rw-r--r--examples/widgets/tutorials/modelview/7_selections/mainwindow.cpp18
-rw-r--r--examples/widgets/tutorials/modelview/7_selections/mainwindow.h6
23 files changed, 114 insertions, 139 deletions
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 <QtWidgets/QApplication>
-#include <QtWidgets/QTableView>
+#include <QApplication>
+#include <QTableView>
#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 <QtWidgets/QApplication>
-#include <QtWidgets/QTableView>
+#include <QApplication>
+#include <QTableView>
#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 <QFont>
#include <QBrush>
-#include "mymodel.h"
#include <QDebug>
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 <QtWidgets/QApplication>
-#include <QtWidgets/QTableView>
+#include <QApplication>
+#include <QTableView>
#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 <QBrush>
-#include <QTime>
#include "mymodel.h"
+#include <QTime>
+
//! [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 <QtWidgets/QApplication>
-#include <QtWidgets/QTableView>
+#include <QApplication>
+#include <QTableView>
#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 <QtWidgets/QApplication>
+#include <QApplication>
#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 <QTableView>
#include "mainwindow.h"
#include "mymodel.h"
+#include <QTableView>
+
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 <QtWidgets/QMainWindow>
+#include <QMainWindow>
-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 <QtWidgets/QApplication>
+#include <QApplication>
#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 <QTreeView>
#include <QStandardItemModel>
#include <QStandardItem>
-#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<QStandardItem *> preparedRow =prepareRow("first", "second", "third");
+ QList<QStandardItem *> 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<QStandardItem *> secondRow =prepareRow("111", "222", "333");
+ QList<QStandardItem *> 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<QStandardItem *> MainWindow::prepareRow(const QString &first,
- const QString &second,
- const QString &third)
+ const QString &second,
+ const QString &third) const
{
- QList<QStandardItem *> 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 <QtWidgets/QMainWindow>
+#include <QMainWindow>
-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<QStandardItem *> prepareRow( const QString &first,
- const QString &second,
- const QString &third );
+ QList<QStandardItem *> 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 <QtWidgets/QApplication>
+#include <QApplication>
#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 <QTreeView>
#include <QStandardItemModel>
#include <QItemSelectionModel>
-#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 <QtWidgets/QMainWindow>
+#include <QMainWindow>
-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);
};