QtoolTip自定义实现。
Callout.cpp
/****************************************************************************
**
** 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 by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "callout.h"
#include <QtGui/QPainter>
#include <QtGui/QFontMetrics>
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QMouseEvent>
#include <QtCharts/QChart>Callout::Callout(QChart *chart):QGraphicsItem(chart),m_chart(chart)
{
}QRectF Callout::boundingRect() const
{QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));QRectF rect;rect.setLeft(qMin(m_rect.left(), anchor.x()));rect.setRight(qMax(m_rect.right(), anchor.x()));rect.setTop(qMin(m_rect.top(), anchor.y()));rect.setBottom(qMax(m_rect.bottom(), anchor.y()));return rect;
}void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{Q_UNUSED(option)Q_UNUSED(widget)QPainterPath path;path.addRoundedRect(m_rect, 5, 5);QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));if (!m_rect.contains(anchor)) {QPointF point1, point2;// establish the position of the anchor point in relation to m_rectbool above = anchor.y() <= m_rect.top();bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y();bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom();bool below = anchor.y() > m_rect.bottom();bool onLeft = anchor.x() <= m_rect.left();bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x();bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right();bool onRight = anchor.x() > m_rect.right();// get the nearest m_rect corner.qreal x = (onRight + rightOfCenter) * m_rect.width();qreal y = (below + belowCenter) * m_rect.height();bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight);bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y);qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20);qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);;point1.setX(x1);point1.setY(y1);qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);;qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);;point2.setX(x2);point2.setY(y2);path.moveTo(point1);path.lineTo(anchor);path.lineTo(point2);path = path.simplified();}painter->setBrush(QColor(255, 255, 255));painter->drawPath(path);painter->drawText(m_textRect, m_text);
}void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
{event->setAccepted(true);
}void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{if (event->buttons() & Qt::LeftButton){setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton)));event->setAccepted(true);} else {event->setAccepted(false);}
}void Callout::setText(const QString &text)
{m_text = text;QFontMetrics metrics(m_font);m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text);m_textRect.translate(5, 5);prepareGeometryChange();m_rect = m_textRect.adjusted(-5, -5, 5, 5);
}void Callout::setAnchor(QPointF point)
{m_anchor = point;
}void Callout::updateGeometry()
{prepareGeometryChange();setPos(m_chart->mapToPosition(m_anchor) + QPoint(10, -50));
}
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 by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#ifndef CALLOUT_H
#define CALLOUT_H#include <QtCharts/QChartGlobal>
#include <QtWidgets/QGraphicsItem>
#include <QtGui/QFont>QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACEQT_CHARTS_BEGIN_NAMESPACE
class QChart;
QT_CHARTS_END_NAMESPACEQT_CHARTS_USE_NAMESPACEclass Callout : public QGraphicsItem
{
public:Callout(QChart *parent);void setText(const QString &text);void setAnchor(QPointF point);void updateGeometry();QRectF boundingRect() const;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);protected:void mousePressEvent(QGraphicsSceneMouseEvent *event);void mouseMoveEvent(QGraphicsSceneMouseEvent *event);private:QString m_text;QRectF m_textRect;QRectF m_rect;QPointF m_anchor;QFont m_font;QChart *m_chart;
};#endif // CALLOUT_H

如何使用该类:

QChart *m_chart=new QChart;

Callout m_tooltip = new Callout(m_chart);

m_tooltip->setText(QString("时间: %1 \n人数: %2 ").arg(strBuffer).arg(qRound(point.y())));//显示文本
m_tooltip->setAnchor(point);
m_tooltip->setZValue(11);
m_tooltip->updateGeometry();
m_tooltip->show();//显示
m_tooltip->hide();//隐藏
--------------------- 
作者:fsfsfsdfsdfdr 
来源:CSDN 
原文:https://blog.csdn.net/fsfsfsdfsdfdr/article/details/89757553 
版权声明:本文为博主原创文章,转载请附上博文链接!

Qt 官方例子 Callout Example相关推荐

  1. Qt 官方示例 | 这几个 QML 版的 Hello World 你学会了吗?

    .我是老吴,一枚光荣的嵌入式底层劳动人民. 作为一名 C++ 手残党的我,又来分享 Qt 的学习心得啦. 学习 Qt 的最佳途径是阅读官方的手册和示例, 今天要分享的是 Qt 官方提供的几个 Qt Q ...

  2. qwt官方例子-CurveDome

    qwt官方例子-CurveDome 从效果展示,元素分析,代码分析来逐步说明qwt的使用方式. 本教程需要结合qwt的Class List来学习. Qwt Class List: Qwt Class ...

  3. 【Netty】入门Netty官方例子解析(二)Time Server

    本文承接上文<[Netty]入门Netty官方例子解析(一)写个 Discard Server> ,接下来讲解官网文档中Netty入门官方例子第二个例子 Time Server 原文这个章 ...

  4. QT小例子GUI(主)线程与子线程之间的通信

    QT小例子GUI(主)线程与子线程之间的通信 在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. #ifndef TQT_H_ ...

  5. QT官方第三方开源工具

    QT官方第三方开源工具.今天再次在网上看到了QT的第三方开源工具列表,这里留下link地址,以备将来用到的时候参考使用. http://qt-project.org/wiki/Category:Add ...

  6. Mule 官方例子研究

    Mule 官方例子研究 一.编译导入Mule自带的例子 1.准备 安装Mule.这里就不介绍mule的安装了,请参考<Mule安装部署手册>. 2. 编译Mule自带例子中的Hello例子 ...

  7. Qt官方开发环境生成的exe发布方式--使用windeployqt

    2019独角兽企业重金招聘Python工程师标准>>> Qt 官方开发环境使用的动态链接库方式,在发布生成的exe程序时,需要复制一大堆 dll, 如果自己去复制dll,很可能丢三落 ...

  8. [FFmpeg] 编译官方例子

    官方例子地址 如果下载了源码,可在 ffmpeg-4.3.1/doc/examples/ 中查看 在Ubuntu 16.04 上,通过源码安装好 ffmpeg 后,运行官方例子 gcc -o demu ...

  9. 【TensorFlow】官方例子mnist_with_summaries.py在windows下运行tensorboard

    一.修改方案   TensorFlow官方例子mnist_with_summaries.py里面的主函数里面是按照linux的路径在写的,这在windows运行后生成的log文件路径会出现问题. 官方 ...

最新文章

  1. linux 调试利器gdb, strace, pstack, pstree, lsof
  2. 局域网伪造源地址DDoS***解决方法
  3. nginx php页面打开404,nginx php页面 error_page 404不起作用解决
  4. android AVD运行chrome,contentshell,chromeshell失败解决方法
  5. 使用线程自己join自己完成线程门栓
  6. 分布式版本控制系统Git的安装与使用(作业2)
  7. mysql8.0.22安装步骤图解_MySQL server 5.5的安装 步骤图解
  8. 中国工商银行的 Service Mesh 探索与实践
  9. day 61 pymysql
  10. Trapcode Particular 5 - Particle
  11. k9s加速k8s集群管理
  12. 用Excel做时间顺序的行为流程图
  13. 阿里云部署网站全流程(基于nodejs)
  14. 【程序员(媛)国人之光】知(美)识(色)贩卖贴】非标题党】
  15. 信安小组 第四周 总结
  16. Codeforces Round #476 (Div. 2) 题解
  17. NodeJS-框架express-Koa-Hapi的区别
  18. 加油站都需要什么手续_开一个加油站需要什么手续,需要多少启动资金?
  19. 不用iTunes也能添加音乐到iPod
  20. 科技云报道:2023,云计算的风向变了

热门文章

  1. win10强制关闭飞行模式_win10笔记本突然连不上wifi怎么办?
  2. 设置了position: fixed; 并且能够左右滚动 #html #Css
  3. 微信公众号开发之授权
  4. 生物特征识别技术的安全性分析
  5. 回溯法求解图着色问题
  6. 生日祝福卡片 html,暖心的卡片生日祝福语
  7. iOS和Android和H5交互WebViewJavascriptBridge
  8. 陶森大学计算机专业收入水平,2020PayScale计算机专业本科薪水排行
  9. python计算复数的辐角_python做傅里叶变换
  10. 如何安装最纯净的win7系统