summaryrefslogtreecommitdiffstats
path: root/examples/widgets/itemviews
diff options
context:
space:
mode:
Diffstat (limited to 'examples/widgets/itemviews')
-rw-r--r--examples/widgets/itemviews/addressbook/adddialog.cpp38
-rw-r--r--examples/widgets/itemviews/addressbook/adddialog.h14
-rw-r--r--examples/widgets/itemviews/addressbook/addresswidget.cpp69
-rw-r--r--examples/widgets/itemviews/addressbook/addresswidget.h5
-rw-r--r--examples/widgets/itemviews/addressbook/mainwindow.cpp18
-rw-r--r--examples/widgets/itemviews/addressbook/mainwindow.h6
-rw-r--r--examples/widgets/itemviews/addressbook/newaddresstab.cpp21
-rw-r--r--examples/widgets/itemviews/addressbook/newaddresstab.h10
-rw-r--r--examples/widgets/itemviews/addressbook/tablemodel.cpp52
-rw-r--r--examples/widgets/itemviews/addressbook/tablemodel.h10
-rw-r--r--examples/widgets/itemviews/chart/mainwindow.cpp18
-rw-r--r--examples/widgets/itemviews/chart/mainwindow.h8
-rw-r--r--examples/widgets/itemviews/chart/pieview.cpp56
-rw-r--r--examples/widgets/itemviews/chart/pieview.h14
-rw-r--r--examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp10
-rw-r--r--examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h4
-rw-r--r--examples/widgets/itemviews/coloreditorfactory/window.cpp17
-rw-r--r--examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp5
-rw-r--r--examples/widgets/itemviews/customsortfiltermodel/filterwidget.h2
-rw-r--r--examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp6
-rw-r--r--examples/widgets/itemviews/customsortfiltermodel/window.cpp13
-rw-r--r--examples/widgets/itemviews/editabletreemodel/main.cpp4
-rw-r--r--examples/widgets/itemviews/editabletreemodel/mainwindow.cpp32
-rw-r--r--examples/widgets/itemviews/editabletreemodel/mainwindow.h3
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treeitem.cpp22
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treeitem.h5
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treemodel.cpp66
-rw-r--r--examples/widgets/itemviews/editabletreemodel/treemodel.h2
-rw-r--r--examples/widgets/itemviews/fetchmore/filelistmodel.cpp25
-rw-r--r--examples/widgets/itemviews/fetchmore/filelistmodel.h3
-rw-r--r--examples/widgets/itemviews/fetchmore/window.cpp4
-rw-r--r--examples/widgets/itemviews/fetchmore/window.h2
-rw-r--r--examples/widgets/itemviews/interview/model.cpp3
-rw-r--r--examples/widgets/itemviews/interview/model.h4
-rw-r--r--examples/widgets/itemviews/pixelator/mainwindow.cpp2
-rw-r--r--examples/widgets/itemviews/pixelator/pixeldelegate.cpp14
-rw-r--r--examples/widgets/itemviews/pixelator/pixeldelegate.h6
-rw-r--r--examples/widgets/itemviews/puzzle/piecesmodel.cpp9
-rw-r--r--examples/widgets/itemviews/puzzle/piecesmodel.h10
-rw-r--r--examples/widgets/itemviews/puzzle/puzzlewidget.cpp2
-rw-r--r--examples/widgets/itemviews/simpledommodel/domitem.cpp30
-rw-r--r--examples/widgets/itemviews/simpledommodel/domitem.h6
-rw-r--r--examples/widgets/itemviews/simpledommodel/dommodel.cpp40
-rw-r--r--examples/widgets/itemviews/simpledommodel/dommodel.h2
-rw-r--r--examples/widgets/itemviews/simpledommodel/mainwindow.cpp9
-rw-r--r--examples/widgets/itemviews/simpledommodel/mainwindow.h2
-rw-r--r--examples/widgets/itemviews/simpletreemodel/treeitem.cpp18
-rw-r--r--examples/widgets/itemviews/simpletreemodel/treeitem.h8
-rw-r--r--examples/widgets/itemviews/simpletreemodel/treemodel.cpp35
-rw-r--r--examples/widgets/itemviews/simpletreemodel/treemodel.h2
-rw-r--r--examples/widgets/itemviews/simplewidgetmapper/window.h2
-rw-r--r--examples/widgets/itemviews/spinboxdelegate/delegate.cpp7
-rw-r--r--examples/widgets/itemviews/spinboxdelegate/delegate.h6
-rw-r--r--examples/widgets/itemviews/spreadsheet/main.cpp3
-rw-r--r--examples/widgets/itemviews/spreadsheet/printview.cpp5
-rw-r--r--examples/widgets/itemviews/spreadsheet/spreadsheet.cpp92
-rw-r--r--examples/widgets/itemviews/spreadsheet/spreadsheet.h4
-rw-r--r--examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp12
-rw-r--r--examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h4
-rw-r--r--examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp35
-rw-r--r--examples/widgets/itemviews/spreadsheet/spreadsheetitem.h7
-rw-r--r--examples/widgets/itemviews/stardelegate/main.cpp5
-rw-r--r--examples/widgets/itemviews/stardelegate/stardelegate.cpp8
-rw-r--r--examples/widgets/itemviews/stardelegate/stardelegate.h3
-rw-r--r--examples/widgets/itemviews/stardelegate/stareditor.cpp20
-rw-r--r--examples/widgets/itemviews/stardelegate/stareditor.h5
-rw-r--r--examples/widgets/itemviews/stardelegate/starrating.cpp27
-rw-r--r--examples/widgets/itemviews/stardelegate/starrating.h8
-rw-r--r--examples/widgets/itemviews/storageview/main.cpp7
-rw-r--r--examples/widgets/itemviews/storageview/storagemodel.cpp8
-rw-r--r--examples/widgets/itemviews/storageview/storagemodel.h2
71 files changed, 486 insertions, 550 deletions
diff --git a/examples/widgets/itemviews/addressbook/adddialog.cpp b/examples/widgets/itemviews/addressbook/adddialog.cpp
index 1ce2d3110a..bf87307253 100644
--- a/examples/widgets/itemviews/addressbook/adddialog.cpp
+++ b/examples/widgets/itemviews/addressbook/adddialog.cpp
@@ -54,17 +54,16 @@
//! [0]
AddDialog::AddDialog(QWidget *parent)
- : QDialog(parent)
+ : QDialog(parent),
+ nameText(new QLineEdit),
+ addressText(new QTextEdit)
{
- nameLabel = new QLabel("Name");
- addressLabel = new QLabel("Address");
- okButton = new QPushButton("OK");
- cancelButton = new QPushButton("Cancel");
+ auto nameLabel = new QLabel(tr("Name"));
+ auto addressLabel = new QLabel(tr("Address"));
+ auto okButton = new QPushButton(tr("OK"));
+ auto cancelButton = new QPushButton(tr("Cancel"));
- nameText = new QLineEdit;
- addressText = new QTextEdit;
-
- QGridLayout *gLayout = new QGridLayout;
+ auto gLayout = new QGridLayout;
gLayout->setColumnStretch(1, 2);
gLayout->addWidget(nameLabel, 0, 0);
gLayout->addWidget(nameText, 0, 1);
@@ -72,13 +71,13 @@ AddDialog::AddDialog(QWidget *parent)
gLayout->addWidget(addressLabel, 1, 0, Qt::AlignLeft|Qt::AlignTop);
gLayout->addWidget(addressText, 1, 1, Qt::AlignLeft);
- QHBoxLayout *buttonLayout = new QHBoxLayout;
+ auto buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
gLayout->addLayout(buttonLayout, 2, 1, Qt::AlignRight);
- QVBoxLayout *mainLayout = new QVBoxLayout;
+ auto mainLayout = new QVBoxLayout;
mainLayout->addLayout(gLayout);
setLayout(mainLayout);
@@ -87,4 +86,21 @@ AddDialog::AddDialog(QWidget *parent)
setWindowTitle(tr("Add a Contact"));
}
+
+QString AddDialog::name() const
+{
+ return nameText->text();
+}
+
+QString AddDialog::address() const
+{
+ return addressText->toPlainText();
+}
+
+void AddDialog::editAddress(const QString &name, const QString &address)
+{
+ nameText->setReadOnly(true);
+ nameText->setText(name);
+ addressText->setPlainText(address);
+}
//! [0]
diff --git a/examples/widgets/itemviews/addressbook/adddialog.h b/examples/widgets/itemviews/addressbook/adddialog.h
index 16fa7bc650..2867034122 100644
--- a/examples/widgets/itemviews/addressbook/adddialog.h
+++ b/examples/widgets/itemviews/addressbook/adddialog.h
@@ -66,15 +66,15 @@ class AddDialog : public QDialog
Q_OBJECT
public:
- AddDialog(QWidget *parent = 0);
- QLineEdit *nameText;
- QTextEdit *addressText;
+ AddDialog(QWidget *parent = nullptr);
+
+ QString name() const;
+ QString address() const;
+ void editAddress(const QString &name, const QString &address);
private:
- QLabel *nameLabel;
- QLabel *addressLabel;
- QPushButton *okButton;
- QPushButton *cancelButton;
+ QLineEdit *nameText;
+ QTextEdit *addressText;
};
//! [0]
diff --git a/examples/widgets/itemviews/addressbook/addresswidget.cpp b/examples/widgets/itemviews/addressbook/addresswidget.cpp
index 143f6266dd..b1b65174ee 100644
--- a/examples/widgets/itemviews/addressbook/addresswidget.cpp
+++ b/examples/widgets/itemviews/addressbook/addresswidget.cpp
@@ -48,21 +48,21 @@
**
****************************************************************************/
-#include "adddialog.h"
#include "addresswidget.h"
+#include "adddialog.h"
#include <QtWidgets>
//! [0]
AddressWidget::AddressWidget(QWidget *parent)
- : QTabWidget(parent)
+ : QTabWidget(parent),
+ table(new TableModel(this)),
+ newAddressTab(new NewAddressTab(this))
{
- table = new TableModel(this);
- newAddressTab = new NewAddressTab(this);
connect(newAddressTab, &NewAddressTab::sendDetails,
this, &AddressWidget::addEntry);
- addTab(newAddressTab, "Address Book");
+ addTab(newAddressTab, tr("Address Book"));
setupTabs();
}
@@ -73,17 +73,13 @@ void AddressWidget::showAddEntryDialog()
{
AddDialog aDialog;
- if (aDialog.exec()) {
- QString name = aDialog.nameText->text();
- QString address = aDialog.addressText->toPlainText();
-
- addEntry(name, address);
- }
+ if (aDialog.exec())
+ addEntry(aDialog.name(), aDialog.address());
}
//! [2]
//! [3]
-void AddressWidget::addEntry(QString name, QString address)
+void AddressWidget::addEntry(const QString &name, const QString &address)
{
if (!table->getContacts().contains({ name, address })) {
table->insertRows(0, 1, QModelIndex());
@@ -107,12 +103,12 @@ void AddressWidget::editEntry()
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
QItemSelectionModel *selectionModel = temp->selectionModel();
- QModelIndexList indexes = selectionModel->selectedRows();
+ const QModelIndexList indexes = selectionModel->selectedRows();
QString name;
QString address;
int row = -1;
- foreach (QModelIndex index, indexes) {
+ for (const QModelIndex &index : indexes) {
row = proxy->mapToSource(index).row();
QModelIndex nameIndex = table->index(row, 0, QModelIndex());
QVariant varName = table->data(nameIndex, Qt::DisplayRole);
@@ -127,15 +123,12 @@ void AddressWidget::editEntry()
//! [4b]
AddDialog aDialog;
aDialog.setWindowTitle(tr("Edit a Contact"));
-
- aDialog.nameText->setReadOnly(true);
- aDialog.nameText->setText(name);
- aDialog.addressText->setText(address);
+ aDialog.editAddress(name, address);
if (aDialog.exec()) {
- QString newAddress = aDialog.addressText->toPlainText();
+ const QString newAddress = aDialog.address();
if (newAddress != address) {
- QModelIndex index = table->index(row, 1, QModelIndex());
+ const QModelIndex index = table->index(row, 1, QModelIndex());
table->setData(index, newAddress, Qt::EditRole);
}
}
@@ -149,52 +142,46 @@ void AddressWidget::removeEntry()
QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
QItemSelectionModel *selectionModel = temp->selectionModel();
- QModelIndexList indexes = selectionModel->selectedRows();
+ const QModelIndexList indexes = selectionModel->selectedRows();
- foreach (QModelIndex index, indexes) {
+ for (QModelIndex index : indexes) {
int row = proxy->mapToSource(index).row();
table->removeRows(row, 1, QModelIndex());
}
- if (table->rowCount(QModelIndex()) == 0) {
- insertTab(0, newAddressTab, "Address Book");
- }
+ if (table->rowCount(QModelIndex()) == 0)
+ insertTab(0, newAddressTab, tr("Address Book"));
}
//! [5]
//! [1]
void AddressWidget::setupTabs()
{
- QStringList groups;
- groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ";
+ const auto groups = { "ABC", "DEF", "GHI", "JKL", "MNO", "PQR", "STU", "VW", "XYZ" };
- for (int i = 0; i < groups.size(); ++i) {
- QString str = groups.at(i);
- QString regExp = QString("^[%1].*").arg(str);
+ for (const QString &str : groups) {
+ const auto regExp = QRegularExpression(QString("^[%1].*").arg(str),
+ QRegularExpression::CaseInsensitiveOption);
- proxyModel = new QSortFilterProxyModel(this);
+ auto proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(table);
- proxyModel->setFilterRegExp(QRegExp(regExp, Qt::CaseInsensitive));
+ proxyModel->setFilterRegularExpression(regExp);
proxyModel->setFilterKeyColumn(0);
QTableView *tableView = new QTableView;
tableView->setModel(proxyModel);
-
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->horizontalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->hide();
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
-
tableView->setSortingEnabled(true);
- connect(tableView->selectionModel(),
- &QItemSelectionModel::selectionChanged,
- this, &AddressWidget::selectionChanged);
+ connect(tableView->selectionModel(), &QItemSelectionModel::selectionChanged,
+ this, &AddressWidget::selectionChanged);
- connect(this, &QTabWidget::currentChanged, this, [this](int tabIndex) {
- auto *tableView = qobject_cast<QTableView *>(widget(tabIndex));
- if (tableView)
+ connect(this, &QTabWidget::currentChanged, this, [this, tableView](int tabIndex) {
+ if (widget(tabIndex) == tableView)
emit selectionChanged(tableView->selectionModel()->selection());
});
@@ -214,7 +201,7 @@ void AddressWidget::readFromFile(const QString &fileName)
return;
}
- QList<Contact> contacts;
+ QVector<Contact> contacts;
QDataStream in(&file);
in >> contacts;
diff --git a/examples/widgets/itemviews/addressbook/addresswidget.h b/examples/widgets/itemviews/addressbook/addresswidget.h
index de5a2da766..111ea98328 100644
--- a/examples/widgets/itemviews/addressbook/addresswidget.h
+++ b/examples/widgets/itemviews/addressbook/addresswidget.h
@@ -68,13 +68,13 @@ class AddressWidget : public QTabWidget
Q_OBJECT
public:
- AddressWidget(QWidget *parent = 0);
+ AddressWidget(QWidget *parent = nullptr);
void readFromFile(const QString &fileName);
void writeToFile(const QString &fileName);
public slots:
void showAddEntryDialog();
- void addEntry(QString name, QString address);
+ void addEntry(const QString &name, const QString &address);
void editEntry();
void removeEntry();
@@ -86,7 +86,6 @@ private:
TableModel *table;
NewAddressTab *newAddressTab;
- QSortFilterProxyModel *proxyModel;
};
//! [0]
diff --git a/examples/widgets/itemviews/addressbook/mainwindow.cpp b/examples/widgets/itemviews/addressbook/mainwindow.cpp
index 6d8b15b032..42966de375 100644
--- a/examples/widgets/itemviews/addressbook/mainwindow.cpp
+++ b/examples/widgets/itemviews/addressbook/mainwindow.cpp
@@ -56,8 +56,9 @@
//! [0]
MainWindow::MainWindow()
+ : QMainWindow(),
+ addressWidget(new AddressWidget)
{
- addressWidget = new AddressWidget;
setCentralWidget(addressWidget);
createMenus();
setWindowTitle(tr("Address Book"));
@@ -67,28 +68,29 @@ MainWindow::MainWindow()
//! [1a]
void MainWindow::createMenus()
{
- fileMenu = menuBar()->addMenu(tr("&File"));
+ QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
- openAct = new QAction(tr("&Open..."), this);
+ QAction *openAct = new QAction(tr("&Open..."), this);
fileMenu->addAction(openAct);
connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
//! [1a]
- saveAct = new QAction(tr("&Save As..."), this);
+ QAction *saveAct = new QAction(tr("&Save As..."), this);
fileMenu->addAction(saveAct);
connect(saveAct, &QAction::triggered, this, &MainWindow::saveFile);
fileMenu->addSeparator();
- exitAct = new QAction(tr("E&xit"), this);
+ QAction *exitAct = new QAction(tr("E&xit"), this);
fileMenu->addAction(exitAct);
connect(exitAct, &QAction::triggered, this, &QWidget::close);
- toolMenu = menuBar()->addMenu(tr("&Tools"));
+ QMenu *toolMenu = menuBar()->addMenu(tr("&Tools"));
- addAct = new QAction(tr("&Add Entry..."), this);
+ QAction *addAct = new QAction(tr("&Add Entry..."), this);
toolMenu->addAction(addAct);
- connect(addAct, &QAction::triggered, addressWidget, &AddressWidget::showAddEntryDialog);
+ connect(addAct, &QAction::triggered,
+ addressWidget, &AddressWidget::showAddEntryDialog);
//! [1b]
editAct = new QAction(tr("&Edit Entry..."), this);
diff --git a/examples/widgets/itemviews/addressbook/mainwindow.h b/examples/widgets/itemviews/addressbook/mainwindow.h
index 3d323e5799..fca5934e68 100644
--- a/examples/widgets/itemviews/addressbook/mainwindow.h
+++ b/examples/widgets/itemviews/addressbook/mainwindow.h
@@ -72,12 +72,6 @@ private:
void createMenus();
AddressWidget *addressWidget;
- QMenu *fileMenu;
- QMenu *toolMenu;
- QAction *openAct;
- QAction *saveAct;
- QAction *exitAct;
- QAction *addAct;
QAction *editAct;
QAction *removeAct;
};
diff --git a/examples/widgets/itemviews/addressbook/newaddresstab.cpp b/examples/widgets/itemviews/addressbook/newaddresstab.cpp
index f0a7369796..ef725d723f 100644
--- a/examples/widgets/itemviews/addressbook/newaddresstab.cpp
+++ b/examples/widgets/itemviews/addressbook/newaddresstab.cpp
@@ -48,24 +48,23 @@
**
****************************************************************************/
-#include "adddialog.h"
#include "newaddresstab.h"
+#include "adddialog.h"
#include <QtWidgets>
//! [0]
NewAddressTab::NewAddressTab(QWidget *parent)
+ : QWidget(parent)
{
- Q_UNUSED(parent);
+ auto descriptionLabel = new QLabel(tr("There are currently no contacts in your address book. "
+ "\nClick Add to add new contacts."));
- descriptionLabel = new QLabel(tr("There are currently no contacts in your address book. "
- "\nClick Add to add new contacts."));
-
- addButton = new QPushButton(tr("Add"));
+ auto addButton = new QPushButton(tr("Add"));
connect(addButton, &QAbstractButton::clicked, this, &NewAddressTab::addEntry);
- mainLayout = new QVBoxLayout;
+ auto mainLayout = new QVBoxLayout;
mainLayout->addWidget(descriptionLabel);
mainLayout->addWidget(addButton, 0, Qt::AlignCenter);
@@ -78,11 +77,7 @@ void NewAddressTab::addEntry()
{
AddDialog aDialog;
- if (aDialog.exec()) {
- QString name = aDialog.nameText->text();
- QString address = aDialog.addressText->toPlainText();
-
- emit sendDetails(name, address);
- }
+ if (aDialog.exec())
+ emit sendDetails(aDialog.name(), aDialog.address());
}
//! [1]
diff --git a/examples/widgets/itemviews/addressbook/newaddresstab.h b/examples/widgets/itemviews/addressbook/newaddresstab.h
index 05c7a8dada..6a147e3c7c 100644
--- a/examples/widgets/itemviews/addressbook/newaddresstab.h
+++ b/examples/widgets/itemviews/addressbook/newaddresstab.h
@@ -65,19 +65,13 @@ class NewAddressTab : public QWidget
Q_OBJECT
public:
- NewAddressTab(QWidget *parent = 0);
+ NewAddressTab(QWidget *parent = nullptr);
public slots:
void addEntry();
signals:
- void sendDetails(QString name, QString address);
-
-private:
- QLabel *descriptionLabel;
- QPushButton *addButton;
- QVBoxLayout *mainLayout;
-
+ void sendDetails(const QString &name, const QString &address);
};
//! [0]
diff --git a/examples/widgets/itemviews/addressbook/tablemodel.cpp b/examples/widgets/itemviews/addressbook/tablemodel.cpp
index 674e312753..ddf79a3fa3 100644
--- a/examples/widgets/itemviews/addressbook/tablemodel.cpp
+++ b/examples/widgets/itemviews/addressbook/tablemodel.cpp
@@ -56,9 +56,9 @@ TableModel::TableModel(QObject *parent)
{
}
-TableModel::TableModel(QList<Contact> contacts, QObject *parent)
- : QAbstractTableModel(parent)
- , contacts(contacts)
+TableModel::TableModel(const QVector<Contact> &contacts, QObject *parent)
+ : QAbstractTableModel(parent),
+ contacts(contacts)
{
}
//! [0]
@@ -66,14 +66,12 @@ TableModel::TableModel(QList<Contact> contacts, QObject *parent)
//! [1]
int TableModel::rowCount(const QModelIndex &parent) const
{
- Q_UNUSED(parent);
- return contacts.size();
+ return parent.isValid() ? 0 : contacts.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
- Q_UNUSED(parent);
- return 2;
+ return parent.isValid() ? 0 : 2;
}
//! [1]
@@ -89,10 +87,14 @@ QVariant TableModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole) {
const auto &contact = contacts.at(index.row());
- if (index.column() == 0)
- return contact.name;
- else if (index.column() == 1)
- return contact.address;
+ switch (index.column()) {
+ case 0:
+ return contact.name;
+ case 1:
+ return contact.address;
+ default:
+ break;
+ }
}
return QVariant();
}
@@ -108,12 +110,10 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
switch (section) {
case 0:
return tr("Name");
-
case 1:
return tr("Address");
-
default:
- return QVariant();
+ break;
}
}
return QVariant();
@@ -152,19 +152,21 @@ bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
- int row = index.row();
-
+ const int row = index.row();
auto contact = contacts.value(row);
- if (index.column() == 0)
- contact.name = value.toString();
- else if (index.column() == 1)
- contact.address = value.toString();
- else
- return false;
-
+ switch (index.column()) {
+ case 0:
+ contact.name = value.toString();
+ break;
+ case 1:
+ contact.address = value.toString();
+ break;
+ default:
+ return false;
+ }
contacts.replace(row, contact);
- emit(dataChanged(index, index));
+ emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
return true;
}
@@ -184,7 +186,7 @@ Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
//! [7]
//! [8]
-QList<Contact> TableModel::getContacts() const
+const QVector<Contact> &TableModel::getContacts() const
{
return contacts;
}
diff --git a/examples/widgets/itemviews/addressbook/tablemodel.h b/examples/widgets/itemviews/addressbook/tablemodel.h
index 1004a35d31..e4025fc734 100644
--- a/examples/widgets/itemviews/addressbook/tablemodel.h
+++ b/examples/widgets/itemviews/addressbook/tablemodel.h
@@ -52,7 +52,7 @@
#define TABLEMODEL_H
#include <QAbstractTableModel>
-#include <QList>
+#include <QVector>
//! [0]
@@ -82,8 +82,8 @@ class TableModel : public QAbstractTableModel
Q_OBJECT
public:
- TableModel(QObject *parent = 0);
- TableModel(QList<Contact> contacts, QObject *parent = 0);
+ TableModel(QObject *parent = nullptr);
+ TableModel(const QVector<Contact> &contacts, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
@@ -93,10 +93,10 @@ public:
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
- QList<Contact> getContacts() const;
+ const QVector<Contact> &getContacts() const;
private:
- QList<Contact> contacts;
+ QVector<Contact> contacts;
};
//! [0]
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 <QtWidgets>
-
#include "pieview.h"
#include "mainwindow.h"
-MainWindow::MainWindow()
+#include <QtWidgets>
+
+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 <QtWidgets>
-#include <qmath.h>
-#include <cmath>
#include "pieview.h"
+#include <QtWidgets>
+
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<int> &)
+ const QVector<int> &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/coloreditorfactory/colorlisteditor.cpp b/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp
index 75d809a44f..0b65c53477 100644
--- a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp
+++ b/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.cpp
@@ -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]);
diff --git a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h b/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h
index f284c07c89..f9d736d298 100644
--- a/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h
+++ b/examples/widgets/itemviews/coloreditorfactory/colorlisteditor.h
@@ -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();
diff --git a/examples/widgets/itemviews/coloreditorfactory/window.cpp b/examples/widgets/itemviews/coloreditorfactory/window.cpp
index f7ce821d31..e4a9379d8f 100644
--- a/examples/widgets/itemviews/coloreditorfactory/window.cpp
+++ b/examples/widgets/itemviews/coloreditorfactory/window.cpp
@@ -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;
diff --git a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp
index 74d88caae6..302042a6b3 100644
--- a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp
+++ b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.cpp
@@ -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;
diff --git a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h
index ea12954e52..70214b862e 100644
--- a/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h
+++ b/examples/widgets/itemviews/customsortfiltermodel/filterwidget.h
@@ -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);
diff --git a/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp b/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp
index 35426657d9..4753d04d9b 100644
--- a/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp
+++ b/examples/widgets/itemviews/customsortfiltermodel/mysortfilterproxymodel.cpp
@@ -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);
diff --git a/examples/widgets/itemviews/customsortfiltermodel/window.cpp b/examples/widgets/itemviews/customsortfiltermodel/window.cpp
index bfd2350da0..3356c971ad 100644
--- a/examples/widgets/itemviews/customsortfiltermodel/window.cpp
+++ b/examples/widgets/itemviews/customsortfiltermodel/window.cpp
@@ -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]
diff --git a/examples/widgets/itemviews/editabletreemodel/main.cpp b/examples/widgets/itemviews/editabletreemodel/main.cpp
index 80f6842730..5e9ff214a3 100644
--- a/examples/widgets/itemviews/editabletreemodel/main.cpp
+++ b/examples/widgets/itemviews/editabletreemodel/main.cpp
@@ -48,10 +48,10 @@
**
****************************************************************************/
-#include <QApplication>
-
#include "mainwindow.h"
+#include <QApplication>
+
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(editabletreemodel);
diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
index 0c54d8930f..7faa325469 100644
--- a/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
+++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.cpp
@@ -58,8 +58,7 @@ MainWindow::MainWindow(QWidget *parent)
{
setupUi(this);
- QStringList headers;
- headers << tr("Title") << tr("Description");
+ const QStringList headers({tr("Title"), tr("Description")});
QFile file(":/default.txt");
file.open(QIODevice::ReadOnly);
@@ -87,7 +86,7 @@ MainWindow::MainWindow(QWidget *parent)
void MainWindow::insertChild()
{
- QModelIndex index = view->selectionModel()->currentIndex();
+ const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (model->columnCount(index) == 0) {
@@ -99,10 +98,10 @@ void MainWindow::insertChild()
return;
for (int column = 0; column < model->columnCount(index); ++column) {
- QModelIndex child = model->index(0, column, index);
- model->setData(child, QVariant("[No data]"), Qt::EditRole);
+ const QModelIndex child = model->index(0, column, index);
+ model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
if (!model->headerData(column, Qt::Horizontal).isValid())
- model->setHeaderData(column, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
+ model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole);
}
view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
@@ -127,7 +126,7 @@ bool MainWindow::insertColumn()
void MainWindow::insertRow()
{
- QModelIndex index = view->selectionModel()->currentIndex();
+ const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (!model->insertRow(index.row()+1, index.parent()))
@@ -136,19 +135,18 @@ void MainWindow::insertRow()
updateActions();
for (int column = 0; column < model->columnCount(index.parent()); ++column) {
- QModelIndex child = model->index(index.row()+1, column, index.parent());
- model->setData(child, QVariant("[No data]"), Qt::EditRole);
+ const QModelIndex child = model->index(index.row() + 1, column, index.parent());
+ model->setData(child, QVariant(tr("[No data]")), Qt::EditRole);
}
}
bool MainWindow::removeColumn()
{
QAbstractItemModel *model = view->model();
- int column = view->selectionModel()->currentIndex().column();
+ const int column = view->selectionModel()->currentIndex().column();
// Insert columns in each child of the parent item.
- bool changed = model->removeColumn(column);
-
+ const bool changed = model->removeColumn(column);
if (changed)
updateActions();
@@ -157,7 +155,7 @@ bool MainWindow::removeColumn()
void MainWindow::removeRow()
{
- QModelIndex index = view->selectionModel()->currentIndex();
+ const QModelIndex index = view->selectionModel()->currentIndex();
QAbstractItemModel *model = view->model();
if (model->removeRow(index.row(), index.parent()))
updateActions();
@@ -165,19 +163,19 @@ void MainWindow::removeRow()
void MainWindow::updateActions()
{
- bool hasSelection = !view->selectionModel()->selection().isEmpty();
+ const bool hasSelection = !view->selectionModel()->selection().isEmpty();
removeRowAction->setEnabled(hasSelection);
removeColumnAction->setEnabled(hasSelection);
- bool hasCurrent = view->selectionModel()->currentIndex().isValid();
+ const bool hasCurrent = view->selectionModel()->currentIndex().isValid();
insertRowAction->setEnabled(hasCurrent);
insertColumnAction->setEnabled(hasCurrent);
if (hasCurrent) {
view->closePersistentEditor(view->selectionModel()->currentIndex());
- int row = view->selectionModel()->currentIndex().row();
- int column = view->selectionModel()->currentIndex().column();
+ const int row = view->selectionModel()->currentIndex().row();
+ const int column = view->selectionModel()->currentIndex().column();
if (view->selectionModel()->currentIndex().parent().isValid())
statusBar()->showMessage(tr("Position: (%1,%2)").arg(row).arg(column));
else
diff --git a/examples/widgets/itemviews/editabletreemodel/mainwindow.h b/examples/widgets/itemviews/editabletreemodel/mainwindow.h
index dbc745e25d..314b0cb1fa 100644
--- a/examples/widgets/itemviews/editabletreemodel/mainwindow.h
+++ b/examples/widgets/itemviews/editabletreemodel/mainwindow.h
@@ -54,14 +54,13 @@
#include "ui_mainwindow.h"
#include <QMainWindow>
-#include <QModelIndex>
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
- MainWindow(QWidget *parent = 0);
+ MainWindow(QWidget *parent = nullptr);
public slots:
void updateActions();
diff --git a/examples/widgets/itemviews/editabletreemodel/treeitem.cpp b/examples/widgets/itemviews/editabletreemodel/treeitem.cpp
index 8fbe86ad1e..027a5f8849 100644
--- a/examples/widgets/itemviews/editabletreemodel/treeitem.cpp
+++ b/examples/widgets/itemviews/editabletreemodel/treeitem.cpp
@@ -56,14 +56,11 @@
#include "treeitem.h"
-#include <QStringList>
-
//! [0]
TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
-{
- parentItem = parent;
- itemData = data;
-}
+ : itemData(data),
+ parentItem(parent)
+{}
//! [0]
//! [1]
@@ -76,7 +73,9 @@ TreeItem::~TreeItem()
//! [2]
TreeItem *TreeItem::child(int number)
{
- return childItems.value(number);
+ if (number < 0 || number >= childItems.size())
+ return nullptr;
+ return childItems.at(number);
}
//! [2]
@@ -92,7 +91,6 @@ int TreeItem::childNumber() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
-
return 0;
}
//! [4]
@@ -107,7 +105,9 @@ int TreeItem::columnCount() const
//! [6]
QVariant TreeItem::data(int column) const
{
- return itemData.value(column);
+ if (column < 0 || column >= itemData.size())
+ return QVariant();
+ return itemData.at(column);
}
//! [6]
@@ -136,7 +136,7 @@ bool TreeItem::insertColumns(int position, int columns)
for (int column = 0; column < columns; ++column)
itemData.insert(position, QVariant());
- foreach (TreeItem *child, childItems)
+ for (TreeItem *child : qAsConst(childItems))
child->insertColumns(position, columns);
return true;
@@ -171,7 +171,7 @@ bool TreeItem::removeColumns(int position, int columns)
for (int column = 0; column < columns; ++column)
itemData.remove(position);
- foreach (TreeItem *child, childItems)
+ for (TreeItem *child : qAsConst(childItems))
child->removeColumns(position, columns);
return true;
diff --git a/examples/widgets/itemviews/editabletreemodel/treeitem.h b/examples/widgets/itemviews/editabletreemodel/treeitem.h
index fd65f12268..867be5f380 100644
--- a/examples/widgets/itemviews/editabletreemodel/treeitem.h
+++ b/examples/widgets/itemviews/editabletreemodel/treeitem.h
@@ -51,7 +51,6 @@
#ifndef TREEITEM_H
#define TREEITEM_H
-#include <QList>
#include <QVariant>
#include <QVector>
@@ -59,7 +58,7 @@
class TreeItem
{
public:
- explicit TreeItem(const QVector<QVariant> &data, TreeItem *parent = 0);
+ explicit TreeItem(const QVector<QVariant> &data, TreeItem *parent = nullptr);
~TreeItem();
TreeItem *child(int number);
@@ -75,7 +74,7 @@ public:
bool setData(int column, const QVariant &value);
private:
- QList<TreeItem*> childItems;
+ QVector<TreeItem*> childItems;
QVector<QVariant> itemData;
TreeItem *parentItem;
};
diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
index dbd53df1e6..836c2e8651 100644
--- a/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
+++ b/examples/widgets/itemviews/editabletreemodel/treemodel.cpp
@@ -48,21 +48,21 @@
**
****************************************************************************/
-#include <QtWidgets>
-
-#include "treeitem.h"
#include "treemodel.h"
+#include "treeitem.h"
+
+#include <QtWidgets>
//! [0]
TreeModel::TreeModel(const QStringList &headers, const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
QVector<QVariant> rootData;
- foreach (QString header, headers)
+ for (const QString &header : headers)
rootData << header;
rootItem = new TreeItem(rootData);
- setupModelData(data.split(QString("\n")), rootItem);
+ setupModelData(data.split('\n'), rootItem);
}
//! [0]
@@ -74,8 +74,9 @@ TreeModel::~TreeModel()
//! [1]
//! [2]
-int TreeModel::columnCount(const QModelIndex & /* parent */) const
+int TreeModel::columnCount(const QModelIndex &parent) const
{
+ Q_UNUSED(parent);
return rootItem->columnCount();
}
//! [2]
@@ -97,7 +98,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
- return 0;
+ return Qt::NoItemFlags;
return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
}
@@ -133,21 +134,20 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con
//! [6]
TreeItem *parentItem = getItem(parent);
+ if (!parentItem)
+ return QModelIndex();
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
- else
- return QModelIndex();
+ return QModelIndex();
}
//! [6]
bool TreeModel::insertColumns(int position, int columns, const QModelIndex &parent)
{
- bool success;
-
beginInsertColumns(parent, position, position + columns - 1);
- success = rootItem->insertColumns(position, columns);
+ const bool success = rootItem->insertColumns(position, columns);
endInsertColumns();
return success;
@@ -156,10 +156,13 @@ bool TreeModel::insertColumns(int position, int columns, const QModelIndex &pare
bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
- bool success;
+ if (!parentItem)
+ return false;
beginInsertRows(parent, position, position + rows - 1);
- success = parentItem->insertChildren(position, rows, rootItem->columnCount());
+ const bool success = parentItem->insertChildren(position,
+ rows,
+ rootItem->columnCount());
endInsertRows();
return success;
@@ -172,9 +175,9 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
return QModelIndex();
TreeItem *childItem = getItem(index);
- TreeItem *parentItem = childItem->parent();
+ TreeItem *parentItem = childItem ? childItem->parent() : nullptr;
- if (parentItem == rootItem)
+ if (parentItem == rootItem || !parentItem)
return QModelIndex();
return createIndex(parentItem->childNumber(), 0, parentItem);
@@ -183,10 +186,8 @@ QModelIndex TreeModel::parent(const QModelIndex &index) const
bool TreeModel::removeColumns(int position, int columns, const QModelIndex &parent)
{
- bool success;
-
beginRemoveColumns(parent, position, position + columns - 1);
- success = rootItem->removeColumns(position, columns);
+ const bool success = rootItem->removeColumns(position, columns);
endRemoveColumns();
if (rootItem->columnCount() == 0)
@@ -198,10 +199,11 @@ bool TreeModel::removeColumns(int position, int columns, const QModelIndex &pare
bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
{
TreeItem *parentItem = getItem(parent);
- bool success = true;
+ if (!parentItem)
+ return false;
beginRemoveRows(parent, position, position + rows - 1);
- success = parentItem->removeChildren(position, rows);
+ const bool success = parentItem->removeChildren(position, rows);
endRemoveRows();
return success;
@@ -210,9 +212,9 @@ bool TreeModel::removeRows(int position, int rows, const QModelIndex &parent)
//! [8]
int TreeModel::rowCount(const QModelIndex &parent) const
{
- TreeItem *parentItem = getItem(parent);
+ const TreeItem *parentItem = getItem(parent);
- return parentItem->childCount();
+ return parentItem ? parentItem->childCount() : 0;
}
//! [8]
@@ -225,7 +227,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, {Qt::DisplayRole, Qt::EditRole});
return result;
}
@@ -236,7 +238,7 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
if (role != Qt::EditRole || orientation != Qt::Horizontal)
return false;
- bool result = rootItem->setData(section, value);
+ const bool result = rootItem->setData(section, value);
if (result)
emit headerDataChanged(orientation, section, section);
@@ -246,8 +248,8 @@ bool TreeModel::setHeaderData(int section, Qt::Orientation orientation,
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
- QList<TreeItem*> parents;
- QList<int> indentations;
+ QVector<TreeItem*> parents;
+ QVector<int> indentations;
parents << parent;
indentations << 0;
@@ -261,14 +263,15 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
++position;
}
- QString lineData = lines[number].mid(position).trimmed();
+ const QString lineData = lines[number].mid(position).trimmed();
if (!lineData.isEmpty()) {
// Read the column data from the rest of the line.
- QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
+ const QStringList columnStrings = lineData.split('\t', QString::SkipEmptyParts);
QVector<QVariant> columnData;
- for (int column = 0; column < columnStrings.count(); ++column)
- columnData << columnStrings[column];
+ columnData.reserve(columnStrings.size());
+ for (const QString &columnString : columnStrings)
+ columnData << columnString;
if (position > indentations.last()) {
// The last child of the current parent is now the new parent
@@ -291,7 +294,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
for (int column = 0; column < columnData.size(); ++column)
parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
}
-
++number;
}
}
diff --git a/examples/widgets/itemviews/editabletreemodel/treemodel.h b/examples/widgets/itemviews/editabletreemodel/treemodel.h
index d76d138831..8419f9cb9d 100644
--- a/examples/widgets/itemviews/editabletreemodel/treemodel.h
+++ b/examples/widgets/itemviews/editabletreemodel/treemodel.h
@@ -64,7 +64,7 @@ class TreeModel : public QAbstractItemModel
public:
TreeModel(const QStringList &headers, const QString &data,
- QObject *parent = 0);
+ QObject *parent = nullptr);
~TreeModel();
//! [0] //! [1]
diff --git a/examples/widgets/itemviews/fetchmore/filelistmodel.cpp b/examples/widgets/itemviews/fetchmore/filelistmodel.cpp
index 086d45eca3..3ee80617c0 100644
--- a/examples/widgets/itemviews/fetchmore/filelistmodel.cpp
+++ b/examples/widgets/itemviews/fetchmore/filelistmodel.cpp
@@ -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;
diff --git a/examples/widgets/itemviews/fetchmore/filelistmodel.h b/examples/widgets/itemviews/fetchmore/filelistmodel.h
index 9251eaf959..35cf6f7b46 100644
--- a/examples/widgets/itemviews/fetchmore/filelistmodel.h
+++ b/examples/widgets/itemviews/fetchmore/filelistmodel.h
@@ -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;
diff --git a/examples/widgets/itemviews/fetchmore/window.cpp b/examples/widgets/itemviews/fetchmore/window.cpp
index 886f76d6ab..d71ac7059d 100644
--- a/examples/widgets/itemviews/fetchmore/window.cpp
+++ b/examples/widgets/itemviews/fetchmore/window.cpp
@@ -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,
diff --git a/examples/widgets/itemviews/fetchmore/window.h b/examples/widgets/itemviews/fetchmore/window.h
index f234683785..61bcb94bde 100644
--- a/examples/widgets/itemviews/fetchmore/window.h
+++ b/examples/widgets/itemviews/fetchmore/window.h
@@ -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);
diff --git a/examples/widgets/itemviews/interview/model.cpp b/examples/widgets/itemviews/interview/model.cpp
index a8f3a6f31e..feaf8bb98c 100644
--- a/examples/widgets/itemviews/interview/model.cpp
+++ b/examples/widgets/itemviews/interview/model.cpp
@@ -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()))
{
}
diff --git a/examples/widgets/itemviews/interview/model.h b/examples/widgets/itemviews/interview/model.h
index 969cf693b2..132f1d01aa 100644
--- a/examples/widgets/itemviews/interview/model.h
+++ b/examples/widgets/itemviews/interview/model.h
@@ -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;
diff --git a/examples/widgets/itemviews/pixelator/mainwindow.cpp b/examples/widgets/itemviews/pixelator/mainwindow.cpp
index 63617f0169..f6b67e4dba 100644
--- a/examples/widgets/itemviews/pixelator/mainwindow.cpp
+++ b/examples/widgets/itemviews/pixelator/mainwindow.cpp
@@ -48,8 +48,8 @@
**
****************************************************************************/
-#include "imagemodel.h"
#include "mainwindow.h"
+#include "imagemodel.h"
#include "pixeldelegate.h"
#include <QtWidgets>
diff --git a/examples/widgets/itemviews/pixelator/pixeldelegate.cpp b/examples/widgets/itemviews/pixelator/pixeldelegate.cpp
index ecf44c1224..dc8736c836 100644
--- a/examples/widgets/itemviews/pixelator/pixeldelegate.cpp
+++ b/examples/widgets/itemviews/pixelator/pixeldelegate.cpp
@@ -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]
diff --git a/examples/widgets/itemviews/pixelator/pixeldelegate.h b/examples/widgets/itemviews/pixelator/pixeldelegate.h
index d968d9e8d2..9f52221a28 100644
--- a/examples/widgets/itemviews/pixelator/pixeldelegate.h
+++ b/examples/widgets/itemviews/pixelator/pixeldelegate.h
@@ -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);
diff --git a/examples/widgets/itemviews/puzzle/piecesmodel.cpp b/examples/widgets/itemviews/puzzle/piecesmodel.cpp
index 28b46836f4..a9c53ed2cd 100644
--- a/examples/widgets/itemviews/puzzle/piecesmodel.cpp
+++ b/examples/widgets/itemviews/puzzle/piecesmodel.cpp
@@ -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);
diff --git a/examples/widgets/itemviews/puzzle/piecesmodel.h b/examples/widgets/itemviews/puzzle/piecesmodel.h
index bd979f4c7e..2a96603707 100644
--- a/examples/widgets/itemviews/puzzle/piecesmodel.h
+++ b/examples/widgets/itemviews/puzzle/piecesmodel.h
@@ -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;
};
diff --git a/examples/widgets/itemviews/puzzle/puzzlewidget.cpp b/examples/widgets/itemviews/puzzle/puzzlewidget.cpp
index 06968da80f..974a972306 100644
--- a/examples/widgets/itemviews/puzzle/puzzlewidget.cpp
+++ b/examples/widgets/itemviews/puzzle/puzzlewidget.cpp
@@ -169,7 +169,7 @@ void PuzzleWidget::mousePressEvent(QMouseEvent *event)
drag->setHotSpot(event->pos() - square.topLeft());
drag->setPixmap(piece.pixmap);
- if (drag->start(Qt::MoveAction) == Qt::IgnoreAction) {
+ if (drag->exec(Qt::MoveAction) == Qt::IgnoreAction) {
pieces.insert(found, piece);
update(targetSquare(event->pos()));
diff --git a/examples/widgets/itemviews/simpledommodel/domitem.cpp b/examples/widgets/itemviews/simpledommodel/domitem.cpp
index c8b35b2e0a..a2dc9530f0 100644
--- a/examples/widgets/itemviews/simpledommodel/domitem.cpp
+++ b/examples/widgets/itemviews/simpledommodel/domitem.cpp
@@ -53,23 +53,20 @@
#include <QtXml>
//! [0]
-DomItem::DomItem(QDomNode &node, int row, DomItem *parent)
-{
- domNode = node;
+DomItem::DomItem(const QDomNode &node, int row, DomItem *parent)
+ : domNode(node),
//! [0]
- // Record the item's location within its parent.
+ // Record the item's location within its parent.
//! [1]
- rowNumber = row;
- parentItem = parent;
-}
+ parentItem(parent),
+ rowNumber(row)
+{}
//! [1]
//! [2]
DomItem::~DomItem()
{
- QHash<int,DomItem*>::iterator it;
- for (it = childItems.begin(); it != childItems.end(); ++it)
- delete it.value();
+ qDeleteAll(childItems);
}
//! [2]
@@ -90,21 +87,22 @@ DomItem *DomItem::parent()
//! [5]
DomItem *DomItem::child(int i)
{
- if (childItems.contains(i))
- return childItems[i];
+ DomItem *childItem = childItems.value(i);
+ if (childItem)
+ return childItem;
+ // if child does not yet exist, create it
if (i >= 0 && i < domNode.childNodes().count()) {
QDomNode childNode = domNode.childNodes().item(i);
- DomItem *childItem = new DomItem(childNode, i, this);
+ childItem = new DomItem(childNode, i, this);
childItems[i] = childItem;
- return childItem;
}
- return 0;
+ return childItem;
}
//! [5]
//! [6]
-int DomItem::row()
+int DomItem::row() const
{
return rowNumber;
}
diff --git a/examples/widgets/itemviews/simpledommodel/domitem.h b/examples/widgets/itemviews/simpledommodel/domitem.h
index b5db16009c..4a74db5139 100644
--- a/examples/widgets/itemviews/simpledommodel/domitem.h
+++ b/examples/widgets/itemviews/simpledommodel/domitem.h
@@ -58,16 +58,16 @@
class DomItem
{
public:
- DomItem(QDomNode &node, int row, DomItem *parent = 0);
+ DomItem(const QDomNode &node, int row, DomItem *parent = nullptr);
~DomItem();
DomItem *child(int i);
DomItem *parent();
QDomNode node() const;
- int row();
+ int row() const;
private:
QDomNode domNode;
- QHash<int,DomItem*> childItems;
+ QHash<int, DomItem *> childItems;
DomItem *parentItem;
int rowNumber;
};
diff --git a/examples/widgets/itemviews/simpledommodel/dommodel.cpp b/examples/widgets/itemviews/simpledommodel/dommodel.cpp
index f9ba0b5801..8e15252ebb 100644
--- a/examples/widgets/itemviews/simpledommodel/dommodel.cpp
+++ b/examples/widgets/itemviews/simpledommodel/dommodel.cpp
@@ -48,16 +48,17 @@
**
****************************************************************************/
-#include "domitem.h"
#include "dommodel.h"
+#include "domitem.h"
#include <QtXml>
//! [0]
-DomModel::DomModel(QDomDocument document, QObject *parent)
- : QAbstractItemModel(parent), domDocument(document)
+DomModel::DomModel(const QDomDocument &document, QObject *parent)
+ : QAbstractItemModel(parent),
+ domDocument(document),
+ rootItem(new DomItem(domDocument, 0))
{
- rootItem = new DomItem(domDocument, 0);
}
//! [0]
@@ -69,8 +70,9 @@ DomModel::~DomModel()
//! [1]
//! [2]
-int DomModel::columnCount(const QModelIndex &/*parent*/) const
+int DomModel::columnCount(const QModelIndex &parent) const
{
+ Q_UNUSED(parent);
return 3;
}
//! [2]
@@ -84,28 +86,31 @@ QVariant DomModel::data(const QModelIndex &index, int role) const
if (role != Qt::DisplayRole)
return QVariant();
- DomItem *item = static_cast<DomItem*>(index.internalPointer());
+ const DomItem *item = static_cast<DomItem*>(index.internalPointer());
- QDomNode node = item->node();
+ const QDomNode node = item->node();
//! [3] //! [4]
- QStringList attributes;
- QDomNamedNodeMap attributeMap = node.attributes();
switch (index.column()) {
case 0:
return node.nodeName();
case 1:
+ {
+ const QDomNamedNodeMap attributeMap = node.attributes();
+ QStringList attributes;
for (int i = 0; i < attributeMap.count(); ++i) {
QDomNode attribute = attributeMap.item(i);
attributes << attribute.nodeName() + "=\""
- +attribute.nodeValue() + '"';
+ + attribute.nodeValue() + '"';
}
return attributes.join(' ');
+ }
case 2:
- return node.nodeValue().split("\n").join(' ');
+ return node.nodeValue().split('\n').join(' ');
default:
- return QVariant();
+ break;
}
+ return QVariant();
}
//! [4]
@@ -113,7 +118,7 @@ QVariant DomModel::data(const QModelIndex &index, int role) const
Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
- return 0;
+ return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
@@ -132,17 +137,15 @@ QVariant DomModel::headerData(int section, Qt::Orientation orientation,
case 2:
return tr("Value");
default:
- return QVariant();
+ break;
}
}
-
return QVariant();
}
//! [6]
//! [7]
-QModelIndex DomModel::index(int row, int column, const QModelIndex &parent)
- const
+QModelIndex DomModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
@@ -159,8 +162,7 @@ QModelIndex DomModel::index(int row, int column, const QModelIndex &parent)
DomItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
- else
- return QModelIndex();
+ return QModelIndex();
}
//! [8]
diff --git a/examples/widgets/itemviews/simpledommodel/dommodel.h b/examples/widgets/itemviews/simpledommodel/dommodel.h
index 411f58295c..a91bb33cf9 100644
--- a/examples/widgets/itemviews/simpledommodel/dommodel.h
+++ b/examples/widgets/itemviews/simpledommodel/dommodel.h
@@ -63,7 +63,7 @@ class DomModel : public QAbstractItemModel
Q_OBJECT
public:
- explicit DomModel(QDomDocument document, QObject *parent = 0);
+ explicit DomModel(const QDomDocument &document, QObject *parent = nullptr);
~DomModel();
QVariant data(const QModelIndex &index, int role) const override;
diff --git a/examples/widgets/itemviews/simpledommodel/mainwindow.cpp b/examples/widgets/itemviews/simpledommodel/mainwindow.cpp
index 7d43d0fc02..86543b07a6 100644
--- a/examples/widgets/itemviews/simpledommodel/mainwindow.cpp
+++ b/examples/widgets/itemviews/simpledommodel/mainwindow.cpp
@@ -48,22 +48,23 @@
**
****************************************************************************/
-#include "dommodel.h"
#include "mainwindow.h"
+#include "dommodel.h"
#include <QDomDocument>
#include <QTreeView>
#include <QMenuBar>
#include <QFileDialog>
-MainWindow::MainWindow() : QMainWindow(), model(0)
+MainWindow::MainWindow(QWidget *parent)
+ : QMainWindow(parent),
+ model(new DomModel(QDomDocument(), this)),
+ view(new QTreeView(this))
{
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(tr("&Open..."), this, &MainWindow::openFile, QKeySequence::Open);
fileMenu->addAction(tr("E&xit"), this, &QWidget::close, QKeySequence::Quit);
- model = new DomModel(QDomDocument(), this);
- view = new QTreeView(this);
view->setModel(model);
setCentralWidget(view);
diff --git a/examples/widgets/itemviews/simpledommodel/mainwindow.h b/examples/widgets/itemviews/simpledommodel/mainwindow.h
index 938a1c54c1..b3f8d77f35 100644
--- a/examples/widgets/itemviews/simpledommodel/mainwindow.h
+++ b/examples/widgets/itemviews/simpledommodel/mainwindow.h
@@ -65,7 +65,7 @@ class MainWindow : public QMainWindow
Q_OBJECT
public:
- MainWindow();
+ MainWindow(QWidget *parent = nullptr);
public slots:
void openFile();
diff --git a/examples/widgets/itemviews/simpletreemodel/treeitem.cpp b/examples/widgets/itemviews/simpletreemodel/treeitem.cpp
index e11639ff67..41fe24e737 100644
--- a/examples/widgets/itemviews/simpletreemodel/treeitem.cpp
+++ b/examples/widgets/itemviews/simpletreemodel/treeitem.cpp
@@ -54,16 +54,12 @@
A container for items of data supplied by the simple tree model.
*/
-#include <QStringList>
-
#include "treeitem.h"
//! [0]
-TreeItem::TreeItem(const QList<QVariant> &data, TreeItem *parent)
-{
- m_parentItem = parent;
- m_itemData = data;
-}
+TreeItem::TreeItem(const QVector<QVariant> &data, TreeItem *parent)
+ : m_itemData(data), m_parentItem(parent)
+{}
//! [0]
//! [1]
@@ -83,7 +79,9 @@ void TreeItem::appendChild(TreeItem *item)
//! [3]
TreeItem *TreeItem::child(int row)
{
- return m_childItems.value(row);
+ if (row < 0 || row >= m_childItems.size())
+ return nullptr;
+ return m_childItems.at(row);
}
//! [3]
@@ -104,7 +102,9 @@ int TreeItem::columnCount() const
//! [6]
QVariant TreeItem::data(int column) const
{
- return m_itemData.value(column);
+ if (column < 0 || column >= m_itemData.size())
+ return QVariant();
+ return m_itemData.at(column);
}
//! [6]
diff --git a/examples/widgets/itemviews/simpletreemodel/treeitem.h b/examples/widgets/itemviews/simpletreemodel/treeitem.h
index 8d0bb49627..390433597d 100644
--- a/examples/widgets/itemviews/simpletreemodel/treeitem.h
+++ b/examples/widgets/itemviews/simpletreemodel/treeitem.h
@@ -51,14 +51,14 @@
#ifndef TREEITEM_H
#define TREEITEM_H
-#include <QList>
#include <QVariant>
+#include <QVector>
//! [0]
class TreeItem
{
public:
- explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = 0);
+ explicit TreeItem(const QVector<QVariant> &data, TreeItem *parentItem = nullptr);
~TreeItem();
void appendChild(TreeItem *child);
@@ -71,8 +71,8 @@ public:
TreeItem *parentItem();
private:
- QList<TreeItem*> m_childItems;
- QList<QVariant> m_itemData;
+ QVector<TreeItem*> m_childItems;
+ QVector<QVariant> m_itemData;
TreeItem *m_parentItem;
};
//! [0]
diff --git a/examples/widgets/itemviews/simpletreemodel/treemodel.cpp b/examples/widgets/itemviews/simpletreemodel/treemodel.cpp
index 1de970d398..cfa65846ed 100644
--- a/examples/widgets/itemviews/simpletreemodel/treemodel.cpp
+++ b/examples/widgets/itemviews/simpletreemodel/treemodel.cpp
@@ -55,8 +55,8 @@
models.
*/
-#include "treeitem.h"
#include "treemodel.h"
+#include "treeitem.h"
#include <QStringList>
@@ -64,10 +64,8 @@
TreeModel::TreeModel(const QString &data, QObject *parent)
: QAbstractItemModel(parent)
{
- QList<QVariant> rootData;
- rootData << "Title" << "Summary";
- rootItem = new TreeItem(rootData);
- setupModelData(data.split(QString("\n")), rootItem);
+ rootItem = new TreeItem({tr("Title"), tr("Summary")});
+ setupModelData(data.split('\n'), rootItem);
}
//! [0]
@@ -83,8 +81,7 @@ int TreeModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<TreeItem*>(parent.internalPointer())->columnCount();
- else
- return rootItem->columnCount();
+ return rootItem->columnCount();
}
//! [2]
@@ -107,7 +104,7 @@ QVariant TreeModel::data(const QModelIndex &index, int role) const
Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
- return 0;
+ return Qt::NoItemFlags;
return QAbstractItemModel::flags(index);
}
@@ -125,8 +122,7 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
//! [5]
//! [6]
-QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
- const
+QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
@@ -141,8 +137,7 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
TreeItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
- else
- return QModelIndex();
+ return QModelIndex();
}
//! [6]
@@ -180,8 +175,8 @@ int TreeModel::rowCount(const QModelIndex &parent) const
void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
{
- QList<TreeItem*> parents;
- QList<int> indentations;
+ QVector<TreeItem*> parents;
+ QVector<int> indentations;
parents << parent;
indentations << 0;
@@ -195,14 +190,15 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
position++;
}
- QString lineData = lines[number].mid(position).trimmed();
+ const QString lineData = lines[number].mid(position).trimmed();
if (!lineData.isEmpty()) {
// Read the column data from the rest of the line.
- QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
- QList<QVariant> columnData;
- for (int column = 0; column < columnStrings.count(); ++column)
- columnData << columnStrings[column];
+ const QStringList columnStrings = lineData.split('\t', QString::SkipEmptyParts);
+ QVector<QVariant> columnData;
+ columnData.reserve(columnStrings.count());
+ for (const QString &columnString : columnStrings)
+ columnData << columnString;
if (position > indentations.last()) {
// The last child of the current parent is now the new parent
@@ -222,7 +218,6 @@ void TreeModel::setupModelData(const QStringList &lines, TreeItem *parent)
// Append a new item to the current parent's list of children.
parents.last()->appendChild(new TreeItem(columnData, parents.last()));
}
-
++number;
}
}
diff --git a/examples/widgets/itemviews/simpletreemodel/treemodel.h b/examples/widgets/itemviews/simpletreemodel/treemodel.h
index 800728fb82..76fd03f112 100644
--- a/examples/widgets/itemviews/simpletreemodel/treemodel.h
+++ b/examples/widgets/itemviews/simpletreemodel/treemodel.h
@@ -63,7 +63,7 @@ class TreeModel : public QAbstractItemModel
Q_OBJECT
public:
- explicit TreeModel(const QString &data, QObject *parent = 0);
+ explicit TreeModel(const QString &data, QObject *parent = nullptr);
~TreeModel();
QVariant data(const QModelIndex &index, int role) const override;
diff --git a/examples/widgets/itemviews/simplewidgetmapper/window.h b/examples/widgets/itemviews/simplewidgetmapper/window.h
index baa087f8ae..d04b528e16 100644
--- a/examples/widgets/itemviews/simplewidgetmapper/window.h
+++ b/examples/widgets/itemviews/simplewidgetmapper/window.h
@@ -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);
diff --git a/examples/widgets/itemviews/spinboxdelegate/delegate.cpp b/examples/widgets/itemviews/spinboxdelegate/delegate.cpp
index e7e03688c3..f0fe524a60 100644
--- a/examples/widgets/itemviews/spinboxdelegate/delegate.cpp
+++ b/examples/widgets/itemviews/spinboxdelegate/delegate.cpp
@@ -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);
}
diff --git a/examples/widgets/itemviews/spinboxdelegate/delegate.h b/examples/widgets/itemviews/spinboxdelegate/delegate.h
index 47ef1555f7..5dbdda9cd0 100644
--- a/examples/widgets/itemviews/spinboxdelegate/delegate.h
+++ b/examples/widgets/itemviews/spinboxdelegate/delegate.h
@@ -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]
diff --git a/examples/widgets/itemviews/spreadsheet/main.cpp b/examples/widgets/itemviews/spreadsheet/main.cpp
index 769edbdf03..548dc8d604 100644
--- a/examples/widgets/itemviews/spreadsheet/main.cpp
+++ b/examples/widgets/itemviews/spreadsheet/main.cpp
@@ -53,7 +53,8 @@
#include <QApplication>
#include <QLayout>
-int main(int argc, char** argv) {
+int main(int argc, char **argv)
+{
Q_INIT_RESOURCE(spreadsheet);
QApplication app(argc, argv);
SpreadSheet sheet(10, 6);
diff --git a/examples/widgets/itemviews/spreadsheet/printview.cpp b/examples/widgets/itemviews/spreadsheet/printview.cpp
index 31424e9070..7db1a6bad9 100644
--- a/examples/widgets/itemviews/spreadsheet/printview.cpp
+++ b/examples/widgets/itemviews/spreadsheet/printview.cpp
@@ -48,12 +48,11 @@
**
****************************************************************************/
+#include "printview.h"
+
#ifndef QT_NO_PRINTER
#include <QPrinter>
#endif
-#include <QStyleOptionViewItem>
-
-#include "printview.h"
PrintView::PrintView()
{
diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp b/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp
index 621f655b02..fc7fbb872c 100644
--- a/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp
+++ b/examples/widgets/itemviews/spreadsheet/spreadsheet.cpp
@@ -48,36 +48,30 @@
**
****************************************************************************/
-#include <QtWidgets>
-#if defined(QT_PRINTSUPPORT_LIB)
-#include <QtPrintSupport/qtprintsupportglobal.h>
-#if QT_CONFIG(printdialog)
-#include <QPrinter>
-#include <QPrintDialog>
-#endif
-#if QT_CONFIG(printpreviewdialog)
-#include <QPrintPreviewDialog>
-#endif
-#endif
-
#include "spreadsheet.h"
#include "spreadsheetdelegate.h"
#include "spreadsheetitem.h"
#include "printview.h"
+#include <QtWidgets>
+#if defined(QT_PRINTSUPPORT_LIB)
+#include <QtPrintSupport>
+#endif
+
SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
- : QMainWindow(parent)
+ : QMainWindow(parent),
+ toolBar(new QToolBar(this)),
+ cellLabel(new QLabel(toolBar)),
+ table(new QTableWidget(rows, cols, this)),
+ formulaInput(new QLineEdit(this))
{
- addToolBar(toolBar = new QToolBar());
- formulaInput = new QLineEdit();
+ addToolBar(toolBar);
- cellLabel = new QLabel(toolBar);
cellLabel->setMinimumSize(80, 0);
toolBar->addWidget(cellLabel);
toolBar->addWidget(formulaInput);
- table = new QTableWidget(rows, cols, this);
table->setSizeAdjustPolicy(QTableWidget::AdjustToContents);
for (int c = 0; c < cols; ++c) {
QString character(QChar('A' + c));
@@ -88,7 +82,7 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
table->setItemDelegate(new SpreadSheetDelegate());
createActions();
- updateColor(0);
+ updateColor(nullptr);
setupMenuBar();
setupContents();
setupContextMenu();
@@ -103,7 +97,8 @@ SpreadSheet::SpreadSheet(int rows, int cols, QWidget *parent)
this, &SpreadSheet::updateLineEdit);
connect(table, &QTableWidget::itemChanged,
this, &SpreadSheet::updateStatus);
- connect(formulaInput, &QLineEdit::returnPressed, this, &SpreadSheet::returnPressed);
+ connect(formulaInput, &QLineEdit::returnPressed,
+ this, &SpreadSheet::returnPressed);
connect(table, &QTableWidget::itemChanged,
this, &SpreadSheet::updateLineEdit);
@@ -193,19 +188,19 @@ void SpreadSheet::updateColor(QTableWidgetItem *item)
QPixmap pix(16, 16);
QColor col;
if (item)
- col = item->backgroundColor();
+ col = item->background().color();
if (!col.isValid())
col = palette().base().color();
QPainter pt(&pix);
pt.fillRect(0, 0, 16, 16, col);
- QColor lighter = col.light();
+ QColor lighter = col.lighter();
pt.setPen(lighter);
QPoint lightFrame[] = { QPoint(0, 15), QPoint(0, 0), QPoint(15, 0) };
pt.drawPolyline(lightFrame, 3);
- pt.setPen(col.dark());
+ pt.setPen(col.darker());
QPoint darkFrame[] = { QPoint(1, 15), QPoint(15, 15), QPoint(15, 1) };
pt.drawPolyline(darkFrame, 3);
@@ -240,18 +235,18 @@ void SpreadSheet::returnPressed()
void SpreadSheet::selectColor()
{
QTableWidgetItem *item = table->currentItem();
- QColor col = item ? item->backgroundColor() : table->palette().base().color();
+ QColor col = item ? item->background().color() : table->palette().base().color();
col = QColorDialog::getColor(col, this);
if (!col.isValid())
return;
- QList<QTableWidgetItem*> selected = table->selectedItems();
- if (selected.count() == 0)
+ const QList<QTableWidgetItem *> selected = table->selectedItems();
+ if (selected.isEmpty())
return;
- foreach (QTableWidgetItem *i, selected) {
+ for (QTableWidgetItem *i : selected) {
if (i)
- i->setBackgroundColor(col);
+ i->setBackground(col);
}
updateColor(table->currentItem());
@@ -259,8 +254,8 @@ void SpreadSheet::selectColor()
void SpreadSheet::selectFont()
{
- QList<QTableWidgetItem*> selected = table->selectedItems();
- if (selected.count() == 0)
+ const QList<QTableWidgetItem *> selected = table->selectedItems();
+ if (selected.isEmpty())
return;
bool ok = false;
@@ -268,7 +263,7 @@ void SpreadSheet::selectFont()
if (!ok)
return;
- foreach (QTableWidgetItem *i, selected) {
+ for (QTableWidgetItem *i : selected) {
if (i)
i->setFont(fnt);
}
@@ -397,7 +392,7 @@ void SpreadSheet::actionSum()
int col_last = 0;
int col_cur = 0;
- QList<QTableWidgetItem*> selected = table->selectedItems();
+ const QList<QTableWidgetItem *> selected = table->selectedItems();
if (!selected.isEmpty()) {
QTableWidgetItem *first = selected.first();
@@ -408,7 +403,7 @@ void SpreadSheet::actionSum()
col_last = table->column(last);
}
- QTableWidgetItem *current = table->currentItem();
+ const QTableWidgetItem *current = table->currentItem();
if (current) {
row_cur = table->row(current);
@@ -435,7 +430,7 @@ void SpreadSheet::actionMath_helper(const QString &title, const QString &op)
QString cell2 = "C2";
QString out = "C3";
- QTableWidgetItem *current = table->currentItem();
+ const QTableWidgetItem *current = table->currentItem();
if (current)
out = encode_pos(table->currentRow(), table->currentColumn());
@@ -468,8 +463,9 @@ void SpreadSheet::actionDivide()
void SpreadSheet::clear()
{
- foreach (QTableWidgetItem *i, table->selectedItems())
- i->setText("");
+ const QList<QTableWidgetItem *> selectedItems = table->selectedItems();
+ for (QTableWidgetItem *i : selectedItems)
+ i->setText(QString());
}
void SpreadSheet::setupContextMenu()
@@ -489,13 +485,13 @@ void SpreadSheet::setupContextMenu()
void SpreadSheet::setupContents()
{
- QColor titleBackground(Qt::lightGray);
+ QBrush titleBackground(Qt::lightGray);
QFont titleFont = table->font();
titleFont.setBold(true);
// column 0
table->setItem(0, 0, new SpreadSheetItem("Item"));
- table->item(0, 0)->setBackgroundColor(titleBackground);
+ table->item(0, 0)->setBackground(titleBackground);
table->item(0, 0)->setToolTip("This column shows the purchased item/service");
table->item(0, 0)->setFont(titleFont);
@@ -510,11 +506,11 @@ void SpreadSheet::setupContents()
table->setItem(9, 0, new SpreadSheetItem("Total:"));
table->item(9, 0)->setFont(titleFont);
- table->item(9, 0)->setBackgroundColor(Qt::lightGray);
+ table->item(9, 0)->setBackground(titleBackground);
// column 1
table->setItem(0, 1, new SpreadSheetItem("Date"));
- table->item(0, 1)->setBackgroundColor(titleBackground);
+ table->item(0, 1)->setBackground(titleBackground);
table->item(0, 1)->setToolTip("This column shows the purchase date, double click to change");
table->item(0, 1)->setFont(titleFont);
@@ -528,11 +524,11 @@ void SpreadSheet::setupContents()
table->setItem(8, 1, new SpreadSheetItem("18/6/2006"));
table->setItem(9, 1, new SpreadSheetItem());
- table->item(9, 1)->setBackgroundColor(Qt::lightGray);
+ table->item(9, 1)->setBackground(titleBackground);
// column 2
table->setItem(0, 2, new SpreadSheetItem("Price"));
- table->item(0, 2)->setBackgroundColor(titleBackground);
+ table->item(0, 2)->setBackground(titleBackground);
table->item(0, 2)->setToolTip("This column shows the price of the purchase");
table->item(0, 2)->setFont(titleFont);
@@ -546,11 +542,11 @@ void SpreadSheet::setupContents()
table->setItem(8, 2, new SpreadSheetItem("1240"));
table->setItem(9, 2, new SpreadSheetItem());
- table->item(9, 2)->setBackgroundColor(Qt::lightGray);
+ table->item(9, 2)->setBackground(Qt::lightGray);
// column 3
table->setItem(0, 3, new SpreadSheetItem("Currency"));
- table->item(0, 3)->setBackgroundColor(titleBackground);
+ table->item(0, 3)->setBackground(titleBackground);
table->item(0, 3)->setToolTip("This column shows the currency");
table->item(0, 3)->setFont(titleFont);
@@ -564,11 +560,11 @@ void SpreadSheet::setupContents()
table->setItem(8, 3, new SpreadSheetItem("USD"));
table->setItem(9, 3, new SpreadSheetItem());
- table->item(9,3)->setBackgroundColor(Qt::lightGray);
+ table->item(9, 3)->setBackground(Qt::lightGray);
// column 4
table->setItem(0, 4, new SpreadSheetItem("Ex. Rate"));
- table->item(0, 4)->setBackgroundColor(titleBackground);
+ table->item(0, 4)->setBackground(titleBackground);
table->item(0, 4)->setToolTip("This column shows the exchange rate to NOK");
table->item(0, 4)->setFont(titleFont);
@@ -582,11 +578,11 @@ void SpreadSheet::setupContents()
table->setItem(8, 4, new SpreadSheetItem("7"));
table->setItem(9, 4, new SpreadSheetItem());
- table->item(9,4)->setBackgroundColor(Qt::lightGray);
+ table->item(9, 4)->setBackground(titleBackground);
// column 5
table->setItem(0, 5, new SpreadSheetItem("NOK"));
- table->item(0, 5)->setBackgroundColor(titleBackground);
+ table->item(0, 5)->setBackground(titleBackground);
table->item(0, 5)->setToolTip("This column shows the expenses in NOK");
table->item(0, 5)->setFont(titleFont);
@@ -600,7 +596,7 @@ void SpreadSheet::setupContents()
table->setItem(8, 5, new SpreadSheetItem("* C9 E9"));
table->setItem(9, 5, new SpreadSheetItem("sum F2 F9"));
- table->item(9,5)->setBackgroundColor(Qt::lightGray);
+ table->item(9, 5)->setBackground(titleBackground);
}
const char *htmlText =
diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheet.h b/examples/widgets/itemviews/spreadsheet/spreadsheet.h
index a90d1d8c20..b36a377156 100644
--- a/examples/widgets/itemviews/spreadsheet/spreadsheet.h
+++ b/examples/widgets/itemviews/spreadsheet/spreadsheet.h
@@ -65,10 +65,8 @@ QT_END_NAMESPACE
class SpreadSheet : public QMainWindow
{
Q_OBJECT
-
public:
-
- SpreadSheet(int rows, int cols, QWidget *parent = 0);
+ SpreadSheet(int rows, int cols, QWidget *parent = nullptr);
public slots:
void updateStatus(QTableWidgetItem *item);
diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp
index aaf8902af5..eadd5fadb8 100644
--- a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp
+++ b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.cpp
@@ -53,11 +53,12 @@
#include <QtWidgets>
SpreadSheetDelegate::SpreadSheetDelegate(QObject *parent)
- : QItemDelegate(parent) {}
+ : QItemDelegate(parent)
+{}
QWidget *SpreadSheetDelegate::createEditor(QWidget *parent,
- const QStyleOptionViewItem &,
- const QModelIndex &index) const
+ const QStyleOptionViewItem &,
+ const QModelIndex &index) const
{
if (index.column() == 1) {
QDateTimeEdit *editor = new QDateTimeEdit(parent);
@@ -92,7 +93,7 @@ void SpreadSheetDelegate::commitAndCloseEditor()
}
void SpreadSheetDelegate::setEditorData(QWidget *editor,
- const QModelIndex &index) const
+ const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
if (edit) {
@@ -109,7 +110,8 @@ void SpreadSheetDelegate::setEditorData(QWidget *editor,
}
void SpreadSheetDelegate::setModelData(QWidget *editor,
- QAbstractItemModel *model, const QModelIndex &index) const
+ QAbstractItemModel *model,
+ const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
if (edit) {
diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h
index 333467720f..c89459cadf 100644
--- a/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h
+++ b/examples/widgets/itemviews/spreadsheet/spreadsheetdelegate.h
@@ -51,8 +51,6 @@
#ifndef SPREADSHEETDELEGATE_H
#define SPREADSHEETDELEGATE_H
-#include "spreadsheet.h"
-
#include <QItemDelegate>
class SpreadSheetDelegate : public QItemDelegate
@@ -60,7 +58,7 @@ class SpreadSheetDelegate : public QItemDelegate
Q_OBJECT
public:
- SpreadSheetDelegate(QObject *parent = 0);
+ SpreadSheetDelegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp
index 4c18fa8251..62d3a6fdcd 100644
--- a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp
+++ b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.cpp
@@ -50,19 +50,9 @@
#include "spreadsheetitem.h"
-SpreadSheetItem::SpreadSheetItem()
- : QTableWidgetItem(), isResolving(false)
-{
-}
-
-SpreadSheetItem::SpreadSheetItem(const QString &text)
- : QTableWidgetItem(text), isResolving(false)
-{
-}
-
QTableWidgetItem *SpreadSheetItem::clone() const
{
- SpreadSheetItem *item = new SpreadSheetItem();
+ SpreadSheetItem *item = new SpreadSheetItem;
*item = *this;
return item;
}
@@ -75,21 +65,20 @@ QVariant SpreadSheetItem::data(int role) const
if (role == Qt::DisplayRole)
return display();
- QString t = display().toString();
- bool isNumber = false;
- int number = t.toInt(&isNumber);
+ const QString t = display().toString();
- if (role == Qt::TextColorRole) {
- if (!isNumber)
- return QVariant::fromValue(QColor(Qt::black));
- else if (number < 0)
- return QVariant::fromValue(QColor(Qt::red));
- return QVariant::fromValue(QColor(Qt::blue));
+ if (role == Qt::ForegroundRole) {
+ bool isNumber = false;
+ const int number = t.toInt(&isNumber);
+ QColor color = Qt::black;
+ if (isNumber)
+ color = (number < 0) ? Qt::red : Qt::blue;
+ return QVariant::fromValue(color);
}
- if (role == Qt::TextAlignmentRole)
- if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
- return (int)(Qt::AlignRight | Qt::AlignVCenter);
+ if (role == Qt::TextAlignmentRole)
+ if (!t.isEmpty() && (t.at(0).isNumber() || t.at(0) == '-'))
+ return int(Qt::AlignRight | Qt::AlignVCenter);
return QTableWidgetItem::data(role);
}
diff --git a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h
index 46460df527..159f4a7eea 100644
--- a/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h
+++ b/examples/widgets/itemviews/spreadsheet/spreadsheetitem.h
@@ -58,8 +58,7 @@
class SpreadSheetItem : public QTableWidgetItem
{
public:
- SpreadSheetItem();
- SpreadSheetItem(const QString &text);
+ using QTableWidgetItem::QTableWidgetItem;
QTableWidgetItem *clone() const override;
@@ -74,10 +73,10 @@ public:
static QVariant computeFormula(const QString &formula,
const QTableWidget *widget,
- const QTableWidgetItem *self = 0);
+ const QTableWidgetItem *self = nullptr);
private:
- mutable bool isResolving;
+ mutable bool isResolving = false;
};
#endif // SPREADSHEETITEM_H
diff --git a/examples/widgets/itemviews/stardelegate/main.cpp b/examples/widgets/itemviews/stardelegate/main.cpp
index 51ca30c73a..452976bba0 100644
--- a/examples/widgets/itemviews/stardelegate/main.cpp
+++ b/examples/widgets/itemviews/stardelegate/main.cpp
@@ -101,10 +101,7 @@ int main(int argc, char *argv[])
tableWidget.setEditTriggers(QAbstractItemView::DoubleClicked
| QAbstractItemView::SelectedClicked);
tableWidget.setSelectionBehavior(QAbstractItemView::SelectRows);
-
- QStringList headerLabels;
- headerLabels << "Title" << "Genre" << "Artist" << "Rating";
- tableWidget.setHorizontalHeaderLabels(headerLabels);
+ tableWidget.setHorizontalHeaderLabels({"Title", "Genre", "Artist", "Rating"});
populateTableWidget(&tableWidget);
diff --git a/examples/widgets/itemviews/stardelegate/stardelegate.cpp b/examples/widgets/itemviews/stardelegate/stardelegate.cpp
index da5902a160..41ae5a920f 100644
--- a/examples/widgets/itemviews/stardelegate/stardelegate.cpp
+++ b/examples/widgets/itemviews/stardelegate/stardelegate.cpp
@@ -65,7 +65,7 @@ void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
painter->fillRect(option.rect, option.palette.highlight());
starRating.paint(painter, option.rect, option.palette,
- StarRating::ReadOnly);
+ StarRating::EditMode::ReadOnly);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
@@ -79,9 +79,8 @@ QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option,
if (index.data().canConvert<StarRating>()) {
StarRating starRating = qvariant_cast<StarRating>(index.data());
return starRating.sizeHint();
- } else {
- return QStyledItemDelegate::sizeHint(option, index);
}
+ return QStyledItemDelegate::sizeHint(option, index);
}
//! [1]
@@ -96,9 +95,8 @@ QWidget *StarDelegate::createEditor(QWidget *parent,
connect(editor, &StarEditor::editingFinished,
this, &StarDelegate::commitAndCloseEditor);
return editor;
- } else {
- return QStyledItemDelegate::createEditor(parent, option, index);
}
+ return QStyledItemDelegate::createEditor(parent, option, index);
}
//! [2]
diff --git a/examples/widgets/itemviews/stardelegate/stardelegate.h b/examples/widgets/itemviews/stardelegate/stardelegate.h
index ffc65fbedd..1fc31f8ee8 100644
--- a/examples/widgets/itemviews/stardelegate/stardelegate.h
+++ b/examples/widgets/itemviews/stardelegate/stardelegate.h
@@ -57,9 +57,8 @@
class StarDelegate : public QStyledItemDelegate
{
Q_OBJECT
-
public:
- StarDelegate(QWidget *parent = 0) : QStyledItemDelegate(parent) {}
+ using QStyledItemDelegate::QStyledItemDelegate;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
diff --git a/examples/widgets/itemviews/stardelegate/stareditor.cpp b/examples/widgets/itemviews/stardelegate/stareditor.cpp
index 19a4b54d9e..43706eeae0 100644
--- a/examples/widgets/itemviews/stardelegate/stareditor.cpp
+++ b/examples/widgets/itemviews/stardelegate/stareditor.cpp
@@ -48,11 +48,11 @@
**
****************************************************************************/
-#include <QtWidgets>
-
#include "stareditor.h"
#include "starrating.h"
+#include <QtWidgets>
+
//! [0]
StarEditor::StarEditor(QWidget *parent)
: QWidget(parent)
@@ -71,35 +71,37 @@ QSize StarEditor::sizeHint() const
void StarEditor::paintEvent(QPaintEvent *)
{
QPainter painter(this);
- myStarRating.paint(&painter, rect(), this->palette(),
- StarRating::Editable);
+ myStarRating.paint(&painter, rect(), palette(),
+ StarRating::EditMode::Editable);
}
//! [1]
//! [2]
void StarEditor::mouseMoveEvent(QMouseEvent *event)
{
- int star = starAtPosition(event->x());
+ const int star = starAtPosition(event->x());
if (star != myStarRating.starCount() && star != -1) {
myStarRating.setStarCount(star);
update();
}
+ QWidget::mouseMoveEvent(event);
}
//! [2]
//! [3]
-void StarEditor::mouseReleaseEvent(QMouseEvent * /* event */)
+void StarEditor::mouseReleaseEvent(QMouseEvent *event)
{
emit editingFinished();
+ QWidget::mouseReleaseEvent(event);
}
//! [3]
//! [4]
-int StarEditor::starAtPosition(int x)
+int StarEditor::starAtPosition(int x) const
{
- int star = (x / (myStarRating.sizeHint().width()
- / myStarRating.maxStarCount())) + 1;
+ const int star = (x / (myStarRating.sizeHint().width()
+ / myStarRating.maxStarCount())) + 1;
if (star <= 0 || star > myStarRating.maxStarCount())
return -1;
diff --git a/examples/widgets/itemviews/stardelegate/stareditor.h b/examples/widgets/itemviews/stardelegate/stareditor.h
index 4a4c3a4954..8b1bf2efed 100644
--- a/examples/widgets/itemviews/stardelegate/stareditor.h
+++ b/examples/widgets/itemviews/stardelegate/stareditor.h
@@ -59,9 +59,8 @@
class StarEditor : public QWidget
{
Q_OBJECT
-
public:
- StarEditor(QWidget *parent = 0);
+ StarEditor(QWidget *parent = nullptr);
QSize sizeHint() const override;
void setStarRating(const StarRating &starRating) {
@@ -78,7 +77,7 @@ protected:
void mouseReleaseEvent(QMouseEvent *event) override;
private:
- int starAtPosition(int x);
+ int starAtPosition(int x) const;
StarRating myStarRating;
};
diff --git a/examples/widgets/itemviews/stardelegate/starrating.cpp b/examples/widgets/itemviews/stardelegate/starrating.cpp
index 845e474de9..75f0bd9cf7 100644
--- a/examples/widgets/itemviews/stardelegate/starrating.cpp
+++ b/examples/widgets/itemviews/stardelegate/starrating.cpp
@@ -48,19 +48,18 @@
**
****************************************************************************/
+#include "starrating.h"
+
#include <QtWidgets>
#include <cmath>
-#include "starrating.h"
-
-const int PaintingScaleFactor = 20;
+constexpr int PaintingScaleFactor = 20;
//! [0]
StarRating::StarRating(int starCount, int maxStarCount)
+ : myStarCount(starCount),
+ myMaxStarCount(maxStarCount)
{
- myStarCount = starCount;
- myMaxStarCount = maxStarCount;
-
starPolygon << QPointF(1.0, 0.5);
for (int i = 1; i < 5; ++i)
starPolygon << QPointF(0.5 + 0.5 * std::cos(0.8 * i * 3.14),
@@ -87,23 +86,19 @@ void StarRating::paint(QPainter *painter, const QRect &rect,
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(Qt::NoPen);
+ painter->setBrush(mode == EditMode::Editable ?
+ palette.highlight() :
+ palette.windowText());
- if (mode == Editable) {
- painter->setBrush(palette.highlight());
- } else {
- painter->setBrush(palette.foreground());
- }
-
- int yOffset = (rect.height() - PaintingScaleFactor) / 2;
+ const int yOffset = (rect.height() - PaintingScaleFactor) / 2;
painter->translate(rect.x(), rect.y() + yOffset);
painter->scale(PaintingScaleFactor, PaintingScaleFactor);
for (int i = 0; i < myMaxStarCount; ++i) {
- if (i < myStarCount) {
+ if (i < myStarCount)
painter->drawPolygon(starPolygon, Qt::WindingFill);
- } else if (mode == Editable) {
+ else if (mode == EditMode::Editable)
painter->drawPolygon(diamondPolygon, Qt::WindingFill);
- }
painter->translate(1.0, 0.0);
}
diff --git a/examples/widgets/itemviews/stardelegate/starrating.h b/examples/widgets/itemviews/stardelegate/starrating.h
index fa77311914..fc3028db58 100644
--- a/examples/widgets/itemviews/stardelegate/starrating.h
+++ b/examples/widgets/itemviews/stardelegate/starrating.h
@@ -51,15 +51,15 @@
#ifndef STARRATING_H
#define STARRATING_H
-#include <QMetaType>
-#include <QPointF>
-#include <QVector>
+#include <QPainter>
+#include <QPolygonF>
+#include <QSize>
//! [0]
class StarRating
{
public:
- enum EditMode { Editable, ReadOnly };
+ enum class EditMode { Editable, ReadOnly };
explicit StarRating(int starCount = 1, int maxStarCount = 5);
diff --git a/examples/widgets/itemviews/storageview/main.cpp b/examples/widgets/itemviews/storageview/main.cpp
index f349e58fad..8f23a4820a 100644
--- a/examples/widgets/itemviews/storageview/main.cpp
+++ b/examples/widgets/itemviews/storageview/main.cpp
@@ -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"
diff --git a/examples/widgets/itemviews/storageview/storagemodel.cpp b/examples/widgets/itemviews/storageview/storagemodel.cpp
index 063f126d86..b98fc8445c 100644
--- a/examples/widgets/itemviews/storageview/storagemodel.cpp
+++ b/examples/widgets/itemviews/storageview/storagemodel.cpp
@@ -53,14 +53,6 @@
#include <QDir>
#include <QLocale>
-#include <qmath.h>
-#include <algorithm>
-#include <cmath>
-
-StorageModel::StorageModel(QObject *parent) :
- QAbstractTableModel(parent)
-{
-}
void StorageModel::refresh()
{
diff --git a/examples/widgets/itemviews/storageview/storagemodel.h b/examples/widgets/itemviews/storageview/storagemodel.h
index 787b2f04de..597218c220 100644
--- a/examples/widgets/itemviews/storageview/storagemodel.h
+++ b/examples/widgets/itemviews/storageview/storagemodel.h
@@ -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;