Practical Model View Programming
-
Upload
marius-bugge-monsen -
Category
Documents
-
view
4.961 -
download
0
description
Transcript of Practical Model View Programming
![Page 1: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/1.jpg)
PRA CT ICA L M OD EL V IEW PROGRA M M IN GCOD E LESS. V IEW M ORE.
![Page 2: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/2.jpg)
Marius Bugge Monsen,MSc (NTNU),Software Developer
![Page 3: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/3.jpg)
Contents
● An Overview of the Q t Model View Architecture● An Introduction to The Model Interface● Advanced Model View Techniques (Q t 4.1+)
![Page 4: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/4.jpg)
A n Overview of the Q t M odel View A rchitecture
![Page 5: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/5.jpg)
![Page 6: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/6.jpg)
M odel
View
Change N otifi cations
Item Selections
U ser Input
Item D elegate
D ata Changes
U ser Input
Item Selection State
![Page 7: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/7.jpg)
Tree W idgetTable W idget List W idget
Item B ased Views (Qt 3 and 4)
![Page 8: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/8.jpg)
Item Selections
M odel
View View
![Page 9: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/9.jpg)
int main(int argc, char *argv[]){
QApplication app(argc, argv);QDirModel model;QTableView view1;QTreeView view2;
view1.setModel(&model);view2.setModel(&model);
view1.setRootIndex(model.index(0, 0));view1.show();view2.show();app.exec();
}
![Page 10: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/10.jpg)
![Page 11: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/11.jpg)
Effi ciency
W hat do you get ?
Flexibility
Maintainability
![Page 12: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/12.jpg)
A n Introduction To T he M odel Interface
![Page 13: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/13.jpg)
class ListModel : public QAbstractListModel{
Q_OBJECTpublic:
ListModel(QObject *parent = 0);~ListModel();
int rowCount(const QModelIndex &parent) const;QVariant data(const QModelIndex &index, int role) const;
};
![Page 14: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/14.jpg)
ListModel::ListModel(QObject *parent): QAbstractListModel(parent) {}
ListModel::~ListModel() {}
int ListModel::rowCount(const QModelIndex &) const{
return 10000;}
QVariant ListModel::data(const QModelIndex &index, int role) const
{if (role == Qt::DisplayRole)
return index.row();return QVariant();
}
![Page 15: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/15.jpg)
![Page 16: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/16.jpg)
0
0
1
2
0
1
2
0 1 2
![Page 17: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/17.jpg)
![Page 18: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/18.jpg)
int main(int, char *[]){
ListModel model;
QModelIndex index = model.index(2);QVariant data = model.data(index, Qt::DisplayRole);
qDebug() << data;}
![Page 19: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/19.jpg)
D ecoration Role
D isplay Role
M y W orld
Type: Image File Size: 1.2 Mb
ToolT ip Role
![Page 20: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/20.jpg)
QVariant ListModel::data(const QModelIndex &index, int role) const
{if (role == Qt::DisplayRole)
return index.row();
if (role == Qt::DecorationRole)return QColor(Qt::green);
return QVariant();}
![Page 21: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/21.jpg)
![Page 22: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/22.jpg)
class ColorDelegate : public QItemDelegate{ Q_OBJECTpublic: ColorDelegate(QObject *parent = 0);protected: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;};
![Page 23: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/23.jpg)
void ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ const QAbstractItemModel *model = index.model();
QVariant decoration = model->data(index, Qt::DecorationRole); if (decoration.type() == QVariant::Color) { QLinearGradient gradient(option.rect.left(), 0, option.rect.width(), 0); QColor left = option.palette.color(QPalette::Background); gradient.setColorAt(0, left); QColor right = qvariant_cast<QColor>(decoration); gradient.setColorAt(1, right); painter->fillRect(option.rect, gradient); } QItemDelegate::paint(painter, option, index);}
![Page 24: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/24.jpg)
![Page 25: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/25.jpg)
One 100 -300 500Two 599 300 233Three 33 -34 -55Four 200 502 200
![Page 26: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/26.jpg)
class TableModel : public QAbstractTableModel{
Q_OBJECTpublic:
TableModel(QObject *parent = 0);~TableModel();int rowCount(const QModelIndex &parent) const;int columnCount(const QModelIndex &parent) const;QVariant data(const QModelIndex &index, int role) const;bool setData(const QModelIndex &index,
const QVariant &value, int role);Qt::ItemFlags flags(const QModelIndex &index) const;// not part of the model interfacebool load(const QString &fileName);bool save(const QString &fileName) const;
private:int rows, columns;QVector<QVariant> table;
};
![Page 27: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/27.jpg)
QVariant TableModel::data(const QModelIndex &index, int role) const
{int i = index.row() * columns + index.column();QVariant value = table.at(i);
if (role == Qt::EditRole || role == Qt::DisplayRole)return value;
if (role == Qt::TextColorRole&& value.type() == QVariant::Int) {
if (value.toInt() < 0)return QColor(Qt::red);return QColor(Qt::blue);
}return QVariant();
}
![Page 28: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/28.jpg)
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{int i = index.row() * columns + index.column();if (role == Qt::EditRole) {
table[i] = value;QModelIndex topLeft = index;QModelIndex bottomRight = index;emit dataChanged(topLeft, bottomRight);// <<< important!return true;
}return false;
}Qt::ItemFlags TableModel::flags(const QModelIndex &index) const{
return QAbstractTableModel::flags(index)|Qt::ItemIsEditable;}
![Page 29: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/29.jpg)
bool TableModel::load(const QString &fileName){
QFile file(fileName);if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
return false;rows = columns = 0;table.clear();QTextStream in(&file);bool result = parse(in);
reset();// <<< important!
return result;}
![Page 30: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/30.jpg)
![Page 31: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/31.jpg)
A dvanced M odel V iew Techniques (Qt 4.1+)
![Page 32: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/32.jpg)
M odel Proxy View
![Page 33: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/33.jpg)
M odel Sorting View
![Page 34: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/34.jpg)
int main(int argc, char *argv[]){
QApplication app(argc, argv);TableModel model;model.load("table.data");
QSortingProxyModel sorter;sorter.setSourceModel(&model);QTreeView treeview;treeview.setModel(&sorter);
treeview.header()->setClickable(true);treeview.header()->setSortIndicatorShown(true);treeview.show();return app.exec();
}
![Page 35: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/35.jpg)
![Page 36: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/36.jpg)
M odel Filtering View
![Page 37: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/37.jpg)
int main(int argc, char *argv[]){
QApplication app(argc, argv);QWidget widget;QBoxLayout *layout = new QBoxLayout(QBoxLayout::TopToBottom,
&widget);QLineEdit *lineedit = new QLineEdit;layout->addWidget(lineedit);TableModel model;model.load("table.data");QStringFilterModel filter;filter.setSourceModel(&model);QObject::connect(lineedit,SIGNAL(textChanged(const QString&)),
&filter,SLOT(setPattern(const QString&)));QTableView *tableview = new QTableView;tableview->setModel(&filter);layout->addWidget(tableview);widget.show();return app.exec();
}
![Page 38: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/38.jpg)
![Page 39: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/39.jpg)
A ggregating View
M odel 1
M odel 2
![Page 40: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/40.jpg)
int main(int argc, char *argv[]){ QApplication app(argc, argv); QTreeView treeview; QStringListModel model_1(QStringList() << "ALPHA" << "BRAVO" << "CHARLIE" << "DELTA"); QDirModel model_2; QAggregatingProxyModel aggregator; aggregator.appendSourceModel(&model_1); aggregator.appendSourceModel(&model_2); treeview.setModel(&aggregator); treeview.show(); return app.exec();}
![Page 41: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/41.jpg)
![Page 42: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/42.jpg)
● Qt Model View Architecture O verview
● The Model Interface Introduction● Advanced Techniques Using the Model Interface
W hat we have covered
![Page 43: Practical Model View Programming](https://reader035.fdocuments.net/reader035/viewer/2022081802/5407482c8d7f72d8088b4a54/html5/thumbnails/43.jpg)
http://doc.trolltech.com/4.0/model-view-programming.html
http://doc.trolltech.com/4.0/model-view.html
M ore Information