##// END OF EJS Templates
legend example update
sauimone -
r1273:096757dada5b
parent child
Show More
@@ -1,134 +1,128
1 1 #include "mainwidget.h"
2 2 #include <QChart>
3 3 #include <QChartView>
4 4 #include <QPushButton>
5 5 #include <QLabel>
6 6 #include <QDebug>
7 7 #include <QBarSet>
8 8 #include <QBarSeries>
9 9 #include <QLegend>
10 10
11 11 QTCOMMERCIALCHART_USE_NAMESPACE
12 12
13 13 MainWidget::MainWidget(QWidget *parent) :
14 14 QWidget(parent)
15 15 {
16 16 m_setCount = 0;
17 17 m_chart = new QChart();
18 18
19 //![1]
19 20 m_buttonLayout = new QGridLayout();
20 21 QPushButton *detachLegendButton = new QPushButton("detach legend");
21 22 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
22 23 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
23 24 QPushButton *attachLegendButton = new QPushButton("attach legend");
24 25 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
25 26 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
26 27
27 28 QPushButton *addSetButton = new QPushButton("add barset");
28 29 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
29 30 m_buttonLayout->addWidget(addSetButton, 2, 0);
30 31 QPushButton *removeBarsetButton = new QPushButton("remove barset");
31 32 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
32 33 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
33
34 // add row with empty label to make all the other rows static
35 m_buttonLayout->addWidget(new QLabel(""), m_buttonLayout->rowCount(), 0);
36 m_buttonLayout->setRowStretch(m_buttonLayout->rowCount() - 1, 1);
34 //![1]
37 35
38 36 // Create chart view with the chart
39 37 m_chartView = new QChartView(m_chart, this);
40 38 m_chartView->setRubberBand(QChartView::HorizonalRubberBand);
41 39
40 //![2]
42 41 m_customView = new QGraphicsView(this);
43 42 m_customScene = new QGraphicsScene(this);
44 43 m_customView->setScene(m_customScene);
44 //![2]
45 45
46 46 // Create layout for grid and detached legend
47 47 m_mainLayout = new QGridLayout();
48 48 m_mainLayout->addLayout(m_buttonLayout, 0, 0);
49 49 m_mainLayout->addWidget(m_chartView, 0, 1, 3, 1);
50 50 m_mainLayout->addWidget(m_customView, 0, 2, 3, 1);
51 51 setLayout(m_mainLayout);
52 52
53 53 createSeries();
54 54 }
55 55
56 56 void MainWidget::createSeries()
57 57 {
58 //![1]
59 58 m_series = new QBarSeries();
60
61 59 addBarset();
62 60 addBarset();
63 61 addBarset();
64 62 addBarset();
65 //![1]
66 63
67 //![2]
68 64 m_chart->addSeries(m_series);
69 65 m_chart->setTitle("Legend detach example");
70 //![2]
71 66
72 //![3]
73 67 m_chart->legend()->setVisible(true);
74 68 m_chart->legend()->setAlignment(QLegend::AlignmentBottom);
75 69 m_chart->axisY()->setNiceNumbersEnabled(true);
76 //![3]
77 70
78 //![4]
79 71 m_chartView->setRenderHint(QPainter::Antialiasing);
80 //![4]
81 72 }
82 73
83 void MainWidget::attachLegend()
74 void MainWidget::detachLegend()
84 75 {
85 qDebug() << "attach legend";
76 //![3]
77 // Detach legend from chart
86 78 QLegend *legend = m_chart->legend();
79 legend->detachFromChart();
87 80
88 if (m_customScene->items().contains(legend)) {
89 qDebug() << "legend removed from other scene";
90 m_customScene->removeItem(legend);
91 legend->setParent(m_chart);
92 legend->attachToChart();
93 }
81 // Put legend to our custom scene
82 m_customScene->addItem(legend);
83 //![3]
94 84
95 // legend->attachToChart();
96 85 // This causes redraw
97 86 QSize size(1,1);
98 87 this->resize(this->size() + size);
99 88 this->resize(this->size() - size);
100 89 }
101 90
102 void MainWidget::detachLegend()
91
92 void MainWidget::attachLegend()
103 93 {
104 qDebug() << "detach legend";
94 //![4]
105 95 QLegend *legend = m_chart->legend();
106 legend->detachFromChart();
107 96
108 m_customScene->addItem(legend);
109 // m_mainLayout->addWidget(legend,0,2,3,1);
110 // setLayout(m_layout);
97 if (m_customScene->items().contains(legend)) {
98 // Remove legend from custom scene and put it back to chartview scene.
99 // Attach legend back to chart, so that layout works.
100 m_customScene->removeItem(legend);
101 legend->setParent(m_chart);
102 m_chartView->scene()->addItem(legend);
103 legend->attachToChart();
104 }
105 //![4]
111 106
112 // TODO: layout legend to somewhere else
113 107 // This causes redraw
114 108 QSize size(1,1);
115 109 this->resize(this->size() + size);
116 110 this->resize(this->size() - size);
117 111 }
118 112
119 113 void MainWidget::addBarset()
120 114 {
121 115 QBarSet *barSet = new QBarSet(QString("set ") + QString::number(m_setCount));
122 116 m_setCount++;
123 117 qreal delta = m_series->barsetCount() * 0.1;
124 118 *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);
125 119 m_series->append(barSet);
126 120 }
127 121
128 122 void MainWidget::removeBarset()
129 123 {
130 124 QList<QBarSet*> sets = m_series->barSets();
131 125 if (sets.count() > 0) {
132 126 m_series->remove(sets.at(sets.count()-1));
133 127 }
134 128 }
@@ -1,49 +1,49
1 1 #ifndef MAINWIDGET_H
2 2 #define MAINWIDGET_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "qchart.h"
6 6 #include "qchartview.h"
7 7 #include <QWidget>
8 8 #include <QGraphicsWidget>
9 9 #include <QGridLayout>
10 10 #include <QGraphicsGridLayout>
11 11
12 12 QTCOMMERCIALCHART_USE_NAMESPACE
13 13
14 14 class MainWidget : public QWidget
15 15 //class MainWidget : public QGraphicsWidget
16 16 {
17 17 Q_OBJECT
18 18 public:
19 19 explicit MainWidget(QWidget *parent = 0);
20 20
21 21 void createSeries();
22 22
23 23 signals:
24 24
25 25 public slots:
26 void attachLegend();
27 26 void detachLegend();
27 void attachLegend();
28 28 void addBarset();
29 29 void removeBarset();
30 30
31 31 private:
32 32
33 33 QChart *m_chart;
34 34 QBarSeries *m_series;
35 35
36 36 QGraphicsScene *m_scene;
37 37 QChartView *m_chartView;
38 38 QGridLayout *m_mainLayout;
39 39 QGridLayout *m_buttonLayout;
40 40
41 41 QGraphicsView *m_customView;
42 42 QGraphicsScene *m_customScene;
43 43 QGraphicsGridLayout *m_customLayout;
44 44
45 45
46 46 int m_setCount;
47 47 };
48 48
49 49 #endif // MAINWIDGET_H
General Comments 0
You need to be logged in to leave comments. Login now