diff options
author | David Boddie <dboddie@trolltech.com> | 2010-07-13 17:04:31 +0200 |
---|---|---|
committer | David Boddie <dboddie@trolltech.com> | 2010-07-13 17:04:31 +0200 |
commit | 5608f5c35dd3f4470f51436ead9a7048d561affa (patch) | |
tree | 4a797f9b89b8f811345ddb9752ef6cf391da6336 /examples/tutorials | |
parent | 3e326e54856b1fce33ab2c005de2a46af6a4ea0f (diff) | |
download | qt4-tools-5608f5c35dd3f4470f51436ead9a7048d561affa.tar.gz |
Doc: Reviewed Michael's model/view tutorial and overview document.
Reviewed-by: Trust Me
Diffstat (limited to 'examples/tutorials')
18 files changed, 67 insertions, 82 deletions
diff --git a/examples/tutorials/modelview/1_readonly/main.cpp b/examples/tutorials/modelview/1_readonly/main.cpp index ad11f38d4b..fb4726aa69 100755 --- a/examples/tutorials/modelview/1_readonly/main.cpp +++ b/examples/tutorials/modelview/1_readonly/main.cpp @@ -39,6 +39,7 @@ ****************************************************************************/ //! [Quoting ModelView Tutorial] +// main.cpp #include <QtGui/QApplication> #include "modelview.h" diff --git a/examples/tutorials/modelview/1_readonly/modelview.cpp b/examples/tutorials/modelview/1_readonly/modelview.cpp index 027be561da..91a97bf205 100755 --- a/examples/tutorials/modelview/1_readonly/modelview.cpp +++ b/examples/tutorials/modelview/1_readonly/modelview.cpp @@ -39,6 +39,7 @@ ****************************************************************************/ //! [Quoting ModelView Tutorial] +// modelview.cpp #include <QTableView> #include "modelview.h" #include "mymodel.h" @@ -48,6 +49,6 @@ ModelView::ModelView(QWidget *parent) { tableView = new QTableView(this); setCentralWidget(tableView); - tableView->setModel(new MyModel(this) ); + tableView->setModel(new MyModel(this)); } -//! [Quoting ModelView Tutorial]
\ No newline at end of file +//! [Quoting ModelView Tutorial] diff --git a/examples/tutorials/modelview/1_readonly/modelview.h b/examples/tutorials/modelview/1_readonly/modelview.h index d0f96cdc6a..93070830a0 100755 --- a/examples/tutorials/modelview/1_readonly/modelview.h +++ b/examples/tutorials/modelview/1_readonly/modelview.h @@ -38,11 +38,11 @@ ** ****************************************************************************/ -//! [Quoting ModelView Tutorial] #ifndef MODELVIEW_H #define MODELVIEW_H - +//! [Quoting ModelView Tutorial] +// modelview.h #include <QtGui/QMainWindow> class QTableView; //forward declaration @@ -56,6 +56,6 @@ public: ModelView(QWidget *parent = 0); }; +//! [Quoting ModelView Tutorial] #endif // MODELVIEW_H -//! [Quoting ModelView Tutorial]
\ No newline at end of file diff --git a/examples/tutorials/modelview/1_readonly/mymodel.cpp b/examples/tutorials/modelview/1_readonly/mymodel.cpp index c4417201bf..394605abc9 100755 --- a/examples/tutorials/modelview/1_readonly/mymodel.cpp +++ b/examples/tutorials/modelview/1_readonly/mymodel.cpp @@ -39,6 +39,7 @@ ****************************************************************************/ //! [Quoting ModelView Tutorial] +// mymodel.cpp #include "mymodel.h" MyModel::MyModel(QObject *parent) @@ -46,22 +47,19 @@ MyModel::MyModel(QObject *parent) { } -//------------------------------------------------------- -int MyModel::rowCount(const QModelIndex & /*parent*/ ) const +int MyModel::rowCount(const QModelIndex & /*parent*/) const { return 2; } -//------------------------------------------------------- -int MyModel::columnCount(const QModelIndex & /*parent*/ ) const +int MyModel::columnCount(const QModelIndex & /*parent*/) const { return 3; } -//------------------------------------------------------- -QVariant MyModel::data(const QModelIndex &index, int role ) const +QVariant MyModel::data(const QModelIndex &index, int role) const { - if(role == Qt::DisplayRole) + if (role == Qt::DisplayRole) { return QString("Row%1, Column%2") .arg(index.row() + 1) @@ -69,4 +67,4 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const } return QVariant(); } -//! [Quoting ModelView Tutorial]
\ No newline at end of file +//! [Quoting ModelView Tutorial] diff --git a/examples/tutorials/modelview/1_readonly/mymodel.h b/examples/tutorials/modelview/1_readonly/mymodel.h index c0ddf4ac63..6065f6e7c0 100755 --- a/examples/tutorials/modelview/1_readonly/mymodel.h +++ b/examples/tutorials/modelview/1_readonly/mymodel.h @@ -38,10 +38,11 @@ ** ****************************************************************************/ -//! [Quoting ModelView Tutorial] #ifndef MYMODEL_H #define MYMODEL_H +//! [Quoting ModelView Tutorial] +// mymodel.h #include <QAbstractTableModel> class MyModel : public QAbstractTableModel @@ -53,6 +54,6 @@ public: int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; }; +//! [Quoting ModelView Tutorial] #endif // MYMODEL_H -//! [Quoting ModelView Tutorial]
\ No newline at end of file diff --git a/examples/tutorials/modelview/2_formatting/modelview.cpp b/examples/tutorials/modelview/2_formatting/modelview.cpp index 2b05d4c6f3..9a5ce64c96 100755 --- a/examples/tutorials/modelview/2_formatting/modelview.cpp +++ b/examples/tutorials/modelview/2_formatting/modelview.cpp @@ -47,6 +47,6 @@ ModelView::ModelView(QWidget *parent) { tableView = new QTableView(this); setCentralWidget(tableView); - tableView->setModel(new MyModel(this) ); + tableView->setModel(new MyModel(this)); } diff --git a/examples/tutorials/modelview/2_formatting/mymodel.cpp b/examples/tutorials/modelview/2_formatting/mymodel.cpp index e9e68def97..e34e0147f4 100755 --- a/examples/tutorials/modelview/2_formatting/mymodel.cpp +++ b/examples/tutorials/modelview/2_formatting/mymodel.cpp @@ -44,25 +44,23 @@ #include <QDebug> //! [Quoting ModelView Tutorial] +// mymodel.cpp MyModel::MyModel(QObject *parent) :QAbstractTableModel(parent) { } -//------------------------------------------------------- -int MyModel::rowCount(const QModelIndex & /*parent */ ) const +int MyModel::rowCount(const QModelIndex & /*parent */) const { return 2; } -//------------------------------------------------------- -int MyModel::columnCount(const QModelIndex & /*parent */ ) const +int MyModel::columnCount(const QModelIndex & /*parent */) const { return 3; } -//------------------------------------------------------- -QVariant MyModel::data(const QModelIndex &index, int role ) const +QVariant MyModel::data(const QModelIndex &index, int role) const { int row = index.row(); int col = index.column(); @@ -72,15 +70,15 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const switch(role){ case Qt::DisplayRole: - if(row == 0 && col == 1 )return QString("<--left"); - if(row == 1 && col == 1 )return QString("right-->"); + if (row == 0 && col == 1) return QString("<--left"); + if (row == 1 && col == 1) return QString("right-->"); return QString("Row%1, Column%2") .arg(row + 1) .arg(col +1); break; case Qt::FontRole: - if(row == 0 && col ==0 ) //change font only for cell(0,0) + if (row == 0 && col == 0) //change font only for cell(0,0) { QFont boldFont; boldFont.setBold(true); @@ -89,7 +87,7 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const break; case Qt::BackgroundRole: - if(row == 1 && col ==2 ) //change background only for cell(1,2) + if (row == 1 && col == 2) //change background only for cell(1,2) { QBrush redBackground(QColor(Qt::red)); return redBackground; @@ -97,14 +95,14 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const break; case Qt::TextAlignmentRole: - if(row == 1 && col ==1 ) //change text alignment only for cell(1,1) + if (row == 1 && col == 1) //change text alignment only for cell(1,1) { return Qt::AlignRight + Qt::AlignVCenter; } break; case Qt::CheckStateRole: - if(row == 1 && col ==0 ) //add a checkbox to cell(1,0) + if (row == 1 && col == 0) //add a checkbox to cell(1,0) { return Qt::Checked; } diff --git a/examples/tutorials/modelview/3_changingmodel/modelview.cpp b/examples/tutorials/modelview/3_changingmodel/modelview.cpp index 2b05d4c6f3..9a5ce64c96 100755 --- a/examples/tutorials/modelview/3_changingmodel/modelview.cpp +++ b/examples/tutorials/modelview/3_changingmodel/modelview.cpp @@ -47,6 +47,6 @@ ModelView::ModelView(QWidget *parent) { tableView = new QTableView(this); setCentralWidget(tableView); - tableView->setModel(new MyModel(this) ); + tableView->setModel(new MyModel(this)); } diff --git a/examples/tutorials/modelview/3_changingmodel/mymodel.cpp b/examples/tutorials/modelview/3_changingmodel/mymodel.cpp index d80694512b..42915b09f1 100755 --- a/examples/tutorials/modelview/3_changingmodel/mymodel.cpp +++ b/examples/tutorials/modelview/3_changingmodel/mymodel.cpp @@ -50,32 +50,32 @@ MyModel::MyModel(QObject *parent) // selectedCell = 0; timer = new QTimer(this); timer->setInterval(1000); - connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit()) ); + connect(timer, SIGNAL(timeout()) , this, SLOT(timerHit())); timer->start(); } //! [quoting mymodel_a] //------------------------------------------------------- -int MyModel::rowCount(const QModelIndex & /*parent */ ) const +int MyModel::rowCount(const QModelIndex & /*parent */) const { return 2; } //------------------------------------------------------- -int MyModel::columnCount(const QModelIndex & /*parent */ ) const +int MyModel::columnCount(const QModelIndex & /*parent */) const { return 3; } //------------------------------------------------------- //! [quoting mymodel_QVariant ] -QVariant MyModel::data(const QModelIndex &index, int role ) const +QVariant MyModel::data(const QModelIndex &index, int role) const { int row = index.row(); int col = index.column(); - if(role == Qt::DisplayRole) + if (role == Qt::DisplayRole) { - if(row == 0 && col == 0 ) + if (row == 0 && col == 0) { return QTime::currentTime().toString(); } @@ -88,8 +88,8 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const void MyModel::timerHit() { //we identify the top left cell - QModelIndex topLeft = createIndex ( 0,0 ); + QModelIndex topLeft = createIndex(0,0); //emit a signal to make the view reread identified data - emit dataChanged ( topLeft, topLeft ); + emit dataChanged(topLeft, topLeft); } //! [quoting mymodel_b ] diff --git a/examples/tutorials/modelview/4_headers/modelview.cpp b/examples/tutorials/modelview/4_headers/modelview.cpp index f661ab5f17..449dbbcc41 100755 --- a/examples/tutorials/modelview/4_headers/modelview.cpp +++ b/examples/tutorials/modelview/4_headers/modelview.cpp @@ -48,7 +48,7 @@ ModelView::ModelView(QWidget *parent) { tableView = new QTableView(this); setCentralWidget(tableView); - tableView->setModel(new MyModel(this) ); + tableView->setModel(new MyModel(this)); tableView->verticalHeader()->hide(); } diff --git a/examples/tutorials/modelview/4_headers/mymodel.cpp b/examples/tutorials/modelview/4_headers/mymodel.cpp index 94fde34b65..e6f977d012 100755 --- a/examples/tutorials/modelview/4_headers/mymodel.cpp +++ b/examples/tutorials/modelview/4_headers/mymodel.cpp @@ -46,21 +46,21 @@ MyModel::MyModel(QObject *parent) } //------------------------------------------------------- -int MyModel::rowCount(const QModelIndex & /*parent*/ ) const +int MyModel::rowCount(const QModelIndex & /*parent*/) const { return 2; } //------------------------------------------------------- -int MyModel::columnCount(const QModelIndex & /*parent*/ ) const +int MyModel::columnCount(const QModelIndex & /*parent*/) const { return 3; } //------------------------------------------------------- -QVariant MyModel::data(const QModelIndex &index, int role ) const +QVariant MyModel::data(const QModelIndex &index, int role) const { - if(role == Qt::DisplayRole) + if (role == Qt::DisplayRole) { return QString("Row%1, Column%2") .arg(index.row() + 1) @@ -88,4 +88,4 @@ QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) } return QVariant(); } -//! [quoting mymodel_c]
\ No newline at end of file +//! [quoting mymodel_c] diff --git a/examples/tutorials/modelview/5_edit/modelview.cpp b/examples/tutorials/modelview/5_edit/modelview.cpp index d8853c997d..a6c6ef533f 100755 --- a/examples/tutorials/modelview/5_edit/modelview.cpp +++ b/examples/tutorials/modelview/5_edit/modelview.cpp @@ -48,13 +48,13 @@ ModelView::ModelView(QWidget *parent) tableView = new QTableView(this); setCentralWidget(tableView); QAbstractTableModel *myModel = new MyModel(this); - tableView->setModel( myModel ); + tableView->setModel(myModel); //transfer changes to the model to the window title - connect(myModel, SIGNAL(editCompleted(const QString &) ), this, SLOT(setWindowTitle(const QString &))); + connect(myModel, SIGNAL(editCompleted(const QString &)), this, SLOT(setWindowTitle(const QString &))); } void ModelView::showWindowTitle(const QString & title) { -setWindowTitle( title ); +setWindowTitle(title); } diff --git a/examples/tutorials/modelview/5_edit/mymodel.cpp b/examples/tutorials/modelview/5_edit/mymodel.cpp index 6007da1a36..67181ca94d 100755 --- a/examples/tutorials/modelview/5_edit/mymodel.cpp +++ b/examples/tutorials/modelview/5_edit/mymodel.cpp @@ -53,22 +53,19 @@ MyModel::MyModel(QObject *parent) //! [quoting mymodel_d] //! [quoting mymodel_e] -//------------------------------------------------------- -int MyModel::rowCount(const QModelIndex & /*parent*/ ) const +int MyModel::rowCount(const QModelIndex & /*parent*/) const { return ROWS; } -//------------------------------------------------------- -int MyModel::columnCount(const QModelIndex & /*parent*/ ) const +int MyModel::columnCount(const QModelIndex & /*parent*/) const { return COLS; } -//------------------------------------------------------- -QVariant MyModel::data(const QModelIndex &index, int role ) const +QVariant MyModel::data(const QModelIndex &index, int role) const { - if(role == Qt::DisplayRole) + if (role == Qt::DisplayRole) { return m_gridData[modelIndexToOffset(index)]; } @@ -79,23 +76,21 @@ QVariant MyModel::data(const QModelIndex &index, int role ) const //----------------------------------------------------------------- //! [quoting mymodel_f] -bool MyModel::setData ( const QModelIndex & index, const QVariant & value, int role ) +bool MyModel::setData(const QModelIndex & index, const QVariant & value, int role) { - if(role == Qt::EditRole) + if (role == Qt::EditRole) { m_gridData[modelIndexToOffset(index)] = value.toString(); - emit editCompleted(m_gridData.join(" | ") ); + emit editCompleted(m_gridData.join(" | ")); } return true; } -//----------------------------------------------------------------- -Qt::ItemFlags MyModel::flags ( const QModelIndex & /*index*/ ) const +Qt::ItemFlags MyModel::flags(const QModelIndex & /*index*/) const { return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ; } -//----------------------------------------------------------------- //convert row and column information to array offset int MyModel::modelIndexToOffset(const QModelIndex & index) const { diff --git a/examples/tutorials/modelview/5_edit/mymodel.h b/examples/tutorials/modelview/5_edit/mymodel.h index 54f2b30925..0d2a1b881f 100755 --- a/examples/tutorials/modelview/5_edit/mymodel.h +++ b/examples/tutorials/modelview/5_edit/mymodel.h @@ -38,10 +38,11 @@ ** ****************************************************************************/ -//! [Quoting ModelView Tutorial] #ifndef MYMODEL_H #define MYMODEL_H +//! [Quoting ModelView Tutorial] +// mymodel.h #include <QAbstractTableModel> #include <QStringList> @@ -53,14 +54,14 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const ; int columnCount(const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; - bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ); - Qt::ItemFlags flags ( const QModelIndex & index ) const ; + bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole); + Qt::ItemFlags flags(const QModelIndex & index) const ; private: QStringList m_gridData; //holds text entered into QTableView int modelIndexToOffset(const QModelIndex & index) const; signals: void editCompleted(const QString &); }; +//! [Quoting ModelView Tutorial] #endif // MYMODEL_H -//! [Quoting ModelView Tutorial]
\ No newline at end of file diff --git a/examples/tutorials/modelview/6_treeview/modelview.cpp b/examples/tutorials/modelview/6_treeview/modelview.cpp index a25e4e911b..772dbdd1d3 100755 --- a/examples/tutorials/modelview/6_treeview/modelview.cpp +++ b/examples/tutorials/modelview/6_treeview/modelview.cpp @@ -39,6 +39,7 @@ ****************************************************************************/ //! [Quoting ModelView Tutorial] +// modelview.cpp #include <QTreeView> #include <QStandardItemModel> #include <QStandardItem> @@ -64,14 +65,13 @@ ModelView::ModelView(QWidget *parent) // adding a row to an item starts a subtree preparedColumn.first()->appendRow(secondRow); - treeView->setModel( standardModel ); + treeView->setModel(standardModel); treeView->expandAll(); } -//--------------------------------------------------------------------------- QList<QStandardItem *> ModelView::prepareColumn(const QString &first, const QString &second, - const QString &third ) + const QString &third) { QList<QStandardItem *> colItems; colItems << new QStandardItem(first); @@ -79,4 +79,4 @@ QList<QStandardItem *> ModelView::prepareColumn(const QString &first, colItems << new QStandardItem(third); return colItems; } -//! [Quoting ModelView Tutorial]
\ No newline at end of file +//! [Quoting ModelView Tutorial] diff --git a/examples/tutorials/modelview/6_treeview/modelview.h b/examples/tutorials/modelview/6_treeview/modelview.h index 2329e89017..a47111c5c9 100755 --- a/examples/tutorials/modelview/6_treeview/modelview.h +++ b/examples/tutorials/modelview/6_treeview/modelview.h @@ -56,7 +56,7 @@ private: QStandardItemModel *standardModel; QList<QStandardItem *> prepareColumn(const QString &first, const QString &second, - const QString &third ); + const QString &third); public: ModelView(QWidget *parent = 0); }; diff --git a/examples/tutorials/modelview/7_selections/modelview.cpp b/examples/tutorials/modelview/7_selections/modelview.cpp index 2ef980d9fc..3b373c6bd6 100755 --- a/examples/tutorials/modelview/7_selections/modelview.cpp +++ b/examples/tutorials/modelview/7_selections/modelview.cpp @@ -74,13 +74,13 @@ ModelView::ModelView(QWidget *parent) italyItem-> appendRow(veronaItem); //register the model - treeView->setModel( standardModel ); + treeView->setModel(standardModel); treeView->expandAll(); //selection changes shall trigger a slot QItemSelectionModel *selectionModel= treeView->selectionModel(); - connect(selectionModel, SIGNAL(selectionChanged ( const QItemSelection & , const QItemSelection & )), - this, SLOT(selectionChangedSlot(const QItemSelection & , const QItemSelection & ))); + connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)), + this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &))); } //! [quoting modelview_a] @@ -93,7 +93,7 @@ void ModelView::selectionChangedSlot(const QItemSelection & /*newSelection*/, co QString selectedText = index.data(Qt::DisplayRole).toString(); int hierarchyLevel=1; QModelIndex seekRoot = index; - while(seekRoot.parent() != QModelIndex() ) + while(seekRoot.parent() != QModelIndex()) { seekRoot = seekRoot.parent(); hierarchyLevel++; diff --git a/examples/tutorials/modelview/qmake.pro b/examples/tutorials/modelview/qmake.pro deleted file mode 100755 index 7f684bac18..0000000000 --- a/examples/tutorials/modelview/qmake.pro +++ /dev/null @@ -1,10 +0,0 @@ -TEMPLATE = subdirs - -SUBDIRS = 1_readonly \ - 2_formatting \ - 3_changingmodel \ - 4_headers \ - 5_edit \ - 6_treeview \ - 7_selections - |