diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index e3208ce..8979f2b 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -476,6 +476,7 @@ void QPieSeries::setModelMappingRange(int first, int count) Q_D(QPieSeries); d->m_mapFirst = first; d->m_mapCount = count; + d->initializePieFromModel(); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/test/tablemodelchart/customtablemodel.cpp b/test/tablemodelchart/customtablemodel.cpp index e15f715..d4368b9 100644 --- a/test/tablemodelchart/customtablemodel.cpp +++ b/test/tablemodelchart/customtablemodel.cpp @@ -29,9 +29,19 @@ CustomTableModel::CustomTableModel(QObject *parent) : { qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); - m_columnCount = 6; + m_columnCount = 3; m_rowCount = 9; + m_labels.append("Apples"); + m_labels.append("Oranges"); + m_labels.append("Pears"); + m_labels.append("Peaches"); + m_labels.append("Coconuts"); + m_labels.append("Bananas"); + m_labels.append("Kiwis"); + m_labels.append("Grapes"); + m_labels.append("Plums"); + // m_data for (int i = 0; i < m_rowCount; i++) { @@ -44,7 +54,7 @@ CustomTableModel::CustomTableModel(QObject *parent) : dataVec->replace(k, qrand()%100); } m_data.append(dataVec); - m_labels.append(QString("Row: %1").arg((i + 1))); + // m_labels.append(QString("Row: %1").arg((i + 1))); } } @@ -69,8 +79,12 @@ QVariant CustomTableModel::headerData (int section, Qt::Orientation orientation, { switch(section) { - // case 6: - // return "Fruit"; + case 0: + return "Fruit"; + case 1: + return "Count"; + case 2: + return "Ordered"; default: if (section%2 == 0) return "x"; @@ -88,8 +102,8 @@ QVariant CustomTableModel::data(const QModelIndex & index, int role) const { switch(index.column()) { - // case 6: - // return m_labels[index.row()]; + case 0: + return m_labels[index.row()]; default: return m_data[index.row()]->at(index.column()); break; @@ -99,8 +113,8 @@ QVariant CustomTableModel::data(const QModelIndex & index, int role) const { switch(index.column()) { - // case 6: - // return m_labels[index.row()]; + case 0: + return m_labels[index.row()]; default: return m_data[index.row()]->at(index.column()); break; @@ -125,9 +139,9 @@ bool CustomTableModel::setData ( const QModelIndex & index, const QVariant & val { switch(index.column()) { - // case 6: - // m_labels.replace(index.row(), value.toString()); - // break; + case 0: + m_labels.replace(index.row(), value.toString()); + break; default: m_data[index.row()]->replace(index.column(), value.toDouble()); break; diff --git a/test/tablemodelchart/tablewidget.cpp b/test/tablemodelchart/tablewidget.cpp index 35ef9f5..af81e48 100644 --- a/test/tablemodelchart/tablewidget.cpp +++ b/test/tablemodelchart/tablewidget.cpp @@ -37,6 +37,7 @@ #include #include #include +#include TableWidget::TableWidget(QWidget *parent) : QWidget(parent) @@ -49,7 +50,10 @@ TableWidget::TableWidget(QWidget *parent) m_model = new CustomTableModel; m_tableView = new QTableView; m_tableView->setModel(m_model); - m_tableView->setMinimumHeight(300); +// m_tableView->setMinimumHeight(300); + m_tableView->horizontalHeader()->setResizeMode(QHeaderView::Stretch); + m_tableView->verticalHeader()->setResizeMode(QHeaderView::Stretch); + m_chart = new QChart; m_chart->legend()->setVisible(true); m_chart->setAnimationOptions(QChart::SeriesAnimations); @@ -86,13 +90,13 @@ TableWidget::TableWidget(QWidget *parent) // buttons layout QVBoxLayout* buttonsLayout = new QVBoxLayout; - buttonsLayout->addWidget(spinBoxLabel); - buttonsLayout->addWidget(m_linesCountSpinBox); - buttonsLayout->addWidget(addRowAboveButton); +// buttonsLayout->addWidget(spinBoxLabel); +// buttonsLayout->addWidget(m_linesCountSpinBox); +// buttonsLayout->addWidget(addRowAboveButton); buttonsLayout->addWidget(addRowBelowButton); buttonsLayout->addWidget(removeRowButton); - buttonsLayout->addWidget(addColumnRightButton); - buttonsLayout->addWidget(removeColumnButton); +// buttonsLayout->addWidget(addColumnRightButton); +// buttonsLayout->addWidget(removeColumnButton); // buttonsLayout->addWidget(specialPieButton); buttonsLayout->addStretch(); @@ -110,7 +114,7 @@ TableWidget::TableWidget(QWidget *parent) connect(m_pieRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); connect(m_areaRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); connect(m_barRadioButton, SIGNAL(toggled(bool)), this, SLOT(updateChartType(bool))); - m_lineRadioButton->setChecked(true); + m_pieRadioButton->setChecked(true); // radio buttons layout QVBoxLayout* radioLayout = new QVBoxLayout; @@ -124,10 +128,10 @@ TableWidget::TableWidget(QWidget *parent) // create main layout QGridLayout* mainLayout = new QGridLayout; - mainLayout->addLayout(buttonsLayout, 1, 1); - mainLayout->addLayout(radioLayout, 2, 1); + mainLayout->addLayout(buttonsLayout, 2, 0); +// mainLayout->addLayout(radioLayout, 2, 1); mainLayout->addWidget(m_tableView, 1, 0); - mainLayout->addWidget(m_chartView, 2, 0); + mainLayout->addWidget(m_chartView, 1, 1, 2, 1); setLayout(mainLayout); m_lineRadioButton->setFocus(); } @@ -294,41 +298,42 @@ void TableWidget::updateChartType(bool toggle) // pie 1 QPieSeries* pieSeries = new QPieSeries; pieSeries->setModel(m_model); - pieSeries->setModelMappingRange(3, 3); - pieSeries->setModelMapping(0,0, Qt::Vertical); + pieSeries->setModelMappingRange(2, 5); + pieSeries->setModelMapping(1, 0, Qt::Vertical); pieSeries->setLabelsVisible(true); - pieSeries->setPieSize(0.35); - pieSeries->setHorizontalPosition(0.2); - pieSeries->setVerticalPosition(0.3); - - m_chart->addSeries(pieSeries); - seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); - m_model->addMapping(seriesColorHex, QRect(0, 3, 1, 3)); + pieSeries->setPieSize(0.75); +// pieSeries->setHorizontalPosition(0.2); +// pieSeries->setVerticalPosition(0.3); - // pie 2 - pieSeries = new QPieSeries; - pieSeries->setModel(m_model); - pieSeries->setModelMappingRange(2, -1); - pieSeries->setModelMapping(1,1, Qt::Vertical); - pieSeries->setLabelsVisible(true); - pieSeries->setPieSize(0.35); - pieSeries->setHorizontalPosition(0.8); - pieSeries->setVerticalPosition(0.3); - m_chart->addSeries(pieSeries); - seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); - m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000)); - - // pie 3 - pieSeries = new QPieSeries; - pieSeries->setModel(m_model); - pieSeries->setModelMapping(2,2, Qt::Vertical); - pieSeries->setLabelsVisible(true); - pieSeries->setPieSize(0.35); - pieSeries->setHorizontalPosition(0.5); - pieSeries->setVerticalPosition(0.75); m_chart->addSeries(pieSeries); seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); - m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000)); + m_model->addMapping(seriesColorHex, QRect(0, 2, 2, 5)); + +// // pie 2 +// pieSeries = new QPieSeries; +// pieSeries->setModel(m_model); + +// pieSeries->setModelMapping(1,1, Qt::Vertical); +// pieSeries->setModelMappingRange(2, -1); +// pieSeries->setLabelsVisible(true); +// pieSeries->setPieSize(0.35); +// pieSeries->setHorizontalPosition(0.8); +// pieSeries->setVerticalPosition(0.3); +// m_chart->addSeries(pieSeries); +// seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); +// m_model->addMapping(seriesColorHex, QRect(1, 2, 1, 1000)); + +// // pie 3 +// pieSeries = new QPieSeries; +// pieSeries->setModel(m_model); +// pieSeries->setModelMapping(2,2, Qt::Vertical); +// pieSeries->setLabelsVisible(true); +// pieSeries->setPieSize(0.35); +// pieSeries->setHorizontalPosition(0.5); +// pieSeries->setVerticalPosition(0.75); +// m_chart->addSeries(pieSeries); +// seriesColorHex = "#" + QString::number(pieSeries->slices().at(pieSeries->slices().count()/2)->brush().color().rgb(), 16).right(6).toUpper(); +// m_model->addMapping(seriesColorHex, QRect(2, 0, 1, 1000)); // // special pie // specialPie = new QPieSeries;