当前位置:   article > 正文

Qt 官方例子 Callout Example_qt callout

qt callout

 

  1. QtoolTip自定义实现。
  2. Callout.cpp
  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 by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/
  29. #include "callout.h"
  30. #include <QtGui/QPainter>
  31. #include <QtGui/QFontMetrics>
  32. #include <QtWidgets/QGraphicsSceneMouseEvent>
  33. #include <QtGui/QMouseEvent>
  34. #include <QtCharts/QChart>
  35. Callout::Callout(QChart *chart):
  36. QGraphicsItem(chart),
  37. m_chart(chart)
  38. {
  39. }
  40. QRectF Callout::boundingRect() const
  41. {
  42. QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
  43. QRectF rect;
  44. rect.setLeft(qMin(m_rect.left(), anchor.x()));
  45. rect.setRight(qMax(m_rect.right(), anchor.x()));
  46. rect.setTop(qMin(m_rect.top(), anchor.y()));
  47. rect.setBottom(qMax(m_rect.bottom(), anchor.y()));
  48. return rect;
  49. }
  50. void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  51. {
  52. Q_UNUSED(option)
  53. Q_UNUSED(widget)
  54. QPainterPath path;
  55. path.addRoundedRect(m_rect, 5, 5);
  56. QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
  57. if (!m_rect.contains(anchor)) {
  58. QPointF point1, point2;
  59. // establish the position of the anchor point in relation to m_rect
  60. bool above = anchor.y() <= m_rect.top();
  61. bool aboveCenter = anchor.y() > m_rect.top() && anchor.y() <= m_rect.center().y();
  62. bool belowCenter = anchor.y() > m_rect.center().y() && anchor.y() <= m_rect.bottom();
  63. bool below = anchor.y() > m_rect.bottom();
  64. bool onLeft = anchor.x() <= m_rect.left();
  65. bool leftOfCenter = anchor.x() > m_rect.left() && anchor.x() <= m_rect.center().x();
  66. bool rightOfCenter = anchor.x() > m_rect.center().x() && anchor.x() <= m_rect.right();
  67. bool onRight = anchor.x() > m_rect.right();
  68. // get the nearest m_rect corner.
  69. qreal x = (onRight + rightOfCenter) * m_rect.width();
  70. qreal y = (below + belowCenter) * m_rect.height();
  71. bool cornerCase = (above && onLeft) || (above && onRight) || (below && onLeft) || (below && onRight);
  72. bool vertical = qAbs(anchor.x() - x) > qAbs(anchor.y() - y);
  73. qreal x1 = x + leftOfCenter * 10 - rightOfCenter * 20 + cornerCase * !vertical * (onLeft * 10 - onRight * 20);
  74. qreal y1 = y + aboveCenter * 10 - belowCenter * 20 + cornerCase * vertical * (above * 10 - below * 20);;
  75. point1.setX(x1);
  76. point1.setY(y1);
  77. qreal x2 = x + leftOfCenter * 20 - rightOfCenter * 10 + cornerCase * !vertical * (onLeft * 20 - onRight * 10);;
  78. qreal y2 = y + aboveCenter * 20 - belowCenter * 10 + cornerCase * vertical * (above * 20 - below * 10);;
  79. point2.setX(x2);
  80. point2.setY(y2);
  81. path.moveTo(point1);
  82. path.lineTo(anchor);
  83. path.lineTo(point2);
  84. path = path.simplified();
  85. }
  86. painter->setBrush(QColor(255, 255, 255));
  87. painter->drawPath(path);
  88. painter->drawText(m_textRect, m_text);
  89. }
  90. void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
  91. {
  92. event->setAccepted(true);
  93. }
  94. void Callout::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
  95. {
  96. if (event->buttons() & Qt::LeftButton){
  97. setPos(mapToParent(event->pos() - event->buttonDownPos(Qt::LeftButton)));
  98. event->setAccepted(true);
  99. } else {
  100. event->setAccepted(false);
  101. }
  102. }
  103. void Callout::setText(const QString &text)
  104. {
  105. m_text = text;
  106. QFontMetrics metrics(m_font);
  107. m_textRect = metrics.boundingRect(QRect(0, 0, 150, 150), Qt::AlignLeft, m_text);
  108. m_textRect.translate(5, 5);
  109. prepareGeometryChange();
  110. m_rect = m_textRect.adjusted(-5, -5, 5, 5);
  111. }
  112. void Callout::setAnchor(QPointF point)
  113. {
  114. m_anchor = point;
  115. }
  116. void Callout::updateGeometry()
  117. {
  118. prepareGeometryChange();
  119. setPos(m_chart->mapToPosition(m_anchor) + QPoint(10, -50));
  120. }
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 by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/
  29. #ifndef CALLOUT_H
  30. #define CALLOUT_H
  31. #include <QtCharts/QChartGlobal>
  32. #include <QtWidgets/QGraphicsItem>
  33. #include <QtGui/QFont>
  34. QT_BEGIN_NAMESPACE
  35. class QGraphicsSceneMouseEvent;
  36. QT_END_NAMESPACE
  37. QT_CHARTS_BEGIN_NAMESPACE
  38. class QChart;
  39. QT_CHARTS_END_NAMESPACE
  40. QT_CHARTS_USE_NAMESPACE
  41. class Callout : public QGraphicsItem
  42. {
  43. public:
  44. Callout(QChart *parent);
  45. void setText(const QString &text);
  46. void setAnchor(QPointF point);
  47. void updateGeometry();
  48. QRectF boundingRect() const;
  49. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
  50. protected:
  51. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  52. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
  53. private:
  54. QString m_text;
  55. QRectF m_textRect;
  56. QRectF m_rect;
  57. QPointF m_anchor;
  58. QFont m_font;
  59. QChart *m_chart;
  60. };
  61. #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 
版权声明:本文为博主原创文章,转载请附上博文链接!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/805476
推荐阅读
相关标签
  

闽ICP备14008679号