当前位置:   article > 正文

Qt示例解析 【Callout】_qt callout

qt callout

目的:从此示例中了解QChart中曲线的绘制方法 和 插图的生成方法。本博文主要侧重于插图的生成方法。

本示例中有View和Callout两个类

View类:继承自QGraphicsView

添加两个曲线,series和series2,并各自关联了两个事件,鼠标的点击事件(对应KeepCallout)和悬停事件(对应tooltip)

KeepCallout中将Callout图元添加到Scene(场景)中,代码实现如下

  1. m_callouts.append(m_tooltip);
  2. m_tooltip = new Callout(m_chart);

tooltip中,若鼠标悬停在曲线上,则显示Callout图元;若不悬停在曲线上,则隐藏Callout图元。代码实现如下

  1. if (m_tooltip == 0)
  2. m_tooltip = new Callout(m_chart);
  3. if (state) {
  4. m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));
  5. m_tooltip->setAnchor(point);
  6. m_tooltip->setZValue(11);
  7. m_tooltip->updateGeometry();
  8. m_tooltip->show();
  9. } else {
  10. m_tooltip->hide();
  11. }

Callout类:继承自QGraphicsItem

在paint方法中实现了不同位置Callout的链接方式,如下图所示

涉及到Qt QGraphicsView中的知识点:

1. setDragMode主要用于设置鼠标的拖拽方式

  1. //没有任何反应,鼠标事件将被忽略
  2. setDragMode(QGraphicsView::NoDrag)
  3. //光标变为指向手,然后拖动鼠标将滚动滚动条,此模式在交互和非交互模式下均有效
  4. setDragMode(QGraphicsView::ScrollHandDrag)
  5. //将出现矩形块,拖动鼠标将设置矩形的大小,并选中矩形覆盖的所有项目,非交互视图禁用此模式
  6. setDragMode(QGraphicsView::RubberBandDrag)

2. 隐藏滚动条

  1. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  2. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

3. 防止走样(反走样)

  1. //Qt默认关闭反走样,反走样比不反走样质量高
  2. setRenderHint(QPainter::Antialiasing,true);//默认为True

4. 鼠标追踪开始事件

  1. setMouseTracking(false);//鼠标追踪事件从按下左键后开始追踪
  2. setMouseTracking(true);//鼠标追踪开始

 

最后给出程序中的两个主要类:

Callout.h

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Charts module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published b
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/805449
推荐阅读
相关标签
  

闽ICP备14008679号