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

View File

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

View File

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

View File

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

View File

@ -48,7 +48,7 @@
**
****************************************************************************/
#include <QtGui>
#include <QtWidgets>
#include "mainwindow.h"
@ -63,8 +63,8 @@ MainWindow::MainWindow(QWidget *parent)
textBrowser = new QTextBrowser(this);
connect(headingList, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(updateText(QListWidgetItem*)));
connect(headingList, &QListWidget::itemClicked,
this, &MainWindow::updateText);
updateText(headingList->item(0));
headingList->setCurrentRow(0);
@ -119,7 +119,7 @@ void MainWindow::setupMenus()
QAction *exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
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"));
fileMenu->addAction(exitAct);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -48,7 +48,7 @@
**
****************************************************************************/
#include <QtGui>
#include <QtWidgets>
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");
QLineEdit *patternEditor = new QLineEdit;
QObject::
connect(patternEditor, SIGNAL(textChanged(QString)),
filterModel, SLOT(setFilterRegExp(QString)));
QObject::connect(patternEditor, &QLineEdit::textChanged,
filterModel, &QSortFilterProxyModel::setFilterWildcard);
QVBoxLayout *layout = new QVBoxLayout(window);
layout->addWidget(filteredView);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1003,8 +1003,8 @@
\snippet reading-selections/window.cpp 0
The above code uses Qt's convenient \l{Container Classes}{foreach
keyword} to iterate over, and modify, the items corresponding to the
The above code uses a range-based for-loop to iterate over,
and modify, the items corresponding to the
indexes returned by the selection model.
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()
function:
\snippet qtreewidget-using/mainwindow.cpp 6
\snippet qtreewidget-using/mainwindow.cpp 7
The above code causes items in a tree widget to be selected if they