라이트 마커 및 포인트 선택 사용
참고: 이 예제는 위젯이 있는 차트 갤러리 예제의 일부입니다.
먼저 시리즈를 만들고, 데이터로 채우고, 선택 기능을 설정합니다. 포인트 가시성을 true
로 설정하지 않는 것이 중요합니다. 라이트 마커 기능은 독립적인 기능이며 둘 다 설정하면 원치 않는 동작이 발생할 수 있기 때문입니다.
constexpr qreal marker_size = 20.; auto series = new QSplineSeries; series->append({QPointF(0., 0.), QPointF(0.5, 2.27), QPointF(1.5, 2.2), QPointF(3.3, 1.7), QPointF(4.23, 3.1), QPointF(5.3, 2.3), QPointF(6.47, 4.1) }); series->setMarkerSize(marker_size); series->setLightMarker(rectangle(marker_size, Qt::red)); series->setSelectedLightMarker(blueTriangle(marker_size)); QObject::connect(series, &QXYSeries::clicked, series, [=](const QPointF &point) { int index = series->points().indexOf(point); if (index != -1) series->toggleSelection({index}); });
그런 다음 QChart
, QChartview
및 사용자 지정 요소를 정렬하는 레이아웃이 있는 제어 위젯을 만듭니다.
auto chart = new QChart; chart->addSeries(series); chart->createDefaultAxes(); chart->legend()->setVisible(false); chart->layout()->setContentsMargins(0, 0, 0, 0); chart->setTitle("Select points with mouse click"); auto chartView = new QChartView(chart, this); chartView->setRenderHint(QPainter::Antialiasing); auto controlWidget = new QWidget(this); auto controlLayout = new QGridLayout(controlWidget);
다음 단계는 사용자 지정 요소를 만드는 것입니다.
auto charPointCombobox = new QComboBox(controlWidget); auto charPointSelectedCombobox = new QComboBox(controlWidget); auto lineColorCombobox = new QComboBox(controlWidget); auto showUnselectedPointsCheckbox = new QCheckBox(controlWidget);
마커 선택 콤보박스의 레이블을 만들고 항목으로 콤보박스 채우기를 추가합니다. 또한 콤보박스에 기능을 제공합니다. 라이트 마커 기능의 구현을 고려할 때 선택되지 않은 포인트의 표시가 선택되어 있는지 확인해야 합니다. 라이트 마커의 표시 여부를 끄려면 "empty"
QImage
으로 설정하면 됩니다. 사용자가 선택되지 않은 포인트 표시를 선택 해제하고 라이트 마커 이미지를 변경하면 선택되지 않은 포인트가 보이지 않게 됩니다. 확인을 수행하지 않으면 라이트 마커에 QImage
이 새로 설정되고 선택되지 않은 포인트는 꺼져 있어도 표시됩니다.
auto charPoint = new QLabel(tr("Char point: "), controlWidget); charPointCombobox->addItems({tr("Red rectangle"), tr("Green triangle"), tr("Orange circle") }); QObject::connect(charPointCombobox, &QComboBox::currentIndexChanged, series, [=](const int index) { if (showUnselectedPointsCheckbox->isChecked()) series->setLightMarker(getPointRepresentation(PointType(index), marker_size)); });
선택한 점 라이트 마커 및 선 색상에도 거의 동일한 절차가 적용됩니다. 유일한 차이점은 기능에 영향을 주지 않으므로 선택하지 않은 포인트의 가시성을 확인할 필요가 없다는 것입니다.
auto charPointSelected = new QLabel(tr("Char point selected: "), controlWidget); charPointSelectedCombobox->addItems({tr("Blue triangle"), tr("Yellow rectangle"), tr("Lavender circle") }); QObject::connect(charPointSelectedCombobox, &QComboBox::currentIndexChanged, series, [=](const int index) { series->setSelectedLightMarker(getSelectedPointRepresentation(SelectedPointType(index), marker_size)); }); auto lineColorLabel = new QLabel(tr("Line color: "), controlWidget); lineColorCombobox->addItems({tr("Blue"), tr("Black"), tr("Mint") }); QObject::connect(lineColorCombobox, &QComboBox::currentIndexChanged, series, [=](const int index) { series->setColor(makeLineColor(LineColor(index))); });
선택되지 않은 포인트의 가시성을 변경하는 데는 약간의 차이가 있습니다. 앞서 언급했듯이 라이트 마커를 보이지 않게 하려면 "비어 있음" QImage
으로 설정하면 됩니다. 따라서 체크박스 상태에 따라 선택한 포인트 라이트 마커가 "비어 있음" QImage
또는 해당 콤보박스의 현재 인덱스에서 추출한 라이트 마커로 설정됩니다.
auto showUnselectedPointsLabel = new QLabel(tr("Display unselected points: "), controlWidget); showUnselectedPointsCheckbox->setChecked(true); QObject::connect(showUnselectedPointsCheckbox, &QCheckBox::stateChanged, series, [=](const int state) { if (state) series->setLightMarker(getPointRepresentation(PointType(charPointCombobox->currentIndex()), marker_size)); else series->setLightMarker(QImage()); });
마지막 부분은 요소를 정렬하고 모든 위젯을 기본 위젯에 추가하고 기본 창 크기를 설정하는 것입니다.
controlLayout->addWidget(charPoint, 0, 0); controlLayout->addWidget(charPointCombobox, 0, 1); controlLayout->addWidget(charPointSelected, 1, 0); controlLayout->addWidget(charPointSelectedCombobox, 1, 1); controlLayout->addWidget(lineColorLabel, 2, 0); controlLayout->addWidget(lineColorCombobox, 2, 1); controlLayout->addWidget(showUnselectedPointsLabel, 3, 0); controlLayout->addWidget(showUnselectedPointsCheckbox, 3, 1, 1, 2); m_mainWidget = new QWidget(this); auto mainLayout = new QHBoxLayout(m_mainWidget); mainLayout->addWidget(chartView); mainLayout->addWidget(controlWidget);
© 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.