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