赞
踩
目的:从此示例中了解QChart中曲线的绘制方法 和 插图的生成方法。本博文主要侧重于插图的生成方法。
本示例中有View和Callout两个类
添加两个曲线,series和series2,并各自关联了两个事件,鼠标的点击事件(对应KeepCallout)和悬停事件(对应tooltip)
KeepCallout中将Callout图元添加到Scene(场景)中,代码实现如下
- m_callouts.append(m_tooltip);
- m_tooltip = new Callout(m_chart);
tooltip中,若鼠标悬停在曲线上,则显示Callout图元;若不悬停在曲线上,则隐藏Callout图元。代码实现如下
- if (m_tooltip == 0)
- m_tooltip = new Callout(m_chart);
-
- if (state) {
- m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));
- m_tooltip->setAnchor(point);
- m_tooltip->setZValue(11);
- m_tooltip->updateGeometry();
- m_tooltip->show();
- } else {
- m_tooltip->hide();
- }
在paint方法中实现了不同位置Callout的链接方式,如下图所示
1. setDragMode主要用于设置鼠标的拖拽方式
- //没有任何反应,鼠标事件将被忽略
- setDragMode(QGraphicsView::NoDrag)
-
- //光标变为指向手,然后拖动鼠标将滚动滚动条,此模式在交互和非交互模式下均有效
- setDragMode(QGraphicsView::ScrollHandDrag)
-
- //将出现矩形块,拖动鼠标将设置矩形的大小,并选中矩形覆盖的所有项目,非交互视图禁用此模式
- setDragMode(QGraphicsView::RubberBandDrag)
2. 隐藏滚动条
- setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
- setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
3. 防止走样(反走样)
- //Qt默认关闭反走样,反走样比不反走样质量高
- setRenderHint(QPainter::Antialiasing,true);//默认为True
4. 鼠标追踪开始事件
- setMouseTracking(false);//鼠标追踪事件从按下左键后开始追踪
- setMouseTracking(true);//鼠标追踪开始
最后给出程序中的两个主要类:
Callout.h
- /****************************************************************************
- **
- ** Copyright (C) 2016 The Qt Company Ltd.
- ** Contact: https://www.qt.io/licensing/
- **
- ** This file is part of the Qt Charts module of the Qt Toolkit.
- **
- ** $QT_BEGIN_LICENSE:GPL$
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and The Qt Company. For licensing terms
- ** and conditions see https://www.qt.io/terms-conditions. For further
- ** information use the contact form at https://www.qt.io/contact-us.
- **
- ** GNU General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU
- ** General Public License version 3 or (at your option) any later version
- ** approved by the KDE Free Qt Foundation. The licenses are as published b
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。