summaryrefslogtreecommitdiff
path: root/examples/tutorials/modelview/5_edit
diff options
context:
space:
mode:
authorMichael D Scull <ext-michael.scull@nokia.com>2010-05-27 15:12:58 +0200
committerDavid Boddie <dboddie@trolltech.com>2010-07-07 17:07:53 +0200
commitf574700f920ae5d5bda25ba217d6face9f3d3902 (patch)
treea4db1390147f7b6041ca763be6715078b1ddd9ab /examples/tutorials/modelview/5_edit
parent4df3fb13821e13ecf581d87f2d224b2f1b964609 (diff)
downloadqt4-tools-f574700f920ae5d5bda25ba217d6face9f3d3902.tar.gz
Rolands ModelView Source
Diffstat (limited to 'examples/tutorials/modelview/5_edit')
-rwxr-xr-xexamples/tutorials/modelview/5_edit/5_edit.pro10
-rwxr-xr-xexamples/tutorials/modelview/5_edit/main.cpp10
-rwxr-xr-xexamples/tutorials/modelview/5_edit/modelview.cpp20
-rwxr-xr-xexamples/tutorials/modelview/5_edit/modelview.h19
-rwxr-xr-xexamples/tutorials/modelview/5_edit/mymodel.cpp58
-rwxr-xr-xexamples/tutorials/modelview/5_edit/mymodel.h24
6 files changed, 141 insertions, 0 deletions
diff --git a/examples/tutorials/modelview/5_edit/5_edit.pro b/examples/tutorials/modelview/5_edit/5_edit.pro
new file mode 100755
index 0000000000..e18c59610e
--- /dev/null
+++ b/examples/tutorials/modelview/5_edit/5_edit.pro
@@ -0,0 +1,10 @@
+TARGET = mv_edit
+
+TEMPLATE = app
+
+SOURCES += main.cpp \
+ modelview.cpp \
+ mymodel.cpp
+
+HEADERS += modelview.h \
+ mymodel.h
diff --git a/examples/tutorials/modelview/5_edit/main.cpp b/examples/tutorials/modelview/5_edit/main.cpp
new file mode 100755
index 0000000000..998503c2e8
--- /dev/null
+++ b/examples/tutorials/modelview/5_edit/main.cpp
@@ -0,0 +1,10 @@
+#include <QtGui/QApplication>
+#include "modelview.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ ModelView w;
+ w.show();
+ return a.exec();
+}
diff --git a/examples/tutorials/modelview/5_edit/modelview.cpp b/examples/tutorials/modelview/5_edit/modelview.cpp
new file mode 100755
index 0000000000..b6e8e34b1a
--- /dev/null
+++ b/examples/tutorials/modelview/5_edit/modelview.cpp
@@ -0,0 +1,20 @@
+#include <QTableView>
+#include "modelview.h"
+#include "mymodel.h"
+
+ModelView::ModelView(QWidget *parent)
+ : QMainWindow(parent)
+{
+ tableView = new QTableView(this);
+ setCentralWidget(tableView);
+ QAbstractTableModel *myModel = new MyModel(this);
+ tableView->setModel( myModel );
+
+ //transfer changes to the model to the window title
+ connect(myModel, SIGNAL(editCompleted(const QString &) ), this, SLOT(setWindowTitle(const QString &)));
+}
+
+void ModelView::showWindowTitle(const QString & title)
+{
+setWindowTitle( title );
+}
diff --git a/examples/tutorials/modelview/5_edit/modelview.h b/examples/tutorials/modelview/5_edit/modelview.h
new file mode 100755
index 0000000000..e1591f96f4
--- /dev/null
+++ b/examples/tutorials/modelview/5_edit/modelview.h
@@ -0,0 +1,19 @@
+#ifndef MODELVIEW_H
+#define MODELVIEW_H
+
+#include <QtGui/QMainWindow>
+
+class QTableView; //forward declaration
+
+class ModelView : public QMainWindow
+{
+ Q_OBJECT
+private:
+ QTableView *tableView;
+public:
+ ModelView(QWidget *parent = 0);
+public slots:
+ void showWindowTitle(const QString & title);
+};
+
+#endif // MODELVIEW_H
diff --git a/examples/tutorials/modelview/5_edit/mymodel.cpp b/examples/tutorials/modelview/5_edit/mymodel.cpp
new file mode 100755
index 0000000000..c64a6b7b56
--- /dev/null
+++ b/examples/tutorials/modelview/5_edit/mymodel.cpp
@@ -0,0 +1,58 @@
+#include "mymodel.h"
+
+const int COLS= 3;
+const int ROWS= 2;
+
+MyModel::MyModel(QObject *parent)
+ :QAbstractTableModel(parent)
+{
+ //gridData needs to have 6 element, one for each table cell
+ m_gridData << "1/1" << "1/2" << "1/3" << "2/1" << "2/2" << "2/3" ;
+}
+
+//-------------------------------------------------------
+int MyModel::rowCount(const QModelIndex & /*parent*/ ) const
+{
+ return ROWS;
+}
+
+//-------------------------------------------------------
+int MyModel::columnCount(const QModelIndex & /*parent*/ ) const
+{
+ return COLS;
+}
+
+//-------------------------------------------------------
+QVariant MyModel::data(const QModelIndex &index, int role ) const
+{
+ if(role == Qt::DisplayRole)
+ {
+ return m_gridData[modelIndexToOffset(index)];
+ }
+ return QVariant();
+}
+
+//-----------------------------------------------------------------
+bool MyModel::setData ( const QModelIndex & index, const QVariant & value, int role )
+{
+ if(role == Qt::EditRole)
+ {
+ m_gridData[modelIndexToOffset(index)] = value.toString();
+ emit editCompleted(m_gridData.join(" | ") );
+ }
+ return true;
+}
+
+//-----------------------------------------------------------------
+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
+{
+ return index.row()*COLS + index.column();
+}
+
diff --git a/examples/tutorials/modelview/5_edit/mymodel.h b/examples/tutorials/modelview/5_edit/mymodel.h
new file mode 100755
index 0000000000..f8fac77909
--- /dev/null
+++ b/examples/tutorials/modelview/5_edit/mymodel.h
@@ -0,0 +1,24 @@
+#ifndef MYMODEL_H
+#define MYMODEL_H
+
+#include <QAbstractTableModel>
+#include <QStringList>
+
+class MyModel : public QAbstractTableModel
+{
+ Q_OBJECT
+public:
+ MyModel(QObject *parent);
+ 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 ;
+private:
+ QStringList m_gridData; //holds text entered into QTableView
+ int modelIndexToOffset(const QModelIndex & index) const;
+signals:
+ void editCompleted(const QString &);
+};
+
+#endif // MYMODEL_H