##// END OF EJS Templates
fixed crash in legend example
sauimone -
r1396:e5d6ed20234d
parent child
Show More
@@ -1,151 +1,149
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "mainwidget.h"
21 #include "mainwidget.h"
22 #include <QChart>
22 #include <QChart>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QLabel>
25 #include <QLabel>
26 #include <QDebug>
26 #include <QDebug>
27 #include <QBarSet>
27 #include <QBarSet>
28 #include <QBarSeries>
28 #include <QBarSeries>
29 #include <QLegend>
29 #include <QLegend>
30
30
31 QTCOMMERCIALCHART_USE_NAMESPACE
31 QTCOMMERCIALCHART_USE_NAMESPACE
32
32
33 MainWidget::MainWidget(QWidget *parent) :
33 MainWidget::MainWidget(QWidget *parent) :
34 QWidget(parent)
34 QWidget(parent)
35 {
35 {
36 // Create buttons for ui
36 // Create buttons for ui
37 m_buttonLayout = new QGridLayout();
37 m_buttonLayout = new QGridLayout();
38 QPushButton *detachLegendButton = new QPushButton("detach legend");
38 QPushButton *detachLegendButton = new QPushButton("detach legend");
39 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
39 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
40 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
40 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
41 QPushButton *attachLegendButton = new QPushButton("attach legend");
41 QPushButton *attachLegendButton = new QPushButton("attach legend");
42 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
42 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
43 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
43 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
44
44
45 QPushButton *addSetButton = new QPushButton("add barset");
45 QPushButton *addSetButton = new QPushButton("add barset");
46 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
46 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
47 m_buttonLayout->addWidget(addSetButton, 2, 0);
47 m_buttonLayout->addWidget(addSetButton, 2, 0);
48 QPushButton *removeBarsetButton = new QPushButton("remove barset");
48 QPushButton *removeBarsetButton = new QPushButton("remove barset");
49 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
49 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
50 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
50 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
51
51
52 // Create chart view with the chart
52 // Create chart view with the chart
53 //![1]
53 //![1]
54 m_chart = new QChart();
54 m_chart = new QChart();
55 m_chartView = new QChartView(m_chart, this);
55 m_chartView = new QChartView(m_chart, this);
56 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
56 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
57 m_chart->setAnimationOptions(QChart::SeriesAnimations);
58 //![1]
57 //![1]
59
58
60 // Create custom scene and view, where detached legend will be drawn
59 // Create custom scene and view, where detached legend will be drawn
61 //![2]
60 //![2]
62 m_customView = new QGraphicsView(this);
61 m_customView = new QGraphicsView(this);
63 m_customScene = new QGraphicsScene(this);
62 m_customScene = new QGraphicsScene(this);
64 m_customView->setScene(m_customScene);
63 m_customView->setScene(m_customScene);
65 //![2]
64 //![2]
66
65
67 // Create layout for grid and detached legend
66 // Create layout for grid and detached legend
68 m_mainLayout = new QGridLayout();
67 m_mainLayout = new QGridLayout();
69 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
68 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
70 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
69 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
71 m_mainLayout->addWidget(m_customView, 0, 2, 3, 1);
70 m_mainLayout->addWidget(m_customView, 0, 2, 3, 1);
72 setLayout(m_mainLayout);
71 setLayout(m_mainLayout);
73
72
74 createSeries();
73 createSeries();
75 }
74 }
76
75
77 void MainWidget::createSeries()
76 void MainWidget::createSeries()
78 {
77 {
79 //![3]
78 //![3]
80 m_series = new QBarSeries();
79 m_series = new QBarSeries();
81 addBarset();
80 addBarset();
82 addBarset();
81 addBarset();
83 addBarset();
82 addBarset();
84 addBarset();
83 addBarset();
85
84
86 m_chart->addSeries(m_series);
85 m_chart->addSeries(m_series);
87 m_chart->setTitle("Legend detach example");
86 m_chart->setTitle("Legend detach example");
88
87
89 m_chart->legend()->setVisible(true);
88 m_chart->legend()->setVisible(true);
90 m_chart->legend()->setAlignment(Qt::AlignBottom);
89 m_chart->legend()->setAlignment(Qt::AlignBottom);
91 m_chart->axisY()->setNiceNumbersEnabled(true);
90 m_chart->axisY()->setNiceNumbersEnabled(true);
92
91
93 m_chartView->setRenderHint(QPainter::Antialiasing);
92 m_chartView->setRenderHint(QPainter::Antialiasing);
94 //![3]
93 //![3]
95 }
94 }
96
95
97 void MainWidget::detachLegend()
96 void MainWidget::detachLegend()
98 {
97 {
99 // Detach legend from chart and
98 // Detach legend from chart and
100 // put legend to our custom scene
99 // put legend to our custom scene
101 //![4]
100 //![4]
102 QLegend *legend = m_chart->legend();
101 QLegend *legend = m_chart->legend();
103 legend->detachFromChart();
102 legend->detachFromChart();
104 legend->setGeometry(m_customView->rect());
103 legend->setGeometry(m_customView->rect());
105 m_customScene->addItem(legend);
104 m_customScene->addItem(legend);
106 //![4]
105 //![4]
107
106
108 // This forces redraw
107 // This forces redraw
109 QSize delta(1,1);
108 QSize delta(1,1);
110 resize(size() + delta);
109 resize(size() + delta);
111 resize(size() - delta);
110 resize(size() - delta);
112 }
111 }
113
112
114
113
115 void MainWidget::attachLegend()
114 void MainWidget::attachLegend()
116 {
115 {
117 // Remove legend from custom scene and put it back to chartview scene.
116 // Remove legend from custom scene and put it back to chartview scene.
118 // Attach legend back to chart, so that layout works.
117 // Attach legend back to chart, so that layout works.
119
118
120 //![5]
119 //![5]
121 QLegend *legend = m_chart->legend();
120 QLegend *legend = m_chart->legend();
122
121
123 if (m_customScene->items().contains(legend)) {
122 if (m_customScene->items().contains(legend)) {
124 m_customScene->removeItem(legend);
123 m_customScene->removeItem(legend);
125 m_chartView->scene()->addItem(legend);
124 m_chartView->scene()->addItem(legend);
126 legend->attachToChart();
125 legend->attachToChart();
127 }
126 }
128 //![5]
127 //![5]
129
128
130 // This forces redraw
129 // This forces redraw
131 QSize delta(1,1);
130 QSize delta(1,1);
132 resize(size() + delta);
131 resize(size() + delta);
133 resize(size() - delta);
132 resize(size() - delta);
134 }
133 }
135
134
136 void MainWidget::addBarset()
135 void MainWidget::addBarset()
137 {
136 {
138 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount()));
137 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_series->barsetCount()));
139 qreal delta = m_series->barsetCount() * 0.1;
138 qreal delta = m_series->barsetCount() * 0.1;
140 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
139 *barSet << QPointF(0.0 + delta, 1 + delta) << QPointF(1.0 + delta, 2 + delta) << QPointF(2.0 + delta, 3 + delta) << QPointF(3.0 + delta, 4 + delta);
141 m_series->append(barSet);
140 m_series->append(barSet);
142 }
141 }
143
142
144 void MainWidget::removeBarset()
143 void MainWidget::removeBarset()
145 {
144 {
146 m_series->clear();
145 QList<QBarSet*> sets = m_series->barSets();
147 // QList<QBarSet*> sets = m_series->barSets();
146 if (sets.count() > 0) {
148 // if (sets.count() > 0) {
147 m_series->remove(sets.at(sets.count()-1));
149 // m_series->remove(sets.at(sets.count()-1));
148 }
150 // }
151 }
149 }
General Comments 0
You need to be logged in to leave comments. Login now