##// END OF EJS Templates
Fixed a bug with legend attach requiring a screen resize to update UI
Tero Ahola -
r2034:40ab6eb6f3e9
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: modified file, binary diff hidden
@@ -8,7 +8,11
8 8 In example we use barseries where we add or remove barsets. The legend reflects the changes in series. Legend can be detached or attached back to chart and its alignment can be modified.
9 9 When legend is detached, it can be resized and positioned freely.
10 10
11 \image examples_legend_detach.png
11 \table
12 \row
13 \o \inlineimage examples_legend_detach.png
14 \o \inlineimage examples_legend_detach2.png
15 \endtable
12 16
13 17 Here we turn legend visible and set its alignment to the bottom of the chart.
14 18
@@ -30,7 +30,7 int main(int argc, char *argv[])
30 30 QApplication a(argc, argv);
31 31
32 32 MainWidget w;
33 w.resize(1024,768);
33 w.resize(720, 480);
34 34 w.show();
35 35
36 36 return a.exec();
@@ -36,12 +36,9 MainWidget::MainWidget(QWidget *parent) :
36 36 {
37 37 // Create buttons for ui
38 38 m_buttonLayout = new QGridLayout();
39 QPushButton *detachLegendButton = new QPushButton("detach legend");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(detachLegend()));
39 QPushButton *detachLegendButton = new QPushButton("Toggle attached");
40 connect(detachLegendButton, SIGNAL(clicked()), this, SLOT(toggleAttached()));
41 41 m_buttonLayout->addWidget(detachLegendButton, 0, 0);
42 QPushButton *attachLegendButton = new QPushButton("attach legend");
43 connect(attachLegendButton, SIGNAL(clicked()), this, SLOT(attachLegend()));
44 m_buttonLayout->addWidget(attachLegendButton, 1, 0);
45 42
46 43 QPushButton *addSetButton = new QPushButton("add barset");
47 44 connect(addSetButton, SIGNAL(clicked()), this, SLOT(addBarset()));
@@ -50,21 +47,9 MainWidget::MainWidget(QWidget *parent) :
50 47 connect(removeBarsetButton, SIGNAL(clicked()), this, SLOT(removeBarset()));
51 48 m_buttonLayout->addWidget(removeBarsetButton, 3, 0);
52 49
53 QPushButton *leftButton = new QPushButton("Align legend left");
54 connect(leftButton, SIGNAL(clicked()), this, SLOT(setLegendLeft()));
55 m_buttonLayout->addWidget(leftButton, 4, 0);
56
57 QPushButton *rightButton = new QPushButton("Align legend right");
58 connect(rightButton, SIGNAL(clicked()), this, SLOT(setLegendRight()));
59 m_buttonLayout->addWidget(rightButton, 5, 0);
60
61 QPushButton *topButton = new QPushButton("Align legend top");
62 connect(topButton, SIGNAL(clicked()), this, SLOT(setLegendTop()));
63 m_buttonLayout->addWidget(topButton, 6, 0);
64
65 QPushButton *bottomButton = new QPushButton("Align legend bottom");
66 connect(bottomButton, SIGNAL(clicked()), this, SLOT(setLegendBottom()));
67 m_buttonLayout->addWidget(bottomButton, 7, 0);
50 QPushButton *alignButton = new QPushButton("Align (Bottom)");
51 connect(alignButton, SIGNAL(clicked()), this, SLOT(setLegendAlignment()));
52 m_buttonLayout->addWidget(alignButton, 4, 0);
68 53
69 54 QPushButton *boldButton = new QPushButton("Toggle bold");
70 55 connect(boldButton, SIGNAL(clicked()), this, SLOT(toggleBold()));
@@ -85,8 +70,8 MainWidget::MainWidget(QWidget *parent) :
85 70 connect(m_legendHeight, SIGNAL(valueChanged(double)), this, SLOT(updateLegendLayout()));
86 71
87 72 QFormLayout* legendLayout = new QFormLayout();
88 legendLayout->addRow("Horizontal position", m_legendPosX);
89 legendLayout->addRow("Vertical position", m_legendPosY);
73 legendLayout->addRow("HPos", m_legendPosX);
74 legendLayout->addRow("VPos", m_legendPosY);
90 75 legendLayout->addRow("Width", m_legendWidth);
91 76 legendLayout->addRow("Height", m_legendHeight);
92 77 m_legendSettings = new QGroupBox("Detached legend");
@@ -163,32 +148,25 void MainWidget::hideLegendSpinbox()
163 148 }
164 149
165 150
166 void MainWidget::detachLegend()
151 void MainWidget::toggleAttached()
167 152 {
168 //![2]
169 153 QLegend *legend = m_chart->legend();
154 if (legend->isAttachedToChart()) {
155 //![2]
170 156 legend->detachFromChart();
171
172 157 m_chart->legend()->setBackgroundVisible(true);
173 158 m_chart->legend()->setBrush(QBrush(QColor(128,128,128,128)));
174 159 m_chart->legend()->setPen(QPen(QColor(192,192,192,192)));
175 160 //![2]
176
177 161 showLegendSpinbox();
178 162 updateLegendLayout();
179 update();
180 }
181
182
183 void MainWidget::attachLegend()
184 {
163 } else {
185 164 //![3]
186 QLegend *legend = m_chart->legend();
187 165 legend->attachToChart();
188 m_chart->legend()->setBackgroundVisible(false);
166 legend->setBackgroundVisible(false);
189 167 //![3]
190
191 168 hideLegendSpinbox();
169 }
192 170 update();
193 171 }
194 172
@@ -208,24 +186,32 void MainWidget::removeBarset()
208 186 }
209 187 }
210 188
211 void MainWidget::setLegendLeft()
189 void MainWidget::setLegendAlignment()
212 190 {
213 m_chart->legend()->setAlignment(Qt::AlignLeft);
214 }
191 QPushButton *button = qobject_cast<QPushButton *>(sender());
215 192
216 void MainWidget::setLegendRight()
217 {
193 switch (m_chart->legend()->alignment()) {
194 case Qt::AlignTop:
195 m_chart->legend()->setAlignment(Qt::AlignLeft);
196 if (button)
197 button->setText("Align (Left)");
198 break;
199 case Qt::AlignLeft:
200 m_chart->legend()->setAlignment(Qt::AlignBottom);
201 if (button)
202 button->setText("Align (Bottom)");
203 break;
204 case Qt::AlignBottom:
218 205 m_chart->legend()->setAlignment(Qt::AlignRight);
219 }
220
221 void MainWidget::setLegendTop()
222 {
206 if (button)
207 button->setText("Align (Right)");
208 break;
209 default:
210 if (button)
211 button->setText("Align (Top)");
223 212 m_chart->legend()->setAlignment(Qt::AlignTop);
213 break;
224 214 }
225
226 void MainWidget::setLegendBottom()
227 {
228 m_chart->legend()->setAlignment(Qt::AlignBottom);
229 215 }
230 216
231 217 void MainWidget::toggleBold()
@@ -46,15 +46,11 public:
46 46 signals:
47 47
48 48 public slots:
49 void detachLegend();
50 void attachLegend();
49 void toggleAttached();
51 50 void addBarset();
52 51 void removeBarset();
53 52
54 void setLegendLeft();
55 void setLegendRight();
56 void setLegendTop();
57 void setLegendBottom();
53 void setLegendAlignment();
58 54
59 55 void toggleBold();
60 56 void toggleItalic();
@@ -368,7 +368,7 void QLegend::detachFromChart()
368 368 void QLegend::attachToChart()
369 369 {
370 370 d_ptr->m_attachedToChart = true;
371 d_ptr->m_layout->invalidate();
371 d_ptr->m_presenter->layout()->invalidate();
372 372 setParent(d_ptr->m_chart);
373 373 }
374 374
General Comments 0
You need to be logged in to leave comments. Login now