##// END OF EJS Templates
Model data example updated
Marek Rosa -
r546:3faf122e2566
parent child
Show More
@@ -1,144 +1,146
1 #include "customtablemodel.h"
1 #include "customtablemodel.h"
2
2
3 CustomTableModel::CustomTableModel(QObject *parent) :
3 CustomTableModel::CustomTableModel(QObject *parent) :
4 QAbstractTableModel(parent)
4 QAbstractTableModel(parent)
5 {
5 {
6 m_points.append(QPointF(10, 50));
6 m_points.append(QPointF(10, 50));
7 m_labels.append("Apples");
7 m_labels.append("Apples");
8 m_points.append(QPointF(60, 70));
8 m_points.append(QPointF(60, 70));
9 m_labels.append("Oranges");
9 m_labels.append("Oranges");
10 m_points.append(QPointF(110, 50));
10 m_points.append(QPointF(110, 50));
11 m_labels.append("Bananas");
11 m_labels.append("Bananas");
12 m_points.append(QPointF(140, 40));
12 m_points.append(QPointF(140, 40));
13 m_labels.append("Lemons");
13 m_labels.append("Lemons");
14 m_points.append(QPointF(200, 150));
14 m_points.append(QPointF(200, 150));
15 m_labels.append("Plums");
15 m_labels.append("Plums");
16 m_points.append(QPointF(225, 75));
16 m_points.append(QPointF(225, 75));
17 m_labels.append("Pearls");
17 m_labels.append("Pearls");
18 }
18 }
19
19
20 int CustomTableModel::rowCount(const QModelIndex & parent) const
20 int CustomTableModel::rowCount(const QModelIndex & parent) const
21 {
21 {
22 return m_points.count();
22 return m_points.count();
23 }
23 }
24
24
25 int CustomTableModel::columnCount(const QModelIndex & parent) const
25 int CustomTableModel::columnCount(const QModelIndex & parent) const
26 {
26 {
27 return 3;
27 return 3;
28 }
28 }
29
29
30 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
30 QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, int role ) const
31 {
31 {
32 if (role != Qt::DisplayRole)
32 if (role != Qt::DisplayRole)
33 return QVariant();
33 return QVariant();
34
34
35 if (orientation == Qt::Horizontal)
35 if (orientation == Qt::Horizontal)
36 {
36 {
37 switch(section)
37 switch(section)
38 {
38 {
39 case 0:
39 case 0:
40 return "x";
40 return "x";
41 case 1:
41 case 1:
42 return "y";
42 return "y";
43 case 2:
43 case 2:
44 return "Fruit";
44 return "Fruit";
45 default:
45 default:
46 return "What?";
46 return "What?";
47 }
47 }
48 }
48 }
49 else
49 else
50 return QString("%1").arg(section + 1);
50 return QString("%1").arg(section + 1);
51 }
51 }
52
52
53 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
53 QVariant CustomTableModel::data(const QModelIndex & index, int role) const
54 {
54 {
55 if (role == Qt::DisplayRole)
55 if (role == Qt::DisplayRole)
56 {
56 {
57 switch(index.column())
57 switch(index.column())
58 {
58 {
59 case 0:
59 case 0:
60 return m_points[index.row()].x();
60 return m_points[index.row()].x();
61 case 1:
61 case 1:
62 return m_points[index.row()].y();
62 return m_points[index.row()].y();
63 case 2:
63 case 2:
64 return m_labels[index.row()];
64 return m_labels[index.row()];
65 default:
65 default:
66 return QVariant();
66 return QVariant();
67 }
67 }
68 }
68 }
69 else if (role == Qt::EditRole)
69 else if (role == Qt::EditRole)
70 {
70 {
71 switch(index.column())
71 switch(index.column())
72 {
72 {
73 case 0:
73 case 0:
74 return m_points[index.row()].x();
74 return m_points[index.row()].x();
75 case 1:
75 case 1:
76 return m_points[index.row()].y();
76 return m_points[index.row()].y();
77 case 2:
77 case 2:
78 return m_labels[index.row()];
78 return m_labels[index.row()];
79 default:
79 default:
80 return QVariant();
80 return QVariant();
81 }
81 }
82 }
82 }
83 }
83 }
84
84
85 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
85 bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & value, int role)
86 {
86 {
87 if (index.isValid() && role == Qt::EditRole)
87 if (index.isValid() && role == Qt::EditRole)
88 {
88 {
89 switch(index.column())
89 switch(index.column())
90 {
90 {
91 case 0:
91 case 0:
92 m_points[index.row()].setX(value.toDouble());
92 m_points[index.row()].setX(value.toDouble());
93 break;
93 break;
94 case 1:
94 case 1:
95 m_points[index.row()].setY(value.toDouble());
95 m_points[index.row()].setY(value.toDouble());
96 break;
96 break;
97 case 2:
97 case 2:
98 m_labels.replace(index.row(), value.toString());
98 m_labels.replace(index.row(), value.toString());
99 break;
99 break;
100 default:
100 default:
101 return false;
101 return false;
102 }
102 }
103 emit dataChanged(index, index);
103 emit dataChanged(index, index);
104 return true;
104 return true;
105 }
105 }
106 return false;
106 return false;
107 }
107 }
108
108
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
109 Qt::ItemFlags CustomTableModel::flags ( const QModelIndex & index ) const
110 {
110 {
111 // if (!index.isValid())
111 // if (!index.isValid())
112 // return Qt::ItemIsEnabled;
112 // return Qt::ItemIsEnabled;
113 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
113 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
114 }
114 }
115
115
116 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
116 bool CustomTableModel::insertRows ( int row, int count, const QModelIndex & parent)
117 {
117 {
118 if (row < 0)
119 row = 0;
118 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
120 beginInsertRows(QModelIndex(), row /*dataTable.count()*/, row + count - 1);
119 for (int i = row; i < row + count; i++)
121 for (int i = row; i < row + count; i++)
120 {
122 {
121 m_points.insert(row, QPointF());
123 m_points.insert(row, QPointF());
122 m_labels.insert(row,(""));
124 m_labels.insert(row,(""));
123 }
125 }
124 endInsertRows();
126 endInsertRows();
125 return true;
127 return true;
126 }
128 }
127
129
128 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
130 bool CustomTableModel::removeRows ( int row, int count, const QModelIndex & parent)
129 {
131 {
130 if (row > this->rowCount() - 1)
132 if (row > this->rowCount() - 1)
131 return false;
133 return false;
132 if (row < 0)
134 if (row < 0)
133 row = 0;
135 row = 0;
134 if (row + count > rowCount())
136 if (row + count > rowCount())
135 return false;
137 return false;
136 beginRemoveRows(parent, row, row + count - 1);
138 beginRemoveRows(parent, row, row + count - 1);
137 for (int i = row; i < row + count; i++)
139 for (int i = row; i < row + count; i++)
138 {
140 {
139 m_points.removeAt(row);
141 m_points.removeAt(row);
140 m_labels.removeAt(row);
142 m_labels.removeAt(row);
141 }
143 }
142 endRemoveRows();
144 endRemoveRows();
143 return true;
145 return true;
144 }
146 }
@@ -1,109 +1,121
1 #include "tablewidget.h"
1 #include "tablewidget.h"
2 #include <QGridLayout>
2 #include <QGridLayout>
3 #include <QTableView>
3 #include <QTableView>
4 #include <QStyledItemDelegate>
4 #include <QStyledItemDelegate>
5 #include "qlineseries.h"
5 #include "qlineseries.h"
6 #include "qsplineseries.h"
6 #include "qsplineseries.h"
7 #include "qscatterseries.h"
7 #include "qscatterseries.h"
8 #include "customtablemodel.h"
8 #include "customtablemodel.h"
9 #include "qpieseries.h"
9 #include "qpieseries.h"
10 #include <QPushButton>
10 #include <QPushButton>
11 #include <QRadioButton>
11 #include <QRadioButton>
12
12
13 TableWidget::TableWidget(QWidget *parent)
13 TableWidget::TableWidget(QWidget *parent)
14 : QWidget(parent)
14 : QWidget(parent)
15 {
15 {
16 setGeometry(100, 100, 1000, 600);
16 // create simple model for storing data
17 // create simple model for storing data
17 // user's table data model
18 // user's table data model
18 m_model = new CustomTableModel;
19 m_model = new CustomTableModel;
19 tableView = new QTableView;
20 tableView = new QTableView;
20 tableView->setModel(m_model);
21 tableView->setModel(m_model);
21 tableView->setMinimumSize(340, 480);
22 tableView->setMinimumSize(340, 480);
22 // tableView->setItemDelegate(new QStyledItemDelegate);
23 // tableView->setItemDelegate(new QStyledItemDelegate);
23 chartView = new QChartView;
24 chartView = new QChartView(this);
24 chartView->setMinimumSize(640, 480);
25 chartView->setMinimumSize(640, 480);
25
26
26 // create
27 // create
27 // QLineSeries* series = new QLineSeries;
28 // QLineSeries* series = new QLineSeries;
28 // QSplineSeries* series = new QSplineSeries;
29 // QSplineSeries* series = new QSplineSeries;
29 // QScatterSeries* series = new QScatterSeries;
30 // QScatterSeries* series = new QScatterSeries;
30 // series->setModel(m_model);
31 // series->setModel(m_model);
31 // series->setModelMapping(0,1, Qt::Vertical);
32 // series->setModelMapping(0,1, Qt::Vertical);
32
33
33 // QPieSeries* pieSeries = new QPieSeries;
34 // QPieSeries* pieSeries = new QPieSeries;
34 // pieSeries->setModel(model);
35 // pieSeries->setModel(model);
35 // pieSeries
36 // pieSeries
36
37
37 // chartView->addSeries(series);
38 // chartView->addSeries(series);
38
39
39 // add, remove data buttons
40 // add, remove data buttons
40 QPushButton* addRowButton = new QPushButton("Add row");
41 QPushButton* addRowAboveButton = new QPushButton("Add row above");
41 connect(addRowButton, SIGNAL(clicked()), this, SLOT(addRow()));
42 connect(addRowAboveButton, SIGNAL(clicked()), this, SLOT(addRowAbove()));
43
44 QPushButton* addRowBelowButton = new QPushButton("Add row below");
45 connect(addRowBelowButton, SIGNAL(clicked()), this, SLOT(addRowBelow()));
42
46
43 QPushButton* removeRowButton = new QPushButton("Remove row");
47 QPushButton* removeRowButton = new QPushButton("Remove row");
44 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
48 connect(removeRowButton, SIGNAL(clicked()), this, SLOT(removeRow()));
45
49
46 // buttons layout
50 // buttons layout
47 QVBoxLayout* buttonsLayout = new QVBoxLayout;
51 QVBoxLayout* buttonsLayout = new QVBoxLayout;
48 buttonsLayout->addWidget(addRowButton);
52 buttonsLayout->addWidget(addRowAboveButton);
53 buttonsLayout->addWidget(addRowBelowButton);
49 buttonsLayout->addWidget(removeRowButton);
54 buttonsLayout->addWidget(removeRowButton);
50 buttonsLayout->addStretch();
55 buttonsLayout->addStretch();
51
56
52 // chart type radio buttons
57 // chart type radio buttons
53 lineRadioButton = new QRadioButton("Line");
58 lineRadioButton = new QRadioButton("Line");
54 splineRadioButton = new QRadioButton("Spline");
59 splineRadioButton = new QRadioButton("Spline");
55 scatterRadioButton = new QRadioButton("Scatter");
60 scatterRadioButton = new QRadioButton("Scatter");
56
61
57 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
62 connect(lineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
58 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
63 connect(splineRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
59 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
64 connect(scatterRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType()));
60 lineRadioButton->setChecked(true);
65 lineRadioButton->setChecked(true);
61
66
62 // radio buttons layout
67 // radio buttons layout
63 QHBoxLayout* radioLayout = new QHBoxLayout;
68 QHBoxLayout* radioLayout = new QHBoxLayout;
64 radioLayout->addWidget(lineRadioButton);
69 radioLayout->addWidget(lineRadioButton);
65 radioLayout->addWidget(splineRadioButton);
70 radioLayout->addWidget(splineRadioButton);
66 radioLayout->addWidget(scatterRadioButton);
71 radioLayout->addWidget(scatterRadioButton);
67
72
68 // create main layout
73 // create main layout
69 QGridLayout* mainLayout = new QGridLayout;
74 QGridLayout* mainLayout = new QGridLayout;
70 mainLayout->addLayout(buttonsLayout, 0, 1);
75 mainLayout->addLayout(buttonsLayout, 0, 1);
71 mainLayout->addLayout(radioLayout, 0, 2);
76 mainLayout->addLayout(radioLayout, 0, 2);
72 mainLayout->addWidget(tableView, 1, 1);
77 mainLayout->addWidget(tableView, 1, 1);
73 mainLayout->addWidget(chartView, 1, 2);
78 mainLayout->addWidget(chartView, 1, 2);
74 setLayout(mainLayout);
79 setLayout(mainLayout);
75 }
80 }
76
81
77 void TableWidget::addRow()
82 void TableWidget::addRowAbove()
83 {
84 // m_model->insertRow(m_model->rowCount());
85 m_model->insertRow(tableView->currentIndex().row());
86
87 }
88
89 void TableWidget::addRowBelow()
78 {
90 {
79 // m_model->insertRow(m_model->rowCount());
91 // m_model->insertRow(m_model->rowCount());
80 m_model->insertRow(tableView->currentIndex().row() + 1);
92 m_model->insertRow(tableView->currentIndex().row() + 1);
81
93
82 }
94 }
83
95
84 void TableWidget::removeRow()
96 void TableWidget::removeRow()
85 {
97 {
86 // m_model->removeRow(m_model->rowCount() - 1);
98 // m_model->removeRow(m_model->rowCount() - 1);
87 m_model->removeRow(tableView->currentIndex().row());
99 m_model->removeRow(tableView->currentIndex().row());
88 }
100 }
89
101
90 void TableWidget::updateChartType()
102 void TableWidget::updateChartType()
91 {
103 {
92 chartView->removeAllSeries();
104 chartView->removeAllSeries();
93
105
94 if (lineRadioButton->isChecked())
106 if (lineRadioButton->isChecked())
95 series = new QLineSeries;
107 series = new QLineSeries;
96 else if (splineRadioButton->isChecked())
108 else if (splineRadioButton->isChecked())
97 series = new QSplineSeries;
109 series = new QSplineSeries;
98 else
110 else
99 series = new QScatterSeries;
111 series = new QScatterSeries;
100
112
101 series->setModel(m_model);
113 series->setModel(m_model);
102 series->setModelMapping(0,1, Qt::Vertical);
114 series->setModelMapping(0,1, Qt::Vertical);
103 chartView->addSeries(series);
115 chartView->addSeries(series);
104 }
116 }
105
117
106 TableWidget::~TableWidget()
118 TableWidget::~TableWidget()
107 {
119 {
108
120
109 }
121 }
@@ -1,39 +1,40
1 #ifndef TABLEWIDGET_H
1 #ifndef TABLEWIDGET_H
2 #define TABLEWIDGET_H
2 #define TABLEWIDGET_H
3
3
4 #include <QtGui/QWidget>
4 #include <QtGui/QWidget>
5 #include "qchartview.h"
5 #include "qchartview.h"
6 #include "qxyseries.h"
6 #include "qxyseries.h"
7
7
8 QTCOMMERCIALCHART_USE_NAMESPACE
8 QTCOMMERCIALCHART_USE_NAMESPACE
9
9
10 class CustomTableModel;
10 class CustomTableModel;
11 class QTableView;
11 class QTableView;
12 class QRadioButton;
12 class QRadioButton;
13 //class QSeries;
13 //class QSeries;
14
14
15 class TableWidget : public QWidget
15 class TableWidget : public QWidget
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18
18
19 public:
19 public:
20 TableWidget(QWidget *parent = 0);
20 TableWidget(QWidget *parent = 0);
21 ~TableWidget();
21 ~TableWidget();
22
22
23
23
24 public slots:
24 public slots:
25 void addRow();
25 void addRowAbove();
26 void addRowBelow();
26 void removeRow();
27 void removeRow();
27 void updateChartType();
28 void updateChartType();
28
29
29 private:
30 private:
30 QChartView* chartView;
31 QChartView* chartView;
31 QXYSeries* series;
32 QXYSeries* series;
32 CustomTableModel* m_model;
33 CustomTableModel* m_model;
33 QTableView* tableView;
34 QTableView* tableView;
34 QRadioButton* lineRadioButton;
35 QRadioButton* lineRadioButton;
35 QRadioButton* splineRadioButton;
36 QRadioButton* splineRadioButton;
36 QRadioButton* scatterRadioButton;
37 QRadioButton* scatterRadioButton;
37 };
38 };
38
39
39 #endif // TABLEWIDGET_H
40 #endif // TABLEWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now