tablewidget.cpp
121 lines
| 4.0 KiB
| text/x-c
|
CppLexer
Miikka Heikkinen
|
r2854 | /**************************************************************************** | ||
Marek Rosa
|
r1367 | ** | ||
Miikka Heikkinen
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
Marek Rosa
|
r1367 | ** | ||
Miikka Heikkinen
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
Marek Rosa
|
r1367 | ** | ||
Miikka Heikkinen
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
Titta Heikkala
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
Miikka Heikkinen
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
Marek Rosa
|
r1367 | ** | ||
Titta Heikkala
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
Miikka Heikkinen
|
r2854 | ****************************************************************************/ | ||
Marek Rosa
|
r1367 | |||
#include "tablewidget.h" | ||||
Titta Heikkala
|
r2714 | #include <QtWidgets/QGridLayout> | ||
#include <QtWidgets/QTableView> | ||||
#include <QtCharts/QChart> | ||||
#include <QtCharts/QChartView> | ||||
#include <QtCharts/QLineSeries> | ||||
#include <QtCharts/QVXYModelMapper> | ||||
#include <QtCharts/QBarSeries> | ||||
#include <QtCharts/QBarSet> | ||||
#include <QtCharts/QVBarModelMapper> | ||||
#include <QtWidgets/QHeaderView> | ||||
#include <QtCharts/QBarCategoryAxis> | ||||
Marek Rosa
|
r1367 | |||
Titta Heikkala
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
Marek Rosa
|
r1367 | |||
TableWidget::TableWidget(QWidget *parent) | ||||
: QWidget(parent) | ||||
{ | ||||
// create simple model for storing data | ||||
// user's table data model | ||||
//! [1] | ||||
Miikka Heikkinen
|
r2733 | m_model = new CustomTableModel; | ||
Marek Rosa
|
r1367 | //! [1] | ||
//! [2] | ||||
// create table view and add model to it | ||||
QTableView *tableView = new QTableView; | ||||
Miikka Heikkinen
|
r2733 | tableView->setModel(m_model); | ||
Marek Rosa
|
r1393 | tableView->setMinimumWidth(300); | ||
Jani Honkonen
|
r2241 | tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||||
Miikka Heikkinen
|
r2733 | m_model->setParent(tableView); | ||
Marek Rosa
|
r1367 | //! [2] | ||
//! [3] | ||||
QChart *chart = new QChart; | ||||
chart->setAnimationOptions(QChart::AllAnimations); | ||||
//! [3] | ||||
// series 1 | ||||
//! [4] | ||||
sauimone
|
r1594 | QBarSeries *series = new QBarSeries; | ||
Marek Rosa
|
r1367 | |||
int first = 3; | ||||
int count = 5; | ||||
QVBarModelMapper *mapper = new QVBarModelMapper(this); | ||||
mapper->setFirstBarSetColumn(1); | ||||
mapper->setLastBarSetColumn(4); | ||||
Marek Rosa
|
r1495 | mapper->setFirstRow(first); | ||
mapper->setRowCount(count); | ||||
Marek Rosa
|
r1367 | mapper->setSeries(series); | ||
Miikka Heikkinen
|
r2733 | mapper->setModel(m_model); | ||
Marek Rosa
|
r1367 | chart->addSeries(series); | ||
//! [4] | ||||
//! [5] | ||||
// for storing color hex from the series | ||||
QString seriesColorHex = "#000000"; | ||||
// get the color of the series and use it for showing the mapped area | ||||
Jani Honkonen
|
r2102 | QList<QBarSet *> barsets = series->barSets(); | ||
Marek Rosa
|
r1367 | for (int i = 0; i < barsets.count(); i++) { | ||
seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); | ||||
Miikka Heikkinen
|
r2733 | m_model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count())); | ||
Marek Rosa
|
r1367 | } | ||
//! [5] | ||||
Marek Rosa
|
r1369 | //! [6] | ||
QStringList categories; | ||||
categories << "April" << "May" << "June" << "July" << "August"; | ||||
Jani Honkonen
|
r2102 | QBarCategoryAxis *axis = new QBarCategoryAxis(); | ||
Jani Honkonen
|
r2098 | axis->append(categories); | ||
Michal Klocek
|
r1577 | chart->createDefaultAxes(); | ||
Marek Rosa
|
r1582 | chart->setAxisX(axis, series); | ||
Marek Rosa
|
r1369 | //! [6] | ||
//! [7] | ||||
Marek Rosa
|
r1367 | QChartView *chartView = new QChartView(chart); | ||
chartView->setRenderHint(QPainter::Antialiasing); | ||||
chartView->setMinimumSize(640, 480); | ||||
Marek Rosa
|
r1369 | //! [7] | ||
Marek Rosa
|
r1367 | |||
Marek Rosa
|
r1369 | //! [8] | ||
Marek Rosa
|
r1367 | // create main layout | ||
Jani Honkonen
|
r2102 | QGridLayout *mainLayout = new QGridLayout; | ||
Marek Rosa
|
r1367 | mainLayout->addWidget(tableView, 1, 0); | ||
mainLayout->addWidget(chartView, 1, 1); | ||||
mainLayout->setColumnStretch(1, 1); | ||||
mainLayout->setColumnStretch(0, 0); | ||||
setLayout(mainLayout); | ||||
Marek Rosa
|
r1369 | //! [8] | ||
Marek Rosa
|
r1367 | } | ||