/usr/share/qt5/doc/qtcharts/qtcharts-barmodelmapper-example.html is in qtcharts5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- examples-barmodelmapper.qdoc -->
<title>BarModelMapper Example | Qt Charts 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.9</td><td ><a href="qtcharts-index.html">Qt Charts</a></td><td ><a href="qtcharts-examples.html">Qt Charts Examples</a></td><td >BarModelMapper Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#using-data-models-with-bar-charts">Using Data Models with Bar Charts</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">BarModelMapper Example</h1>
<span class="subtitle"></span>
<!-- $$$barmodelmapper-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/examples_barmodelmapper.png" alt="" /></p><a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="using-data-models-with-bar-charts"></a>
<h2 id="using-data-models-with-bar-charts">Using Data Models with Bar Charts</h2>
<p>Let's start by creating an instance of CustomTableModel class. The CustomTableModel class is derived from QAbstractTableModel, and it was created for the purpose of this example. The constructor of this class populates the model's internal data store with the data required for our chart example.</p>
<pre class="cpp">
m_model <span class="operator">=</span> <span class="keyword">new</span> CustomTableModel;
</pre>
<p>We now have a model with data that we would like to display both on the chart and in a QTableView. First, we create QTableView and tell it to use the model as a data source. To have the data presented nicely, the minimum width of the table view is set and its headers resize mode changed to stretch.</p>
<pre class="cpp">
<span class="comment">// create table view and add model to it</span>
<span class="type">QTableView</span> <span class="operator">*</span>tableView <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QTableView</span>;
tableView<span class="operator">-</span><span class="operator">></span>setModel(m_model);
tableView<span class="operator">-</span><span class="operator">></span>setMinimumWidth(<span class="number">300</span>);
tableView<span class="operator">-</span><span class="operator">></span>horizontalHeader()<span class="operator">-</span><span class="operator">></span>setSectionResizeMode(<span class="type">QHeaderView</span><span class="operator">::</span>Stretch);
tableView<span class="operator">-</span><span class="operator">></span>verticalHeader()<span class="operator">-</span><span class="operator">></span>setSectionResizeMode(<span class="type">QHeaderView</span><span class="operator">::</span>Stretch);
m_model<span class="operator">-</span><span class="operator">></span>setParent(tableView);
</pre>
<p>Now we need a <a href="qchart.html">QChart</a> instance to display the same data on the chart. We also enable animations. It makes it easier to see how a modification to the model's data affects the chart.</p>
<pre class="cpp">
<span class="type"><a href="qchart.html">QChart</a></span> <span class="operator">*</span>chart <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qchart.html">QChart</a></span>;
chart<span class="operator">-</span><span class="operator">></span>setAnimationOptions(<span class="type"><a href="qchart.html">QChart</a></span><span class="operator">::</span>AllAnimations);
</pre>
<p>The first line of the code below creates new bar series. Variables firstRow and rowCount are used to define a custom model mapping. Custom mapping allows to take only part of the data from the model. In this case data from 5 rows starting with the row with the index 3. The following three lines create an instance of the <a href="qvbarmodelmapper.html">QVBarModelMapper</a> class and specify that the data for the bar sets should be taken from the model's columns with indexes from 1 to 4 (inclusive). To create a connection between the series and the model we set both of those objects to <a href="qvbarmodelmapper.html">QVBarModelMapper</a>.</p>
<p>Finally the series is added to the chart.</p>
<pre class="cpp">
<span class="type"><a href="qbarseries.html">QBarSeries</a></span> <span class="operator">*</span>series <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbarseries.html">QBarSeries</a></span>;
<span class="type">int</span> first <span class="operator">=</span> <span class="number">3</span>;
<span class="type">int</span> count <span class="operator">=</span> <span class="number">5</span>;
<span class="type"><a href="qvbarmodelmapper.html">QVBarModelMapper</a></span> <span class="operator">*</span>mapper <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvbarmodelmapper.html">QVBarModelMapper</a></span>(<span class="keyword">this</span>);
mapper<span class="operator">-</span><span class="operator">></span>setFirstBarSetColumn(<span class="number">1</span>);
mapper<span class="operator">-</span><span class="operator">></span>setLastBarSetColumn(<span class="number">4</span>);
mapper<span class="operator">-</span><span class="operator">></span>setFirstRow(first);
mapper<span class="operator">-</span><span class="operator">></span>setRowCount(count);
mapper<span class="operator">-</span><span class="operator">></span>setSeries(series);
mapper<span class="operator">-</span><span class="operator">></span>setModel(m_model);
chart<span class="operator">-</span><span class="operator">></span>addSeries(series);
</pre>
<p>To show in QTableView which data corresponds with which bar set, this example uses table coloring. When series is added to the chart, it is assigned a color based on the currently selected theme. Code below extracts that color from the series and uses it to create colored QTableView. Coloring of the view is not a part of the <a href="qchart.html">QChart</a> functionality.</p>
<pre class="cpp">
<span class="comment">// for storing color hex from the series</span>
<span class="type">QString</span> seriesColorHex <span class="operator">=</span> <span class="string">"#000000"</span>;
<span class="comment">// get the color of the series and use it for showing the mapped area</span>
<span class="type">QList</span><span class="operator"><</span><span class="type"><a href="qbarset.html">QBarSet</a></span> <span class="operator">*</span><span class="operator">></span> barsets <span class="operator">=</span> series<span class="operator">-</span><span class="operator">></span>barSets();
<span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator"><</span> barsets<span class="operator">.</span>count(); i<span class="operator">+</span><span class="operator">+</span>) {
seriesColorHex <span class="operator">=</span> <span class="string">"#"</span> <span class="operator">+</span> <span class="type">QString</span><span class="operator">::</span>number(barsets<span class="operator">.</span>at(i)<span class="operator">-</span><span class="operator">></span>brush()<span class="operator">.</span>color()<span class="operator">.</span>rgb()<span class="operator">,</span> <span class="number">16</span>)<span class="operator">.</span>right(<span class="number">6</span>)<span class="operator">.</span>toUpper();
m_model<span class="operator">-</span><span class="operator">></span>addMapping(seriesColorHex<span class="operator">,</span> <span class="type">QRect</span>(<span class="number">1</span> <span class="operator">+</span> i<span class="operator">,</span> first<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> barsets<span class="operator">.</span>at(i)<span class="operator">-</span><span class="operator">></span>count()));
}
</pre>
<p>We would like to have categories placed on the chart's axis that describe what the data means. Next snippet shows how to do that.</p>
<pre class="cpp">
<span class="type">QStringList</span> categories;
categories <span class="operator"><</span><span class="operator"><</span> <span class="string">"April"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"May"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"June"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"July"</span> <span class="operator"><</span><span class="operator"><</span> <span class="string">"August"</span>;
<span class="type"><a href="qbarcategoryaxis.html">QBarCategoryAxis</a></span> <span class="operator">*</span>axis <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbarcategoryaxis.html">QBarCategoryAxis</a></span>();
axis<span class="operator">-</span><span class="operator">></span>append(categories);
chart<span class="operator">-</span><span class="operator">></span>createDefaultAxes();
chart<span class="operator">-</span><span class="operator">></span>setAxisX(axis<span class="operator">,</span> series);
</pre>
<p>To avoid setting up the QGraphicsScene we use the <a href="qchartview.html">QChartView</a> class that does it for us. The <a href="qchart.html">QChart</a> object pointer is used as a parameter of the <a href="qchartview.html">QChartView</a> constructor. To make the render look nicer Antialiasing is turned on and the minimum size of the chartView widget is set.</p>
<pre class="cpp">
<span class="type"><a href="qchartview.html">QChartView</a></span> <span class="operator">*</span>chartView <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qchartview.html">QChartView</a></span>(chart);
chartView<span class="operator">-</span><span class="operator">></span>setRenderHint(<span class="type">QPainter</span><span class="operator">::</span>Antialiasing);
chartView<span class="operator">-</span><span class="operator">></span>setMinimumSize(<span class="number">640</span><span class="operator">,</span> <span class="number">480</span>);
</pre>
<p>Finally, we place both widgets in a layout and use the layout as the application layout.</p>
<pre class="cpp">
<span class="comment">// create main layout</span>
<span class="type">QGridLayout</span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QGridLayout</span>;
mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(tableView<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span>);
mainLayout<span class="operator">-</span><span class="operator">></span>addWidget(chartView<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
mainLayout<span class="operator">-</span><span class="operator">></span>setColumnStretch(<span class="number">1</span><span class="operator">,</span> <span class="number">1</span>);
mainLayout<span class="operator">-</span><span class="operator">></span>setColumnStretch(<span class="number">0</span><span class="operator">,</span> <span class="number">0</span>);
setLayout(mainLayout);
</pre>
<p>The application is ready. Try modifying the data in the table view and see how it affects the chart.</p>
<p>Files:</p>
<ul>
<li><a href="qtcharts-barmodelmapper-customtablemodel-cpp.html">barmodelmapper/customtablemodel.cpp</a></li>
<li><a href="qtcharts-barmodelmapper-customtablemodel-h.html">barmodelmapper/customtablemodel.h</a></li>
<li><a href="qtcharts-barmodelmapper-tablewidget-cpp.html">barmodelmapper/tablewidget.cpp</a></li>
<li><a href="qtcharts-barmodelmapper-tablewidget-h.html">barmodelmapper/tablewidget.h</a></li>
<li><a href="qtcharts-barmodelmapper-main-cpp.html">barmodelmapper/main.cpp</a></li>
<li><a href="qtcharts-barmodelmapper-barmodelmapper-pro.html">barmodelmapper/barmodelmapper.pro</a></li>
</ul>
</div>
<!-- @@@barmodelmapper -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|