diff options
author | Michael D Scull <ext-michael.scull@nokia.com> | 2010-05-27 13:12:58 (GMT) |
---|---|---|
committer | David Boddie <dboddie@trolltech.com> | 2010-07-07 15:07:53 (GMT) |
commit | f574700f920ae5d5bda25ba217d6face9f3d3902 (patch) | |
tree | a4db1390147f7b6041ca763be6715078b1ddd9ab /examples/tutorials/modelview/2_formatting | |
parent | 4df3fb13821e13ecf581d87f2d224b2f1b964609 (diff) | |
download | Qt-f574700f920ae5d5bda25ba217d6face9f3d3902.zip Qt-f574700f920ae5d5bda25ba217d6face9f3d3902.tar.gz Qt-f574700f920ae5d5bda25ba217d6face9f3d3902.tar.bz2 |
Rolands ModelView Source
Diffstat (limited to 'examples/tutorials/modelview/2_formatting')
6 files changed, 138 insertions, 0 deletions
diff --git a/examples/tutorials/modelview/2_formatting/2_formatting.pro b/examples/tutorials/modelview/2_formatting/2_formatting.pro new file mode 100755 index 0000000..7e70d81 --- /dev/null +++ b/examples/tutorials/modelview/2_formatting/2_formatting.pro @@ -0,0 +1,10 @@ +TARGET = mv_formatting + +TEMPLATE = app + +SOURCES += main.cpp \ + modelview.cpp \ + mymodel.cpp + +HEADERS += modelview.h \ + mymodel.h diff --git a/examples/tutorials/modelview/2_formatting/main.cpp b/examples/tutorials/modelview/2_formatting/main.cpp new file mode 100755 index 0000000..998503c --- /dev/null +++ b/examples/tutorials/modelview/2_formatting/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/2_formatting/modelview.cpp b/examples/tutorials/modelview/2_formatting/modelview.cpp new file mode 100755 index 0000000..becd61d --- /dev/null +++ b/examples/tutorials/modelview/2_formatting/modelview.cpp @@ -0,0 +1,12 @@ +#include <QTableView> +#include "modelview.h" +#include "mymodel.h" + +ModelView::ModelView(QWidget *parent) + : QMainWindow(parent) +{ + tableView = new QTableView(this); + setCentralWidget(tableView); + tableView->setModel(new MyModel(this) ); +} + diff --git a/examples/tutorials/modelview/2_formatting/modelview.h b/examples/tutorials/modelview/2_formatting/modelview.h new file mode 100755 index 0000000..98bee38 --- /dev/null +++ b/examples/tutorials/modelview/2_formatting/modelview.h @@ -0,0 +1,17 @@ +#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); +}; + +#endif // MODELVIEW_H diff --git a/examples/tutorials/modelview/2_formatting/mymodel.cpp b/examples/tutorials/modelview/2_formatting/mymodel.cpp new file mode 100755 index 0000000..48b1134 --- /dev/null +++ b/examples/tutorials/modelview/2_formatting/mymodel.cpp @@ -0,0 +1,73 @@ +#include <QFont> +#include <QBrush> +#include "mymodel.h" +#include <QDebug> + +MyModel::MyModel(QObject *parent) + :QAbstractTableModel(parent) +{ +} + +//------------------------------------------------------- +int MyModel::rowCount(const QModelIndex & /*parent */ ) const +{ + return 2; +} + +//------------------------------------------------------- +int MyModel::columnCount(const QModelIndex & /*parent */ ) const +{ + return 3; +} + +//------------------------------------------------------- +QVariant MyModel::data(const QModelIndex &index, int role ) const +{ + int row = index.row(); + int col = index.column(); + // generate a log message when this method gets called + qDebug() << QString("row %1, col%2, role %3") + .arg(row).arg(col).arg(role); + + switch(role){ + case Qt::DisplayRole: + 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) + { + QFont boldFont; + boldFont.setBold(true); + return boldFont; + } + break; + case Qt::BackgroundRole: + + if(row == 1 && col ==2 ) //change background only for cell(1,2) + { + QBrush redBackground(QColor(Qt::red)); + return redBackground; + } + break; + case Qt::TextAlignmentRole: + + 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) + { + return Qt::Checked; + } + } + return QVariant(); +} + diff --git a/examples/tutorials/modelview/2_formatting/mymodel.h b/examples/tutorials/modelview/2_formatting/mymodel.h new file mode 100755 index 0000000..01ae6cb --- /dev/null +++ b/examples/tutorials/modelview/2_formatting/mymodel.h @@ -0,0 +1,16 @@ +#ifndef MYMODEL_H +#define MYMODEL_H + +#include <QAbstractTableModel> + +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; +}; + +#endif // MYMODEL_H |