**
****************************************************************************/
-#include <QtWidgets>
#include "adddialog.h"
+#include <QtWidgets>
+
//! [0]
AddDialog::AddDialog(QWidget *parent)
: QDialog(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"));
}
Q_OBJECT
public:
- AddDialog(QWidget *parent=0);
+ AddDialog(QWidget *parent = 0);
QLineEdit *nameText;
QTextEdit *addressText;
};
//! [0]
-#endif
+#endif // ADDDIALOG_H
**
****************************************************************************/
-#include <QtWidgets>
-#include "addresswidget.h"
#include "adddialog.h"
+#include "addresswidget.h"
+
+#include <QtWidgets>
//! [0]
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");
//! [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)) {
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]
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);
}
}
}
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());
}
//! [1]
//! [7]
-void AddressWidget::readFromFile(QString fileName)
+void AddressWidget::readFromFile(const QString &fileName)
{
QFile file(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);
//! [7]
//! [6]
-void AddressWidget::writeToFile(QString fileName)
+void AddressWidget::writeToFile(const QString &fileName)
{
QFile file(fileName);
return;
}
- QList< QPair<QString, QString> > pairs = table->getList();
+ QList<QPair<QString, QString> > pairs = table->getList();
QDataStream out(&file);
out << pairs;
}
#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;
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();
};
//! [0]
-#endif
+#endif // ADDRESSWIDGET_H
**
****************************************************************************/
-#include <QtWidgets>
#include "mainwindow.h"
+#include <QApplication>
+
//! [0]
int main(int argc, char *argv[])
{
**
****************************************************************************/
-#include <QtWidgets>
#include "mainwindow.h"
+#include <QAction>
+#include <QFileDialog>
+#include <QMenuBar>
+
//! [0]
MainWindow::MainWindow()
{
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)));
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this);
- if (!fileName.isEmpty()) {
+ if (!fileName.isEmpty())
addressWidget->readFromFile(fileName);
- }
}
//! [2]
void MainWindow::saveFile()
{
QString fileName = QFileDialog::getSaveFileName(this);
- if (!fileName.isEmpty()) {
+ if (!fileName.isEmpty())
addressWidget->writeToFile(fileName);
- }
}
//! [3]
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
-#include <QtWidgets>
#include "addresswidget.h"
+#include <QMainWindow>
+
//! [0]
class MainWindow : public QMainWindow
{
};
//! [0]
-#endif
+#endif // MAINWINDOW_H
**
****************************************************************************/
-#include <QtWidgets>
-#include "newaddresstab.h"
#include "adddialog.h"
+#include "newaddresstab.h"
+
+#include <QtWidgets>
//! [0]
NewAddressTab::NewAddressTab(QWidget *parent)
Q_OBJECT
public:
- NewAddressTab(QWidget *parent=0);
+ NewAddressTab(QWidget *parent = 0);
public slots:
void addEntry();
{
}
-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]
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);
}
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);
}
#define TABLEMODEL_H
#include <QAbstractTableModel>
-#include <QPair>
#include <QList>
+#include <QPair>
//! [0]
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