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