QtBase: examples/widgets/itemviews/addressbook codestyle

Change-Id: I710d67018351c34ef14ac30edcca81aba7ff5ad3
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
David Schulz 2012-11-21 15:45:13 +01:00 committed by The Qt Project
parent 8a83c1bb55
commit 78a239bc6d
11 changed files with 69 additions and 73 deletions

View File

@ -38,9 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "adddialog.h" #include "adddialog.h"
#include <QtWidgets>
//! [0] //! [0]
AddDialog::AddDialog(QWidget *parent) AddDialog::AddDialog(QWidget *parent)
: QDialog(parent) : QDialog(parent)
@ -71,11 +72,8 @@ AddDialog::AddDialog(QWidget *parent)
mainLayout->addLayout(gLayout); mainLayout->addLayout(gLayout);
setLayout(mainLayout); setLayout(mainLayout);
connect(okButton, SIGNAL(clicked()), connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
this, SLOT(accept())); connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
connect(cancelButton, SIGNAL(clicked()),
this, SLOT(reject()));
setWindowTitle(tr("Add a Contact")); setWindowTitle(tr("Add a Contact"));
} }

View File

@ -56,7 +56,7 @@ class AddDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
AddDialog(QWidget *parent=0); AddDialog(QWidget *parent = 0);
QLineEdit *nameText; QLineEdit *nameText;
QTextEdit *addressText; QTextEdit *addressText;
@ -68,4 +68,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // ADDDIALOG_H

View File

@ -38,9 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "addresswidget.h"
#include "adddialog.h" #include "adddialog.h"
#include "addresswidget.h"
#include <QtWidgets>
//! [0] //! [0]
AddressWidget::AddressWidget(QWidget *parent) AddressWidget::AddressWidget(QWidget *parent)
@ -48,8 +49,8 @@ AddressWidget::AddressWidget(QWidget *parent)
{ {
table = new TableModel(this); table = new TableModel(this);
newAddressTab = new NewAddressTab(this); newAddressTab = new NewAddressTab(this);
connect(newAddressTab, SIGNAL(sendDetails(QString,QString)), connect(newAddressTab, SIGNAL(sendDetails(QString, QString)),
this, SLOT(addEntry(QString,QString))); this, SLOT(addEntry(QString, QString)));
addTab(newAddressTab, "Address Book"); addTab(newAddressTab, "Address Book");
@ -74,7 +75,7 @@ void AddressWidget::addEntry()
//! [3] //! [3]
void AddressWidget::addEntry(QString name, QString address) void AddressWidget::addEntry(QString name, QString address)
{ {
QList< QPair<QString, QString> >list = table->getList(); QList<QPair<QString, QString> >list = table->getList();
QPair<QString, QString> pair(name, address); QPair<QString, QString> pair(name, address);
if (!list.contains(pair)) { if (!list.contains(pair)) {
@ -100,19 +101,18 @@ void AddressWidget::editEntry()
QItemSelectionModel *selectionModel = temp->selectionModel(); QItemSelectionModel *selectionModel = temp->selectionModel();
QModelIndexList indexes = selectionModel->selectedRows(); QModelIndexList indexes = selectionModel->selectedRows();
QModelIndex index, i;
QString name; QString name;
QString address; QString address;
int row = -1; int row = -1;
foreach (index, indexes) { foreach (QModelIndex index, indexes) {
row = proxy->mapToSource(index).row(); row = proxy->mapToSource(index).row();
i = table->index(row, 0, QModelIndex()); QModelIndex nameIndex = table->index(row, 0, QModelIndex());
QVariant varName = table->data(i, Qt::DisplayRole); QVariant varName = table->data(nameIndex, Qt::DisplayRole);
name = varName.toString(); name = varName.toString();
i = table->index(row, 1, QModelIndex()); QModelIndex addressIndex = table->index(row, 1, QModelIndex());
QVariant varAddr = table->data(i, Qt::DisplayRole); QVariant varAddr = table->data(addressIndex, Qt::DisplayRole);
address = varAddr.toString(); address = varAddr.toString();
} }
//! [4a] //! [4a]
@ -128,8 +128,8 @@ void AddressWidget::editEntry()
if (aDialog.exec()) { if (aDialog.exec()) {
QString newAddress = aDialog.addressText->toPlainText(); QString newAddress = aDialog.addressText->toPlainText();
if (newAddress != address) { if (newAddress != address) {
i = table->index(row, 1, QModelIndex()); QModelIndex index = table->index(row, 1, QModelIndex());
table->setData(i, newAddress, Qt::EditRole); table->setData(index, newAddress, Qt::EditRole);
} }
} }
} }
@ -143,9 +143,8 @@ void AddressWidget::removeEntry()
QItemSelectionModel *selectionModel = temp->selectionModel(); QItemSelectionModel *selectionModel = temp->selectionModel();
QModelIndexList indexes = selectionModel->selectedRows(); QModelIndexList indexes = selectionModel->selectedRows();
QModelIndex index;
foreach (index, indexes) { foreach (QModelIndex index, indexes) {
int row = proxy->mapToSource(index).row(); int row = proxy->mapToSource(index).row();
table->removeRows(row, 1, QModelIndex()); table->removeRows(row, 1, QModelIndex());
} }
@ -193,7 +192,7 @@ void AddressWidget::setupTabs()
//! [1] //! [1]
//! [7] //! [7]
void AddressWidget::readFromFile(QString fileName) void AddressWidget::readFromFile(const QString &fileName)
{ {
QFile file(fileName); QFile file(fileName);
@ -203,7 +202,7 @@ void AddressWidget::readFromFile(QString fileName)
return; return;
} }
QList< QPair<QString, QString> > pairs = table->getList(); QList<QPair<QString, QString> > pairs = table->getList();
QDataStream in(&file); QDataStream in(&file);
in >> pairs; in >> pairs;
@ -220,7 +219,7 @@ void AddressWidget::readFromFile(QString fileName)
//! [7] //! [7]
//! [6] //! [6]
void AddressWidget::writeToFile(QString fileName) void AddressWidget::writeToFile(const QString &fileName)
{ {
QFile file(fileName); QFile file(fileName);
@ -229,7 +228,7 @@ void AddressWidget::writeToFile(QString fileName)
return; return;
} }
QList< QPair<QString, QString> > pairs = table->getList(); QList<QPair<QString, QString> > pairs = table->getList();
QDataStream out(&file); QDataStream out(&file);
out << pairs; out << pairs;
} }

View File

@ -41,10 +41,11 @@
#ifndef ADDRESSWIDGET_H #ifndef ADDRESSWIDGET_H
#define ADDRESSWIDGET_H #define ADDRESSWIDGET_H
#include <QTabWidget>
#include <QItemSelection>
#include "tablemodel.h"
#include "newaddresstab.h" #include "newaddresstab.h"
#include "tablemodel.h"
#include <QItemSelection>
#include <QTabWidget>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QSortFilterProxyModel; class QSortFilterProxyModel;
@ -57,9 +58,9 @@ class AddressWidget : public QTabWidget
Q_OBJECT Q_OBJECT
public: public:
AddressWidget(QWidget *parent=0); AddressWidget(QWidget *parent = 0);
void readFromFile(QString fileName); void readFromFile(const QString &fileName);
void writeToFile(QString fileName); void writeToFile(const QString &fileName);
public slots: public slots:
void addEntry(); void addEntry();
@ -79,4 +80,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // ADDRESSWIDGET_H

View File

@ -38,9 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
#include <QApplication>
//! [0] //! [0]
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View File

@ -38,9 +38,12 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "mainwindow.h" #include "mainwindow.h"
#include <QAction>
#include <QFileDialog>
#include <QMenuBar>
//! [0] //! [0]
MainWindow::MainWindow() MainWindow::MainWindow()
{ {
@ -58,43 +61,37 @@ void MainWindow::createMenus()
openAct = new QAction(tr("&Open..."), this); openAct = new QAction(tr("&Open..."), this);
fileMenu->addAction(openAct); fileMenu->addAction(openAct);
connect(openAct, SIGNAL(triggered()), connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));
this, SLOT(openFile()));
//! [1a] //! [1a]
saveAct = new QAction(tr("&Save As..."), this); saveAct = new QAction(tr("&Save As..."), this);
fileMenu->addAction(saveAct); fileMenu->addAction(saveAct);
connect(saveAct, SIGNAL(triggered()), connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile()));
this, SLOT(saveFile()));
fileMenu->addSeparator(); fileMenu->addSeparator();
exitAct = new QAction(tr("E&xit"), this); exitAct = new QAction(tr("E&xit"), this);
fileMenu->addAction(exitAct); fileMenu->addAction(exitAct);
connect(exitAct, SIGNAL(triggered()), connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
this, SLOT(close()));
toolMenu = menuBar()->addMenu(tr("&Tools")); toolMenu = menuBar()->addMenu(tr("&Tools"));
addAct = new QAction(tr("&Add Entry..."), this); addAct = new QAction(tr("&Add Entry..."), this);
toolMenu->addAction(addAct); toolMenu->addAction(addAct);
connect(addAct, SIGNAL(triggered()), connect(addAct, SIGNAL(triggered()), addressWidget, SLOT(addEntry()));
addressWidget, SLOT(addEntry()));
//! [1b] //! [1b]
editAct = new QAction(tr("&Edit Entry..."), this); editAct = new QAction(tr("&Edit Entry..."), this);
editAct->setEnabled(false); editAct->setEnabled(false);
toolMenu->addAction(editAct); toolMenu->addAction(editAct);
connect(editAct, SIGNAL(triggered()), connect(editAct, SIGNAL(triggered()), addressWidget, SLOT(editEntry()));
addressWidget, SLOT(editEntry()));
toolMenu->addSeparator(); toolMenu->addSeparator();
removeAct = new QAction(tr("&Remove Entry"), this); removeAct = new QAction(tr("&Remove Entry"), this);
removeAct->setEnabled(false); removeAct->setEnabled(false);
toolMenu->addAction(removeAct); toolMenu->addAction(removeAct);
connect(removeAct, SIGNAL(triggered()), connect(removeAct, SIGNAL(triggered()), addressWidget, SLOT(removeEntry()));
addressWidget, SLOT(removeEntry()));
connect(addressWidget, SIGNAL(selectionChanged(QItemSelection)), connect(addressWidget, SIGNAL(selectionChanged(QItemSelection)),
this, SLOT(updateActions(QItemSelection))); this, SLOT(updateActions(QItemSelection)));
@ -105,9 +102,8 @@ void MainWindow::createMenus()
void MainWindow::openFile() void MainWindow::openFile()
{ {
QString fileName = QFileDialog::getOpenFileName(this); QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty()) { if (!fileName.isEmpty())
addressWidget->readFromFile(fileName); addressWidget->readFromFile(fileName);
}
} }
//! [2] //! [2]
@ -115,9 +111,8 @@ void MainWindow::openFile()
void MainWindow::saveFile() void MainWindow::saveFile()
{ {
QString fileName = QFileDialog::getSaveFileName(this); QString fileName = QFileDialog::getSaveFileName(this);
if (!fileName.isEmpty()) { if (!fileName.isEmpty())
addressWidget->writeToFile(fileName); addressWidget->writeToFile(fileName);
}
} }
//! [3] //! [3]

View File

@ -41,9 +41,10 @@
#ifndef MAINWINDOW_H #ifndef MAINWINDOW_H
#define MAINWINDOW_H #define MAINWINDOW_H
#include <QtWidgets>
#include "addresswidget.h" #include "addresswidget.h"
#include <QMainWindow>
//! [0] //! [0]
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
@ -72,4 +73,4 @@ private:
}; };
//! [0] //! [0]
#endif #endif // MAINWINDOW_H

View File

@ -38,9 +38,10 @@
** **
****************************************************************************/ ****************************************************************************/
#include <QtWidgets>
#include "newaddresstab.h"
#include "adddialog.h" #include "adddialog.h"
#include "newaddresstab.h"
#include <QtWidgets>
//! [0] //! [0]
NewAddressTab::NewAddressTab(QWidget *parent) NewAddressTab::NewAddressTab(QWidget *parent)

View File

@ -55,7 +55,7 @@ class NewAddressTab : public QWidget
Q_OBJECT Q_OBJECT
public: public:
NewAddressTab(QWidget *parent=0); NewAddressTab(QWidget *parent = 0);
public slots: public slots:
void addEntry(); void addEntry();

View File

@ -46,10 +46,10 @@ TableModel::TableModel(QObject *parent)
{ {
} }
TableModel::TableModel(QList< QPair<QString, QString> > pairs, QObject *parent) TableModel::TableModel(QList<QPair<QString, QString> > pairs, QObject *parent)
: QAbstractTableModel(parent) : QAbstractTableModel(parent)
{ {
listOfPairs=pairs; listOfPairs = pairs;
} }
//! [0] //! [0]
@ -114,9 +114,9 @@ QVariant TableModel::headerData(int section, Qt::Orientation orientation, int ro
bool TableModel::insertRows(int position, int rows, const QModelIndex &index) bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
{ {
Q_UNUSED(index); Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1); beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row=0; row < rows; row++) { for (int row = 0; row < rows; ++row) {
QPair<QString, QString> pair(" ", " "); QPair<QString, QString> pair(" ", " ");
listOfPairs.insert(position, pair); listOfPairs.insert(position, pair);
} }
@ -130,9 +130,9 @@ bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
bool TableModel::removeRows(int position, int rows, const QModelIndex &index) bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
{ {
Q_UNUSED(index); Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position+rows-1); beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row=0; row < rows; ++row) { for (int row = 0; row < rows; ++row) {
listOfPairs.removeAt(position); listOfPairs.removeAt(position);
} }

View File

@ -42,8 +42,8 @@
#define TABLEMODEL_H #define TABLEMODEL_H
#include <QAbstractTableModel> #include <QAbstractTableModel>
#include <QPair>
#include <QList> #include <QList>
#include <QPair>
//! [0] //! [0]
class TableModel : public QAbstractTableModel class TableModel : public QAbstractTableModel
@ -51,22 +51,22 @@ class TableModel : public QAbstractTableModel
Q_OBJECT Q_OBJECT
public: public:
TableModel(QObject *parent=0); TableModel(QObject *parent = 0);
TableModel(QList< QPair<QString, QString> > listofPairs, QObject *parent=0); TableModel(QList<QPair<QString, QString> > listofPairs, QObject *parent = 0);
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const; QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const;
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole); bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex()); bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()); bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
QList< QPair<QString, QString> > getList(); QList<QPair<QString, QString> > getList();
private: private:
QList< QPair<QString, QString> > listOfPairs; QList<QPair<QString, QString> > listOfPairs;
}; };
//! [0] //! [0]
#endif #endif // TABLEMODEL_H