QtWidgets documentation: cleanup

Cleanup the QtWidgets documentation:
 - use new signal/slot syntax
 - use range-based for loop instead foreach

Change-Id: I621b1ddac108d3df676209241d93d9b4f04a25fe
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Christian Ehrlicher 2019-02-03 19:55:51 +01:00
parent b76a923a8e
commit 3514aedbf3
20 changed files with 104 additions and 114 deletions

View File

@ -59,7 +59,7 @@ mapper->toFirst();
//! [1] //! [1]
QDataWidgetMapper *mapper = new QDataWidgetMapper(); QDataWidgetMapper *mapper = new QDataWidgetMapper;
mapper->setModel(myModel); mapper->setModel(myModel);
mapper->addMapping(nameLineEdit, 0); mapper->addMapping(nameLineEdit, 0);
mapper->addMapping(ageSpinBox, 1); mapper->addMapping(ageSpinBox, 1);
@ -67,7 +67,7 @@ mapper->addMapping(ageSpinBox, 1);
//! [2] //! [2]
QDataWidgetMapper *mapper = new QDataWidgetMapper(); QDataWidgetMapper *mapper = new QDataWidgetMapper;
connect(myTableView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), connect(myTableView->selectionModel(), &QItemSelectionModel::currentRowChanged,
mapper, SLOT(setCurrentModelIndex(QModelIndex))); mapper, &QDataWidgetMapper::setCurrentModelIndex);
//! [2] //! [2]

View File

@ -100,7 +100,8 @@ QSize MyWidget::sizeHint() const
//! [4] //! [4]
void showAllHiddenTopLevelWidgets() void showAllHiddenTopLevelWidgets()
{ {
foreach (QWidget *widget, QApplication::topLevelWidgets()) { const QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
for (QWidget *widget : topLevelWidgets) {
if (widget->isHidden()) if (widget->isHidden())
widget->show(); widget->show();
} }
@ -111,7 +112,8 @@ void showAllHiddenTopLevelWidgets()
//! [5] //! [5]
void updateAllWidgets() void updateAllWidgets()
{ {
foreach (QWidget *widget, QApplication::allWidgets()) const QWidgetList allWidgets = QApplication::allWidgets();
for (QWidget *widget : allWidgets)
widget->update(); widget->update();
} }
//! [5] //! [5]
@ -171,13 +173,15 @@ appname -session id
//! [10] //! [10]
foreach (const QString &command, mySession.restartCommand()) const QStringList commands = mySession.restartCommand();
for (const QString &command : commands)
do_something(command); do_something(command);
//! [10] //! [10]
//! [11] //! [11]
foreach (const QString &command, mySession.discardCommand()) const QStringList commands = mySession.discardCommand();
for (const QString &command : commands)
do_something(command); do_something(command);
//! [11] //! [11]

View File

@ -81,7 +81,7 @@ exec(e->globalPos());
//! [6] //! [6]
QMenu menu; QMenu menu;
QAction *at = actions[0]; // Assumes actions is not empty QAction *at = actions[0]; // Assumes actions is not empty
foreach (QAction *a, actions) for (QAction *a : qAsConst(actions))
menu.addAction(a); menu.addAction(a);
menu.exec(pos, at); menu.exec(pos, at);
//! [6] //! [6]

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
typedef QDialog WordCountDialog; typedef QDialog WordCountDialog;
typedef QDialog FindDialog; typedef QDialog FindDialog;
@ -76,7 +76,8 @@ void EditorWindow::find()
{ {
if (!findDialog) { if (!findDialog) {
findDialog = new FindDialog(this); findDialog = new FindDialog(this);
connect(findDialog, SIGNAL(findNext()), this, SLOT(findNext())); connect(findDialog, &FindDialog::findNext,
this, &EditorWindow::findNext);
} }
findDialog->show(); findDialog->show();
@ -249,9 +250,9 @@ Operation::Operation(QObject *parent)
: QObject(parent), steps(0) : QObject(parent), steps(0)
{ {
pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100); pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100);
connect(pd, SIGNAL(canceled()), this, SLOT(cancel())); connect(pd, &QProgressDialog::canceled, this, &Operation::cancel);
t = new QTimer(this); t = new QTimer(this);
connect(t, SIGNAL(timeout()), this, SLOT(perform())); connect(t, &QTimer::timeout, this, &Operation::perform);
t->start(0); t->start(0);
} }
//! [4] //! [5] //! [4] //! [5]

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
@ -63,8 +63,8 @@ MainWindow::MainWindow(QWidget *parent)
textBrowser = new QTextBrowser(this); textBrowser = new QTextBrowser(this);
connect(headingList, SIGNAL(itemClicked(QListWidgetItem*)), connect(headingList, &QListWidget::itemClicked,
this, SLOT(updateText(QListWidgetItem*))); this, &MainWindow::updateText);
updateText(headingList->item(0)); updateText(headingList->item(0));
headingList->setCurrentRow(0); headingList->setCurrentRow(0);
@ -119,7 +119,7 @@ void MainWindow::setupMenus()
QAction *exitAct = new QAction(tr("E&xit"), this); QAction *exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q")); exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application")); exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows())); connect(exitAct, &QAction::triggered, qApp, &QApplication::closeAllWindows);
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(exitAct); fileMenu->addAction(exitAct);

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
void mainWindowExample() void mainWindowExample()
{ {
@ -95,7 +95,7 @@ int main(int argv, char **args)
QAction *act = new QAction(qApp); QAction *act = new QAction(qApp);
act->setShortcut(Qt::ALT + Qt::Key_S); act->setShortcut(Qt::ALT + Qt::Key_S);
act->setShortcutContext( Qt::ApplicationShortcut ); act->setShortcutContext( Qt::ApplicationShortcut );
QObject::connect(act, SIGNAL(triggered()), qApp, SLOT(aboutQt())); QObject::connect(act, &QAction::triggered, qApp, &QApplication::aboutQt);
QWidget widget5; QWidget widget5;
widget5.show(); widget5.show();

View File

@ -73,7 +73,8 @@ listView->setDropIndicatorShown(true);
this->listView = listView; this->listView = listView;
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered,
this, &QWidget::close);
setupListItems(); setupListItems();

View File

@ -54,7 +54,7 @@
A simple model that uses a QStringList as its data source. A simple model that uses a QStringList as its data source.
*/ */
#include <QtGui> #include <QtWidgets>
#include "model.h" #include "model.h"
@ -121,7 +121,7 @@ bool DragDropListModel::dropMimeData(const QMimeData *data,
//! [6] //! [6]
insertRows(beginRow, rows, QModelIndex()); insertRows(beginRow, rows, QModelIndex());
foreach (const QString &text, newItems) { for (const QString &text : qAsConst(newItems)) {
QModelIndex idx = index(beginRow, 0, QModelIndex()); QModelIndex idx = index(beginRow, 0, QModelIndex());
setData(idx, text); setData(idx, text);
beginRow++; beginRow++;
@ -146,12 +146,12 @@ Qt::ItemFlags DragDropListModel::flags(const QModelIndex &index) const
//! [8] //! [8]
QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const QMimeData *DragDropListModel::mimeData(const QModelIndexList &indexes) const
{ {
QMimeData *mimeData = new QMimeData(); QMimeData *mimeData = new QMimeData;
QByteArray encodedData; QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly); QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) { for (const QModelIndex &index : indexes) {
if (index.isValid()) { if (index.isValid()) {
QString text = data(index, Qt::DisplayRole).toString(); QString text = data(index, Qt::DisplayRole).toString();
stream << text; stream << text;

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
@ -74,7 +74,7 @@ listWidget->setDragDropMode(QAbstractItemView::InternalMove);
this->listWidget = listWidget; this->listWidget = listWidget;
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, this, &QWidget::close);
setupListItems(); setupListItems();

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
@ -77,14 +77,13 @@ MainWindow::MainWindow()
listWidget = new QListWidget(this); listWidget = new QListWidget(this);
listWidget->setSelectionMode(QAbstractItemView::SingleSelection); listWidget->setSelectionMode(QAbstractItemView::SingleSelection);
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, this, &QWidget::close);
connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending())); connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending())); connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem())); connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem())); connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
connect(listWidget, connect(listWidget, &QListWidget::currentItemChanged,
SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)), this, &MainWindow::updateMenus);
this, SLOT(updateMenus(QListWidgetItem*)));
setupListItems(); setupListItems();
updateMenus(listWidget->currentItem()); updateMenus(listWidget->currentItem());

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -74,9 +74,8 @@ int main(int argc, char *argv[])
filteredView->setWindowTitle("Filtered view onto a string list model"); filteredView->setWindowTitle("Filtered view onto a string list model");
QLineEdit *patternEditor = new QLineEdit; QLineEdit *patternEditor = new QLineEdit;
QObject:: QObject::connect(patternEditor, &QLineEdit::textChanged,
connect(patternEditor, SIGNAL(textChanged(QString)), filterModel, &QSortFilterProxyModel::setFilterWildcard);
filterModel, SLOT(setFilterRegExp(QString)));
QVBoxLayout *layout = new QVBoxLayout(window); QVBoxLayout *layout = new QVBoxLayout(window);
layout->addWidget(filteredView); layout->addWidget(filteredView);

View File

@ -48,13 +48,12 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include <QApplication>
class Widget : public QWidget class Widget : public QWidget
{ {
public: public:
Widget(QWidget *parent = 0); Widget(QWidget *parent = nullptr);
}; };
Widget::Widget(QWidget *parent) Widget::Widget(QWidget *parent)
@ -75,8 +74,8 @@ Widget::Widget(QWidget *parent)
pageComboBox->addItem(tr("Page 1")); pageComboBox->addItem(tr("Page 1"));
pageComboBox->addItem(tr("Page 2")); pageComboBox->addItem(tr("Page 2"));
pageComboBox->addItem(tr("Page 3")); pageComboBox->addItem(tr("Page 3"));
connect(pageComboBox, SIGNAL(activated(int)), connect(pageComboBox, QOverload<int>::of(&QComboBox::activated),
stackedLayout, SLOT(setCurrentIndex(int))); stackedLayout, &QStackedLayout::setCurrentIndex);
//! [1] //! [1]
//! [2] //! [2]

View File

@ -48,8 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include <QApplication>
class Widget : public QWidget class Widget : public QWidget
{ {
@ -75,8 +74,8 @@ Widget::Widget(QWidget *parent)
pageComboBox->addItem(tr("Page 1")); pageComboBox->addItem(tr("Page 1"));
pageComboBox->addItem(tr("Page 2")); pageComboBox->addItem(tr("Page 2"));
pageComboBox->addItem(tr("Page 3")); pageComboBox->addItem(tr("Page 3"));
connect(pageComboBox, SIGNAL(activated(int)), connect(pageComboBox, QOverload<int>::of(&QComboBox::activated),
stackedWidget, SLOT(setCurrentIndex(int))); stackedWidget, &QStackedWidget::setCurrentIndex);
//! [1] //! [2] //! [1] //! [2]
QVBoxLayout *layout = new QVBoxLayout; QVBoxLayout *layout = new QVBoxLayout;

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
@ -72,9 +72,9 @@ MainWindow::MainWindow()
//! [0] //! [0]
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, this, &QWidget::close);
connect(tableWidthAction, SIGNAL(triggered()), this, SLOT(changeWidth())); connect(tableWidthAction, &QAction::triggered, this, &MainWindow::changeWidth);
connect(tableHeightAction, SIGNAL(triggered()), this, SLOT(changeHeight())); connect(tableHeightAction, &QAction::triggered, this, &MainWindow::changeHeight);
setupTableItems(); setupTableItems();

View File

@ -48,8 +48,8 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "math.h" #include <math.h>
#include "mainwindow.h" #include "mainwindow.h"
@ -89,9 +89,9 @@ MainWindow::MainWindow()
tableWidget->setHorizontalHeaderItem(1, squaresHeaderItem); tableWidget->setHorizontalHeaderItem(1, squaresHeaderItem);
tableWidget->setHorizontalHeaderItem(2, cubesHeaderItem); tableWidget->setHorizontalHeaderItem(2, cubesHeaderItem);
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, this, &QWidget::close);
connect(sumItemsAction, SIGNAL(triggered()), this, SLOT(sumItems())); connect(sumItemsAction, &QAction::triggered, this, &MainWindow::sumItems);
connect(averageItemsAction, SIGNAL(triggered()), this, SLOT(averageItems())); connect(averageItemsAction, &QAction::triggered, this, &MainWindow::averageItems);
setupTableItems(); setupTableItems();
@ -119,12 +119,11 @@ void MainWindow::setupTableItems()
void MainWindow::averageItems() void MainWindow::averageItems()
{ {
QList<QTableWidgetItem *> selected = tableWidget->selectedItems(); const QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
QTableWidgetItem *item;
int number = 0; int number = 0;
double total = 0; double total = 0;
foreach (item, selected) { for (QTableWidgetItem *item : selected) {
bool ok; bool ok;
double value = item->text().toDouble(&ok); double value = item->text().toDouble(&ok);
@ -140,12 +139,11 @@ void MainWindow::averageItems()
void MainWindow::sumItems() void MainWindow::sumItems()
{ {
//! [4] //! [4]
QList<QTableWidgetItem *> selected = tableWidget->selectedItems(); const QList<QTableWidgetItem *> selected = tableWidget->selectedItems();
QTableWidgetItem *item;
int number = 0; int number = 0;
double total = 0; double total = 0;
foreach (item, selected) { for (QTableWidgetItem *item : selected) {
bool ok; bool ok;
double value = item->text().toDouble(&ok); double value = item->text().toDouble(&ok);

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
@ -90,16 +90,15 @@ MainWindow::MainWindow()
treeWidget->setHeaderLabels(headers); treeWidget->setHeaderLabels(headers);
//! [2] //! [2]
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, this, &QWidget::close);
connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending())); connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
connect(autoSortAction, SIGNAL(triggered()), this, SLOT(updateSortItems())); connect(autoSortAction, &QAction::triggered, this, &MainWindow::updateSortItems);
connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending())); connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
connect(findItemsAction, SIGNAL(triggered()), this, SLOT(findItems())); connect(findItemsAction, &QAction::triggered, this, &MainWindow::findItems);
connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem())); connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem())); connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
connect(treeWidget, connect(treeWidget, &QTreeWidget::currentItemChanged,
SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, &MainWindow::updateMenus);
this, SLOT(updateMenus(QTreeWidgetItem*)));
setupTreeItems(); setupTreeItems();
updateMenus(treeWidget->currentItem()); updateMenus(treeWidget->currentItem());
@ -150,17 +149,15 @@ void MainWindow::findItems()
if (itemText.isEmpty()) if (itemText.isEmpty())
return; return;
//! [6] const QList<QTreeWidgetItem *> items = treeWidget->selectedItems();
QTreeWidgetItem *item; for (QTreeWidgetItem *item : items)
//! [6]
foreach (item, treeWidget->selectedItems())
item->setSelected(false); item->setSelected(false);
//! [7] //! [7]
QList<QTreeWidgetItem *> found = treeWidget->findItems( const QList<QTreeWidgetItem *> found = treeWidget->findItems(
itemText, Qt::MatchWildcard); itemText, Qt::MatchWildcard);
foreach (item, found) { for (QTreeWidgetItem *item : found) {
item->setSelected(true); item->setSelected(true);
// Show the item->text(0) for each item. // Show the item->text(0) for each item.
} }

View File

@ -48,7 +48,7 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui> #include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
@ -85,16 +85,15 @@ MainWindow::MainWindow()
headers << tr("Subject") << tr("Default"); headers << tr("Subject") << tr("Default");
treeWidget->setHeaderLabels(headers); treeWidget->setHeaderLabels(headers);
connect(quitAction, SIGNAL(triggered()), this, SLOT(close())); connect(quitAction, &QAction::triggered, this, &QWidget::close);
connect(ascendingAction, SIGNAL(triggered()), this, SLOT(sortAscending())); connect(ascendingAction, &QAction::triggered, this, &MainWindow::sortAscending);
connect(autoSortAction, SIGNAL(triggered()), this, SLOT(updateSortItems())); connect(autoSortAction, &QAction::triggered, this, &MainWindow::updateSortItems);
connect(descendingAction, SIGNAL(triggered()), this, SLOT(sortDescending())); connect(descendingAction, &QAction::triggered, this, &MainWindow::sortDescending);
connect(findItemsAction, SIGNAL(triggered()), this, SLOT(findItems())); connect(findItemsAction, &QAction::triggered, this, &MainWindow::findItems);
connect(insertAction, SIGNAL(triggered()), this, SLOT(insertItem())); connect(insertAction, &QAction::triggered, this, &MainWindow::insertItem);
connect(removeAction, SIGNAL(triggered()), this, SLOT(removeItem())); connect(removeAction, &QAction::triggered, this, &MainWindow::removeItem);
connect(treeWidget, connect(treeWidget, &QTreeWidget::currentItemChanged,
SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, &MainWindow::updateMenus);
this, SLOT(updateMenus(QTreeWidgetItem*)));
setupTreeItems(); setupTreeItems();
updateMenus(treeWidget->currentItem()); updateMenus(treeWidget->currentItem());

View File

@ -81,9 +81,9 @@ MainWindow::MainWindow(QWidget *parent)
QAction *selectAllAction = actionMenu->addAction(tr("&Select All")); QAction *selectAllAction = actionMenu->addAction(tr("&Select All"));
menuBar()->addMenu(actionMenu); menuBar()->addMenu(actionMenu);
connect(fillAction, SIGNAL(triggered()), this, SLOT(fillSelection())); connect(fillAction, &QAction::triggered, this, &MainWindow::fillSelection);
connect(clearAction, SIGNAL(triggered()), this, SLOT(clearSelection())); connect(clearAction, &QAction::triggered, this, &MainWindow::clearSelection);
connect(selectAllAction, SIGNAL(triggered()), this, SLOT(selectAll())); connect(selectAllAction, &QAction::triggered, this, &MainWindow::selectAll);
selectionModel = table->selectionModel(); selectionModel = table->selectionModel();
@ -94,10 +94,9 @@ MainWindow::MainWindow(QWidget *parent)
void MainWindow::fillSelection() void MainWindow::fillSelection()
{ {
//! [0] //! [0]
QModelIndexList indexes = selectionModel->selectedIndexes(); const QModelIndexList indexes = selectionModel->selectedIndexes();
QModelIndex index;
foreach(index, indexes) { for (const QModelIndex &index : indexes) {
QString text = QString("(%1,%2)").arg(index.row()).arg(index.column()); QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
model->setData(index, text); model->setData(index, text);
} }
@ -106,11 +105,10 @@ void MainWindow::fillSelection()
void MainWindow::clearSelection() void MainWindow::clearSelection()
{ {
QModelIndexList indexes = selectionModel->selectedIndexes(); const QModelIndexList indexes = selectionModel->selectedIndexes();
QModelIndex index;
foreach(index, indexes) for (const QModelIndex &index : indexes)
model->setData(index, ""); model->setData(index, QString());
} }
void MainWindow::selectAll() void MainWindow::selectAll()

View File

@ -74,12 +74,10 @@ MainWindow::MainWindow(QWidget *parent)
table->setModel(model); table->setModel(model);
selectionModel = table->selectionModel(); selectionModel = table->selectionModel();
connect(selectionModel, connect(selectionModel, &QItemSelectionModel::selectionChanged,
SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, &MainWindow::updateSelection);
this, SLOT(updateSelection(QItemSelection,QItemSelection))); connect(selectionModel, &QItemSelectionModel::currentChanged,
connect(selectionModel, this, &MainWindow::changeCurrent);
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(changeCurrent(QModelIndex,QModelIndex)));
statusBar(); statusBar();
setCentralWidget(table); setCentralWidget(table);
@ -89,10 +87,9 @@ MainWindow::MainWindow(QWidget *parent)
void MainWindow::updateSelection(const QItemSelection &selected, void MainWindow::updateSelection(const QItemSelection &selected,
const QItemSelection &deselected) const QItemSelection &deselected)
{ {
QModelIndex index;
QModelIndexList items = selected.indexes(); QModelIndexList items = selected.indexes();
foreach (index, items) { for (const QModelIndex &index : qAsConst(items)) {
QString text = QString("(%1,%2)").arg(index.row()).arg(index.column()); QString text = QString("(%1,%2)").arg(index.row()).arg(index.column());
model->setData(index, text); model->setData(index, text);
//! [0] //! [1] //! [0] //! [1]
@ -102,8 +99,8 @@ void MainWindow::updateSelection(const QItemSelection &selected,
//! [2] //! [2]
items = deselected.indexes(); items = deselected.indexes();
foreach (index, items) for (const QModelIndex &index : qAsConst(items)) {
model->setData(index, ""); model->setData(index, QString());
} }
//! [2] //! [2]

View File

@ -1003,8 +1003,8 @@
\snippet reading-selections/window.cpp 0 \snippet reading-selections/window.cpp 0
The above code uses Qt's convenient \l{Container Classes}{foreach The above code uses a range-based for-loop to iterate over,
keyword} to iterate over, and modify, the items corresponding to the and modify, the items corresponding to the
indexes returned by the selection model. indexes returned by the selection model.
The selection model emits signals to indicate changes in the The selection model emits signals to indicate changes in the
@ -1611,7 +1611,6 @@
We can obtain a list of matching items with the \c findItems() We can obtain a list of matching items with the \c findItems()
function: function:
\snippet qtreewidget-using/mainwindow.cpp 6
\snippet qtreewidget-using/mainwindow.cpp 7 \snippet qtreewidget-using/mainwindow.cpp 7
The above code causes items in a tree widget to be selected if they The above code causes items in a tree widget to be selected if they