@@ -1,57 +1,57 | |||
|
1 | 1 | /*! |
|
2 | 2 | \example examples/modeldata |
|
3 | 3 | \title Model data example |
|
4 | 4 | \subtitle |
|
5 | 5 | |
|
6 | 6 | This example shows how to use QAbstractItemModel derived model as the data for the series. |
|
7 | 7 | |
|
8 | 8 | \image examples_modeldata.png |
|
9 | 9 | |
|
10 | 10 | Let's start by creating an instance of CustomTableModel class. |
|
11 | 11 | CustomTableModel class is derived from QAbstractTableModel and it was created for the purpose of this example. |
|
12 | 12 | The constructor of this class populates the internal data store of the model with the data that is good for our chart example. |
|
13 | 13 | |
|
14 | 14 | \snippet ../examples/modeldata/tablewidget.cpp 1 |
|
15 | 15 | |
|
16 | 16 | We now have a model with data that we would like to display both on the chart and in a QTableView. |
|
17 | 17 | First, we create QTableView and tell it use the model as a data source. To make the data cells fill the table view we also change headers resize mode. |
|
18 | 18 | |
|
19 | 19 | \snippet ../examples/modeldata/tablewidget.cpp 2 |
|
20 | 20 | |
|
21 | 21 | Now we need QChart instance to display the same data on the chart. |
|
22 | 22 | We also enable animations. It makes it easier to see how modifying the model's data affect the chart. |
|
23 | 23 | |
|
24 | 24 | \snippet ../examples/modeldata/tablewidget.cpp 3 |
|
25 | 25 | |
|
26 |
|
|
|
26 | The code below creates new line series and gives it a name. Following line creates an instance of QVXYModelMapper class. | |
|
27 | 27 | Next two lines specifie that x coordinates are taken from the model's column(Qt::Vertical) with index 0 and the y coordinates are taken from the model's column with index 1. |
|
28 | 28 | To create a connection between the series and the model we set both of those objects to QVXYModelMapper. |
|
29 | 29 | |
|
30 | 30 | Finally the series is added to the chart. |
|
31 | 31 | |
|
32 | 32 | \snippet ../examples/modeldata/tablewidget.cpp 4 |
|
33 | 33 | |
|
34 | 34 | To show in QTableView which data coresponds with which series this example uses table coloring. |
|
35 |
When series is added to the chart it is assigned a color b |
|
|
35 | When series is added to the chart it is assigned a color based on the currently selected theme. | |
|
36 | 36 | Code below extracts that color from the series and uses it to create colored QTableView. |
|
37 | 37 | Coloring of the view is not a part of the QChart functionality. |
|
38 | 38 | |
|
39 | 39 | \snippet ../examples/modeldata/tablewidget.cpp 5 |
|
40 | 40 | |
|
41 | 41 | The same operations are done with second series. Notice that for this series different columns of the same model are mapped. |
|
42 | 42 | |
|
43 | 43 | \snippet ../examples/modeldata/tablewidget.cpp 6 |
|
44 | 44 | |
|
45 | 45 | \snippet ../examples/modeldata/tablewidget.cpp 7 |
|
46 | 46 | |
|
47 | 47 | To avoid setting up the QGraphicsScene we use QChartView class that does it for us. QChart object pointer is used as a parameter of the QChartView constructor. |
|
48 | 48 | To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set. |
|
49 | 49 | |
|
50 | 50 | \snippet ../examples/modeldata/tablewidget.cpp 8 |
|
51 | 51 | |
|
52 | 52 | Finally we place both widgets in a layout and use the layout as the application layout. |
|
53 | 53 | |
|
54 | 54 | \snippet ../examples/modeldata/tablewidget.cpp 9 |
|
55 | 55 | |
|
56 | 56 | Application is ready. Try modifying the data in the table view and see how it affects the chart. |
|
57 | 57 | */ |
General Comments 0
You need to be logged in to leave comments.
Login now