범례 분리 및 붙이기
참고: 이 예는 위젯이 있는 차트 갤러리 예제의 일부입니다.
기본적으로 차트는 차트가 포함된 동일한 그래픽 보기 내에 범례를 그립니다. 경우에 따라 사용자가 범례를 다른 곳에 그리기를 원할 수 있습니다. 이를 가능하게 하기 위해 범례를 차트에서 분리할 수 있습니다. 분리한다는 것은 범례가 더 이상 차트의 가장자리에 고정되지 않으며 차트가 더 이상 범례의 레이아웃을 제어하지 않는다는 것을 의미합니다. 대신 범례 지오메트리를 사용자가 제어하여 범례의 위치와 크기를 자유롭게 제어할 수 있습니다. 범례는 차트 콘텐츠 위에 띄울 수도 있고 다른 그래픽 장면에 그릴 수도 있습니다. 이 예제를 실행하여 범례의 동작을 테스트할 수 있습니다.
이 예에서는 막대 시리즈를 보여주며, 막대 세트를 대화형으로 추가하거나 제거할 수 있습니다. 범례는 막대 세트가 추가 및/또는 제거될 때 계열의 변경 사항을 반영합니다. 범례의 부착, 정렬, 이동 및 크기 조정 기능은 QLegend
의 메서드를 사용하여 제어할 수 있습니다.
범례가 분리되면 크기를 조정하고 위치를 자유롭게 조정할 수 있습니다. 범례의 대화형 기능이 활성화된 경우 사용자가 범례를 자유롭게 드래그하여 크기를 조정할 수 있습니다. 사용자가 차트의 어느 쪽에서든 범례를 끌어서 떼어내면 자동으로 해당 쪽에 다시 붙습니다.
여기서는 범례를 표시하고 차트 하단으로 정렬을 설정합니다.
m_chart->legend()->setVisible(true); m_chart->legend()->setAlignment(Qt::AlignBottom);
이 코드 조각은 차트에서 범례를 분리하는 방법을 보여줍니다.
legend->detachFromChart();
여기서는 범례를 차트에 다시 첨부합니다. 배경은 보이지 않게 설정합니다.
legend->attachToChart();
여기에서는 범례를 분리하거나 부착할 때 발생하는 신호에 연결하여 분리하면 배경이 켜지고 부착하면 배경이 꺼집니다. 이렇게 하면 범례가 분리되었을 때 범례 내부의 항목이 어떻게 배열되는지 더 쉽게 확인할 수 있습니다.
connect(legend, &QLegend::attachedToChartChanged, [legend, this](bool attachedToChart) { m_toggleAttachedButton->setChecked(attachedToChart); legend->setBackgroundVisible(!attachedToChart); m_geometrySettings->setDisabled(attachedToChart); });
마우스와 같은 포인팅 장치를 사용하여 범례를 사용자에게 인터랙티브하게 만드는 방법을 보여줍니다.
legend->setInteractive(true);
범례가 대화형 범례가 아닌 경우 분리된 범례 치수를 설정하는 방법을 보여줍니다. 새 값을 설정한 후 업데이트를 호출하여 변경 사항을 화면에 표시합니다.
QRectF geom = m_chart->legend()->geometry(); if (qFuzzyCompare(geom.x(), m_legendPosX->value()) && qFuzzyCompare(geom.y(), m_legendPosY->value()) && qFuzzyCompare(geom.width(), m_legendWidth->value()) && qFuzzyCompare(geom.height(), m_legendHeight->value())) return; m_chart->legend()->setGeometry(QRectF(m_legendPosX->value(), m_legendPosY->value(), m_legendWidth->value(), m_legendHeight->value())); m_chart->legend()->update();
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.