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