QT基于QPolarChart封装一个极坐标系类(控件显示)

1、首先需要下载相应的QChart库

一般在安装QT时会选择此库,如若没有,请自行下载。


2、在工程文件中添加:QT += charts

3、新建一个极坐标系的封装类PolarChart:


4、头文件:

(1)导入库

(2)添加:QT_CHARTS_USE_NAMESPACE

(3)实例相关类: 


5、CPP文件:

(1)曲线设置

(2)角坐标系设置

(3)极坐标系设置

(4)极坐标系设置

 (5)利用QChartView控件显示:


5、完整代码:

(1)h文件

   其中void AddPoint(double Angular, double Radia)是数据接口函数。

#ifndef POLARCHART_H
#define POLARCHART_H

#include <QChart>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>
#include <QtCharts/QPolarChart>
#include <QChartView>

QT_CHARTS_USE_NAMESPACE

class PolarChart
{
public:
    PolarChart();

    //画布
    QChartView *chartView;
    //图表
    QtCharts::QPolarChart* chart;
    //曲线
    QtCharts::QLineSeries* lineSerises;

    //角度坐标轴
    QtCharts::QValueAxis* angularAxis;
    //弧长坐标轴
    QtCharts::QValueAxis *radialAxis;

    void AddPoint(double Angular, double Radia);

    //设置图表标题
    void setChartName(QString Name);
    //极坐标角度范围 默认0~360
    void setRangeAng(int A0, int A1);
    //极坐标弧长范围  默认0~200
    void setRangeRad(int R0, int R1);
    //视图显示大小  默认800*800
    void setResizeView(int wide, int heigh);
    //设置画点是否标记可见 默认不可见
    void setPointsVisible(bool ISVisible);

};

#endif // POLARCHART_H

 (2)cpp文件

#include "polarchart.h"

PolarChart::PolarChart()
{
    chart = new QtCharts::QPolarChart;
    chartView = new QChartView;
    lineSerises = new QtCharts::QLineSeries;

    angularAxis = new QtCharts::QValueAxis();
    radialAxis = new QtCharts::QValueAxis();

    lineSerises->setVisible(true);  //曲线可见
    lineSerises->setColor(QColor(0,100,255)); //曲线颜色设置
    lineSerises->setPointsVisible(false);  //曲线系列点标可见的设置
    chart->addSeries(lineSerises);  //加入表中

    //将legend(图例)隐藏,当然,也可以选择不隐藏
    chart->legend()->hide();
    chart->setVisible(true);
    chart->setBackgroundBrush(QColor(0,0,0));
    //角度坐标轴设置

    //第一个和最后一个间隔位于0/360度角
    angularAxis->setTickCount(9);
    angularAxis->setLabelFormat("%.1f");
    //设置角度坐标系的范围是0~360度
    angularAxis->setRange(0,360);
    //将角度坐标轴加入到图形中去
    chart->addAxis(angularAxis,QtCharts::QPolarChart::PolarOrientationAngular);

    //设置弧长的坐标系设置
    radialAxis->setTickCount(5);
    radialAxis->setLabelFormat("%d");
    radialAxis->setRange(0,200);
    //将弧长坐标系加入到图形中去
    chart->addAxis(radialAxis,QtCharts::QPolarChart::PolarOrientationRadial);

    //将曲线和坐标轴绑定起来
    lineSerises->attachAxis(angularAxis);
    lineSerises->attachAxis(radialAxis);

    chartView->setChart(chart);
    chartView->resize(800, 800);
    chartView->show();

}

void PolarChart::AddPoint(double Angular, double Radia)
{
    //向曲线容器lineSerises中添加数据
    lineSerises->append(Angular,Radia);
}

void PolarChart::setChartName(QString Name)
{
    chart->setTitleBrush(QBrush(QColor(0,0,255)));//设置标题Brush
    chart->setTitleFont(QFont("微软雅黑"));//设置标题字体
    chart->setTitle(Name);
}

void PolarChart::setRangeAng(int A0, int A1)
{
    angularAxis->setRange(A0,A1);
}

void PolarChart::setRangeRad(int R0, int R1)
{
    radialAxis->setRange(R0,R1);
}

void PolarChart::setResizeView(int wide, int heigh)
{
    chartView->resize(wide, heigh);
}

void PolarChart::setPointsVisible(bool ISVisible)
{
    lineSerises->setPointsVisible(ISVisible);
}

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt ,如果需要让其他访问一个的 UI 控件的内容,可以通过以下两种方式来实现: 1. 将 UI 控件的指针作为参数传递给其他的函数。在该函数,可以通过该指针来访问 UI 控件的属性和方法。 例如,在 A 一个 UI 控件,需要让 B 访问该控件的内容,可以将该控件的指针作为参数传递给 B 的函数: ```cpp // A 的代码 class A { public: A(QWidget* parent = nullptr) : QWidget(parent) { ui.setupUi(this); } QPushButton* getButton() const { return ui.pushButton; } private: Ui::AForm ui; }; // B 的代码 class B { public: void doSomething(A* a) { QPushButton* button = a->getButton(); // 在这里访问控件的属性和方法 // ... } }; ``` 2. 将 UI 控件的指针保存在一个公共,在需要访问该控件通过该公共来访问控件的内容。 例如,可以定义一个名为 `Global` 的公共,该保存了 UI 控件的指针: ```cpp // Global.h class Global { public: static void setButton(QPushButton* button) { m_button = button; } static QPushButton* getButton() { return m_button; } private: static QPushButton* m_button; }; // Global.cpp QPushButton* Global::m_button = nullptr; // A 的代码 class A { public: A(QWidget* parent = nullptr) : QWidget(parent) { ui.setupUi(this); Global::setButton(ui.pushButton); } private: Ui::AForm ui; }; // B 的代码 class B { public: void doSomething() { QPushButton* button = Global::getButton(); // 在这里访问控件的属性和方法 // ... } }; ``` 以上两种方法都可以实现在其他访问一个的 UI 控件的内容。需要注意的是,为了保证访问的正确性和安全性,需要在访问 UI 控件的时候进行判空操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值