tablewidget.cpp
112 lines
| 3.3 KiB
| text/x-c
|
CppLexer
|
r1367 | /**************************************************************************** | ||
** | ||||
|
r2688 | ** Copyright (C) 2014 Digia Plc | ||
|
r1367 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r1367 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r1367 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "tablewidget.h" | ||||
#include "customtablemodel.h" | ||||
#include <QGridLayout> | ||||
#include <QTableView> | ||||
#include <QChart> | ||||
#include <QChartView> | ||||
#include <QLineSeries> | ||||
#include <QVXYModelMapper> | ||||
|
r1594 | #include <QBarSeries> | ||
|
r1367 | #include <QBarSet> | ||
#include <QVBarModelMapper> | ||||
#include <QHeaderView> | ||||
|
r1809 | #include <QBarCategoryAxis> | ||
|
r1367 | |||
|
r2712 | QT_CHARTS_USE_NAMESPACE | ||
|
r1367 | |||
TableWidget::TableWidget(QWidget *parent) | ||||
: QWidget(parent) | ||||
{ | ||||
// create simple model for storing data | ||||
// user's table data model | ||||
//! [1] | ||||
CustomTableModel *model = new CustomTableModel; | ||||
//! [1] | ||||
//! [2] | ||||
// create table view and add model to it | ||||
QTableView *tableView = new QTableView; | ||||
tableView->setModel(model); | ||||
|
r1393 | tableView->setMinimumWidth(300); | ||
|
r2241 | tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||||
|
r1367 | //! [2] | ||
//! [3] | ||||
QChart *chart = new QChart; | ||||
chart->setAnimationOptions(QChart::AllAnimations); | ||||
//! [3] | ||||
// series 1 | ||||
//! [4] | ||||
|
r1594 | QBarSeries *series = new QBarSeries; | ||
|
r1367 | |||
int first = 3; | ||||
int count = 5; | ||||
QVBarModelMapper *mapper = new QVBarModelMapper(this); | ||||
mapper->setFirstBarSetColumn(1); | ||||
mapper->setLastBarSetColumn(4); | ||||
|
r1495 | mapper->setFirstRow(first); | ||
mapper->setRowCount(count); | ||||
|
r1367 | mapper->setSeries(series); | ||
mapper->setModel(model); | ||||
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 | ||||
|
r2102 | QList<QBarSet *> barsets = series->barSets(); | ||
|
r1367 | for (int i = 0; i < barsets.count(); i++) { | ||
seriesColorHex = "#" + QString::number(barsets.at(i)->brush().color().rgb(), 16).right(6).toUpper(); | ||||
model->addMapping(seriesColorHex, QRect(1 + i, first, 1, barsets.at(i)->count())); | ||||
} | ||||
//! [5] | ||||
|
r1369 | //! [6] | ||
QStringList categories; | ||||
categories << "April" << "May" << "June" << "July" << "August"; | ||||
|
r2102 | QBarCategoryAxis *axis = new QBarCategoryAxis(); | ||
|
r2098 | axis->append(categories); | ||
|
r1577 | chart->createDefaultAxes(); | ||
|
r1582 | chart->setAxisX(axis, series); | ||
|
r1369 | //! [6] | ||
//! [7] | ||||
|
r1367 | QChartView *chartView = new QChartView(chart); | ||
chartView->setRenderHint(QPainter::Antialiasing); | ||||
chartView->setMinimumSize(640, 480); | ||||
|
r1369 | //! [7] | ||
|
r1367 | |||
|
r1369 | //! [8] | ||
|
r1367 | // create main layout | ||
|
r2102 | QGridLayout *mainLayout = new QGridLayout; | ||
|
r1367 | mainLayout->addWidget(tableView, 1, 0); | ||
mainLayout->addWidget(chartView, 1, 1); | ||||
mainLayout->setColumnStretch(1, 1); | ||||
mainLayout->setColumnStretch(0, 0); | ||||
setLayout(mainLayout); | ||||
|
r1369 | //! [8] | ||
|
r1367 | } | ||