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 <QtWidgets>
//! [0]
AddDialog::AddDialog(QWidget *parent)
: QDialog(parent)
@ -71,11 +72,8 @@ AddDialog::AddDialog(QWidget *parent)
mainLayout->addLayout(gLayout);
setLayout(mainLayout);
connect(okButton, SIGNAL(clicked()),
this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()),
this, SLOT(reject()));
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
setWindowTitle(tr("Add a Contact"));
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -55,7 +55,7 @@ class NewAddressTab : public QWidget
Q_OBJECT
public:
NewAddressTab(QWidget *parent=0);
NewAddressTab(QWidget *parent = 0);
public slots:
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)
{
listOfPairs=pairs;
listOfPairs = pairs;
}
//! [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)
{
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(" ", " ");
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)
{
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);
}

View File

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