@@ -16,6 +16,7 MainWidget::MainWidget(QWidget *parent) : | |||
|
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())); |
@@ -30,18 +31,17 MainWidget::MainWidget(QWidget *parent) : | |||
|
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(); |
@@ -55,61 +55,55 MainWidget::MainWidget(QWidget *parent) : | |||
|
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:: |
|
|
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-> |
|
|
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); |
General Comments 0
You need to be logged in to leave comments.
Login now