Qt Example Callout Extention(about QChart/QGraphicsView/QGraphicsItem)

2024-08-25 04:52

本文主要是介绍Qt Example Callout Extention(about QChart/QGraphicsView/QGraphicsItem),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题

Qt Example callout 展示了在平面直角坐标系中画tips。知识点涉及到QChart/QGraphicsView/QGraphicsItem。如何在平面直角坐标系中画点、折线、圆、长方形?

Example路径

D:\Qt\5.15.2\Src\qtcharts\examples\charts\callout\callout.cpp

代码

main

#include <QtWidgets/QApplication>
#include "view.h"int main(int argc, char *argv[])
{QApplication a(argc, argv);View w;w.show();return a.exec();
}

View 

#ifndef VIEW_H
#define VIEW_H
#include <QtWidgets/QGraphicsView>
#include <QtCharts/QChartGlobal>QT_BEGIN_NAMESPACE
class QGraphicsScene;
class QMouseEvent;
class QResizeEvent;
QT_END_NAMESPACEQT_CHARTS_BEGIN_NAMESPACE
class QChart;
QT_CHARTS_END_NAMESPACEclass Callout;QT_CHARTS_USE_NAMESPACE
class CircleItem;
class RectItem;
class View: public QGraphicsView
{Q_OBJECTpublic:View(QWidget *parent = 0);void DelCallout(Callout* pCallout);
protected:void resizeEvent(QResizeEvent *event);void mouseMoveEvent(QMouseEvent *event);public slots:void keepCallout();void tooltip(QPointF point, bool state);private:QGraphicsSimpleTextItem *m_coordX;QGraphicsSimpleTextItem *m_coordY;QChart *m_chart;Callout *m_tooltip;QList<Callout*> m_lCallout;QList<CircleItem*>m_lCircle;QList<RectItem*>m_lRect;
};#endif
#include "view.h"
#include <QtGui/QResizeEvent>
#include <QtWidgets/QGraphicsScene>
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtWidgets/QGraphicsTextItem>
#include "callout.h"
#include <QtGui/QMouseEvent>
#include <QValueAxis>
#include <QScatterSeries>
#include <QAreaSeries>
#include <QSplineSeries>
#include "CircleItem.h"
#include "RectItem.h"
View::View(QWidget *parent): QGraphicsView(new QGraphicsScene, parent),m_coordX(0),m_coordY(0),m_chart(0),m_tooltip(0)
{setDragMode(QGraphicsView::NoDrag);setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);// chartm_chart = new QChart;m_chart->setMinimumSize(640, 480);m_chart->setTitle(QStringLiteral("平面直角坐标系"));m_chart->legend()->hide();//点的数据源QList<QPointF> lptSource;lptSource << QPointF(0, 6.3) << QPointF(2, 6) << QPointF(3, 6) << QPointF(6, 6) << QPointF(7, 7) << QPointF(8, 8)<< QPointF(4, 3) << QPointF(4, 7) << QPointF(5, 2);//点用折线连起来QLineSeries* line = new QLineSeries();line->append(lptSource);//点用曲线连起来QSplineSeries* spline = new QSplineSeries();spline->append(lptSource);//点用圆圈表示QScatterSeries* scatter = new QScatterSeries();scatter->append(lptSource);//区域QLineSeries* line1 = new QLineSeries();QLineSeries* line2 = new QLineSeries();line1->append(1, 1);line1->append(1, 2);line2->append(2, 1);line2->append(2, 2);QAreaSeries* area = new QAreaSeries(line1, line2);area->setName("Batman");QPen pen(0x059605);pen.setWidth(3);area->setPen(pen);QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));gradient.setColorAt(0.0, 0x3cc63c);gradient.setColorAt(1.0, 0x26f626);gradient.setCoordinateMode(QGradient::ObjectBoundingMode);area->setBrush(gradient);m_chart->addSeries(scatter);m_chart->addSeries(line);//m_chart->addSeries(spline);m_chart->addSeries(area);m_chart->createDefaultAxes();m_chart->setAcceptHoverEvents(true);setRenderHint(QPainter::Antialiasing);scene()->addItem(m_chart);m_coordX = new QGraphicsSimpleTextItem(m_chart);m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height());m_coordX->setText("X: ");m_coordY = new QGraphicsSimpleTextItem(m_chart);m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height());m_coordY->setText("Y: ");connect(line, &QLineSeries::clicked, this, &View::keepCallout);connect(line, &QLineSeries::hovered, this, &View::tooltip);connect(spline, &QSplineSeries::clicked, this, &View::keepCallout);connect(spline, &QSplineSeries::hovered, this, &View::tooltip);connect(area, &QAreaSeries::clicked, this, &View::keepCallout);connect(area, &QAreaSeries::hovered, this, &View::tooltip);this->setMouseTracking(true);CircleItem* pCircle = new CircleItem(m_chart, this);pCircle->SetCenterR(QPointF(4,4.5),1);m_lCircle.push_back(pCircle);RectItem* pRect = new RectItem(m_chart, this);pRect->SetRect(QRectF(QPointF(5.6,2.2),QSizeF(2,3)));m_lRect.push_back(pRect);
}void View::DelCallout(Callout* pCallout)
{m_lCallout.removeOne(pCallout);delete pCallout;
}void View::resizeEvent(QResizeEvent *event)
{if (scene()) {scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));m_chart->resize(event->size());m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height() - 20);m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height() - 20);const auto callouts = m_lCallout;for (Callout *callout : callouts)callout->updateGeometry();for (CircleItem* pCircle : m_lCircle)pCircle->updateGeometry();for (RectItem* pRect : m_lRect)pRect->updateGeometry();}QGraphicsView::resizeEvent(event);
}void View::mouseMoveEvent(QMouseEvent *event)
{m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x()));m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y()));QGraphicsView::mouseMoveEvent(event);
}void View::keepCallout()
{m_lCallout.append(m_tooltip);m_tooltip = new Callout(m_chart, this);
}void View::tooltip(QPointF point, bool state)
{if (m_tooltip == 0)m_tooltip = new Callout(m_chart, this);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();}
}

callout 

#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_NAMESPACE
class View;
class Callout : public QGraphicsItem
{
public:Callout(QChart *parent, View* pView);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);void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event);
private:QString m_text;QRectF m_textRect;QRectF m_rect;QPointF m_anchor;QFont m_font;QChart *m_chart;View* m_view;
};#endif // CALLOUT_H
#include "callout.h"
#include <QtGui/QPainter>
#include <QtGui/QFontMetrics>
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QMouseEvent>
#include <QtCharts/QChart>
#include <QDebug>
#include <view.h>
Callout::Callout(QChart *chart, View* pView):QGraphicsItem(chart),m_chart(chart),m_view(pView)
{
}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.isNull() && !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();qDebug() << point1 << "," << anchor << "," << point2 << "," << path<<"\n\n\n\n";}painter->setBrush(QColor(255, 255, 255,128));painter->drawPath(path);painter->drawText(m_textRect, m_text);
}void Callout::mousePressEvent(QGraphicsSceneMouseEvent *event)
{QPointF pt = event->pos();QRectF rc = mapToParent(m_rect).boundingRect();if (m_rect.contains(pt)){event->setAccepted(true);}elseevent->setAccepted(false);
}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::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{m_view->DelCallout(this);
}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));
}

CircleItem

#pragma once
#include <QtCharts/QChartGlobal>
#include <QtWidgets/QGraphicsItem>
QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACEQT_CHARTS_BEGIN_NAMESPACE
class QChart;
QT_CHARTS_END_NAMESPACEQT_CHARTS_USE_NAMESPACE
class View;
class CircleItem :public QGraphicsItem
{
public:CircleItem(QChart* parent, View* pView);void SetCenterR(QPointF pt,double r);//pt为在坐标系上点的坐标,r为在坐标系上的半径长度void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);void updateGeometry();QRectF boundingRect() const;
private:QPointF m_pt,m_ptDraw;double m_r,m_rDrawW,m_rDrawH;QChart* m_chart;View* m_view;
};
#include "CircleItem.h"
#include "View.h"
#include <QChart>
CircleItem::CircleItem(QChart* parent, View* pView):QGraphicsItem(parent),m_chart(parent),m_view(pView)
{}void CircleItem::SetCenterR(QPointF pt, double r)
{m_pt = pt;m_r = r;m_ptDraw = mapFromParent(m_chart->mapToPosition(m_pt));QPointF ptTemp0 = mapFromParent(m_chart->mapToPosition(QPointF(0, 0)));QPointF ptTempW = mapFromParent(m_chart->mapToPosition(QPointF(m_r, 0)));QPointF ptTempH = mapFromParent(m_chart->mapToPosition(QPointF(0, m_r)));m_rDrawW = abs(ptTempW.x() - ptTemp0.x());m_rDrawH = abs(ptTempH.y() - ptTemp0.y());
}void CircleItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{QPainterPath path;QPointF pt = mapFromParent(m_chart->mapToPosition(m_pt));path.addEllipse(pt,m_rDrawW,m_rDrawH);painter->setBrush(QColor(255, 255, 0, 128));painter->drawPath(path);
}void CircleItem::updateGeometry()
{prepareGeometryChange();m_ptDraw = mapFromParent(m_chart->mapToPosition(m_pt));QPointF ptTemp0 = mapFromParent(m_chart->mapToPosition(QPointF(0, 0)));QPointF ptTempW = mapFromParent(m_chart->mapToPosition(QPointF(m_r, 0)));QPointF ptTempH = mapFromParent(m_chart->mapToPosition(QPointF(0, m_r)));m_rDrawW = abs(ptTempW.x() - ptTemp0.x());m_rDrawH = abs(ptTempH.y() - ptTemp0.y());setPos(m_ptDraw+QPointF(-m_rDrawW, -m_rDrawH));
}QRectF CircleItem::boundingRect() const
{QPointF pt = mapFromParent(m_chart->mapToPosition(m_pt));QRectF rect;rect.setLeft(pt.x() - m_rDrawW);rect.setRight(pt.x() + m_rDrawW);rect.setTop(pt.y() - m_rDrawH);rect.setBottom(pt.y() + m_rDrawH);return rect;
}

RectItem 

#pragma once
#include <QtCharts/QChartGlobal>
#include <QtWidgets/QGraphicsItem>
QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACEQT_CHARTS_BEGIN_NAMESPACE
class QChart;
QT_CHARTS_END_NAMESPACEQT_CHARTS_USE_NAMESPACE
class View;
class RectItem : public QGraphicsItem
{
public:RectItem(QChart* parent, View* pView);void SetRect(QRectF rc);void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);void updateGeometry();QRectF boundingRect() const;
private:QRectF m_rc,m_rcDraw;QChart* m_chart;View* m_view;
};
#include "RectItem.h"
#include "View.h"
#include <QChart>
RectItem::RectItem(QChart* parent, View* pView) :QGraphicsItem(parent),
m_chart(parent), m_view(pView)
{}void RectItem::SetRect(QRectF rc)
{m_rc = rc;m_rcDraw.setTopLeft(mapFromParent(m_chart->mapToPosition(m_rc.topLeft())));m_rcDraw.setBottomRight(mapFromParent(m_chart->mapToPosition(m_rc.bottomRight())));
}void RectItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{QPainterPath path;m_rcDraw.setTopLeft(mapFromParent(m_chart->mapToPosition(m_rc.topLeft())));m_rcDraw.setBottomRight(mapFromParent(m_chart->mapToPosition(m_rc.bottomRight())));path.addRect(m_rcDraw);painter->setBrush(QColor(255, 128, 0, 128));painter->drawPath(path);
}void RectItem::updateGeometry()
{prepareGeometryChange();m_rcDraw.setTopLeft(mapFromParent(m_chart->mapToPosition(m_rc.topLeft())));m_rcDraw.setBottomRight(mapFromParent(m_chart->mapToPosition(m_rc.bottomRight())));setPos(m_rcDraw.topLeft());
}QRectF RectItem::boundingRect() const
{QPointF ptTopLeft = mapFromParent(m_chart->mapToPosition(m_rc.topLeft()));QPointF ptBottomRight = mapFromParent(m_chart->mapToPosition(m_rc.bottomRight()));return QRectF(ptTopLeft,ptBottomRight);
}

运行效果

这篇关于Qt Example Callout Extention(about QChart/QGraphicsView/QGraphicsItem)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104596

相关文章

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设

【QT】基础入门学习

文章目录 浅析Qt应用程序的主函数使用qDebug()函数常用快捷键Qt 编码风格信号槽连接模型实现方案 信号和槽的工作机制Qt对象树机制 浅析Qt应用程序的主函数 #include "mywindow.h"#include <QApplication>// 程序的入口int main(int argc, char *argv[]){// argc是命令行参数个数,argv是

Python QT实现A-star寻路算法

目录 1、界面使用方法 2、注意事项 3、补充说明 用Qt5搭建一个图形化测试寻路算法的测试环境。 1、界面使用方法 设定起点: 鼠标左键双击,设定红色的起点。左键双击设定起点,用红色标记。 设定终点: 鼠标右键双击,设定蓝色的终点。右键双击设定终点,用蓝色标记。 设置障碍点: 鼠标左键或者右键按着不放,拖动可以设置黑色的障碍点。按住左键或右键并拖动,设置一系列黑色障碍点

使用Qt编程QtNetwork无法使用

使用 VS 构建 Qt 项目时 QtNetwork 无法使用的问题 - 摘叶飞镖 - 博客园 (cnblogs.com) 另外,强烈建议在使用QNetworkAccessManager之前看看这篇文章: Qt 之 QNetworkAccessManager踏坑记录-CSDN博客 C++ Qt开发:QNetworkAccessManager网络接口组件 阅读目录 1.1 通用API函数

Qt多语种开发教程

Qt作为跨平台的开发工具,早已应用到各行各业的软件开发中。 今天讲讲,Qt开发的正序怎么做多语言开发。就是说,你设置中文,就中文显示;设置英语就英文显示,设置繁体就繁体显示,设置发育就显示法语等。 开发环境(其实多语种这块根环境没太大关系):win10,Qt.5.12.10 一.先用QtCreator创建一个简单的桌面程序 1.工程就随便命名“LanguageTest”,其他默认。 2.在设计师

Qt中window frame的影响

window frame 在创建图形化界面的时候,会创建窗口主体,上面会多出一条,周围多次一圈细边,这就叫window frame窗口框架,这是操作系统自带的。 这个对geometry的一些属性有一定影响,主要体现在Qt坐标系体系: 窗口当中包含一个按钮,这个按钮的坐标系是以父元素为参考,那么这个参考是widget本体作为参考,还是window frame作为参考,这两种参考体系都存在

【Qt】定时器事件

定时器事件 在之前学习QTimer中实现了定时器的功能,而在QTimer背后是QTimerEvent定时器事件进行支撑的。在QObject中提供了一个timeEvent这个函数。 startTimer启动定时器killTimer关闭定时器 Qt 中在进⾏窗⼝程序的处理过程中,经常要周期性的执⾏某些操作,或者制作⼀些动画效果,使⽤定 时器就可以实现。所谓定时器就是在间隔⼀定时间后,去执⾏某⼀

QT 编译报错:C3861: ‘tr‘ identifier not found

问题: QT 编译报错:C3861: ‘tr’ identifier not found 原因 使用tr的地方所在的类没有继承自 QObject 类 或者在不在某一类中, 解决方案 就直接用类名引用 :QObject::tr( )

在 Qt Creator 中,输入 /** 并按下Enter可以自动生成 Doxygen 风格的注释

在 Qt Creator 中,当你输入 /** 时,确实会自动补全标准的 Doxygen 风格注释。这是因为 Qt Creator 支持 Doxygen 以及类似的文档注释风格,并且提供了代码自动补全功能。 以下是如何在 Qt Creator 中使用和显示这些注释标记的步骤: 1. 自动补全 Doxygen 风格注释 在 Qt Creator 中,你可以这样操作: 在你的代码中,将光标放在

Qt: 详细理解delete与deleteLater (避免访问悬空指针导致程序异常终止)

前言 珍爱生命,远离悬空指针。 正文 delete 立即删除:调用 delete 后,对象会立即被销毁,其内存会立即被释放。调用顺序:对象的析构函数会被立即调用,销毁该对象及其子对象。无事件处理:如果在对象销毁过程中还涉及到信号和槽、事件处理等,直接 delete 可能会导致问题,尤其是在对象正在处理事件时。适用场景:适用于在确定对象已经不再被使用的情况下,并且不涉及异步处理或事件循环中的