使用轻标记和点选择
注意:这是Charts with Widgets Gallery示例的一部分。
我们开始创建一个序列,用数据填充它,并设置选择功能。不要设置点可见性为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))); });
改变未选择点可见性有一个小差异。如前所述,使轻标记不可见是通过将它们设置为"empty" QImage
来实现的。这就是为什么,根据复选框的状态,选择点轻标记被设置为"empty" 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);
© 2024 Qt公司有限公司。包含在此的文档贡献的版权归其各自的业主所有。提供的文档是根据GNU自由文档许可版本1.3的条款许可的,由自由软件基金会出版。Qt以及相关的商标是芬兰的Qt公司及其它国家和地区的商标。所有其他商标均为相应业主的财产。