目录
Qchart模块
1 使用Qchart在子窗口中画图
2 参考资料
3 代码
Qcustomplot模块
1 用Qcustomplot模块在子窗口中画图
2 参考资料
3 代码
Qchart模块
该部分可参考下面的博主
https://blog.csdn.net/baizy77/article/details/84107786
参考该资料,总结流程如下:
1 准备工作
需要在安装qt时带上了charts,否则后面工作无法开展。
对于编译方式安装的Qt,需要注意在configure时不要跳过charts。
对于安装包方式安装的Qt,需要注意在安装时,确保charts组件被选中。
2 修改pro文件
在pro中,使用如下语句包含charts库:
QT+=charts
3 提升widget控件为QChartView
在绘制ui窗体时,从designer的工具箱中选择一个“Widget”类型的控件,将objectName设置为"widget",然后在它上面单击鼠标右键,选择“提升为”。
在弹出的界面中,填写"提升的类名称"为: QChartView,头文件名称会自动生成,我们不用关心。
然后单击“添加”按钮即可。
如果在提升界面的上半部分已经有我们需要的类"QChartView",那么只需要选中它,然后单击"提升"按钮即可。
4 修改界面头文件(关键!)
在界面的头文件中,我们需要编写下面两行代码:
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
#include "ui_xxxxx.h"
请注意,两行加粗的代码必须写在#include "ui_xxxxx.h"的前面,因为"ui_xxxxx.h"头文件也需要用到这里的宏定义。(关键!)
这两句代码的作用是包含QChart所需的一批头文件,并声明QTChats的命名空间。
5 构建图表、构建系列
QChart* chart = new QChart();
这行代码很简单,我们构建一个QChart对象。
QLineSeries *series = new QLineSeries();
for (quint32 i = 0; i < 100; i++) {
series->append(i, sin(0.6f*i));
}
上面的代码构建了一个折线系列对象,并且对它进行初始化。方法是调用append()接口,传递的参数x、y对用的是一组坐标数据。就是折线上的一个点。
然后,我们将系列添加到图表,并创建默认的坐标轴。
chart->addSeries(series);
chart->createDefaultAxes(); // 基于已添加到图表的 series 来创建默认的坐标轴
6 将图表绑定到视图
widget->setChart(chart);
widget是前面第3步中我们提升的控件对象。
7 创建窗体并运行。
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
CDialog dlg(NULL);
dlg.exec();
return 0;
}
代码
RailWayMeasure.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-12-20T09:41:39
#
#-------------------------------------------------
QT += core gui
QT+=charts
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = RailMeasurement
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void PlotWidget();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->PlotWidget();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::PlotWidget()
{
QChart* chart = new QChart();
QLineSeries *series = new QLineSeries();
for (quint32 i = 0; i < 100; i++) {
series->append(i, sin(0.6f*i));
}
chart->addSeries(series);
chart->createDefaultAxes(); // 基于已添加到图表的 series 来创建默认的坐标轴
ui->widget_Plot->setChart(chart);
}
Qcustomplot模块
注意,除了添加qcustomplot.h以及qcustomplot.cpp外,一定要记得在.pro文件中添加printsupport字符串。参考资料是youtube上一个视频。
RailWayMeasure.pro
#-------------------------------------------------
#
# Project created by QtCreator 2019-12-20T09:41:39
#
#-------------------------------------------------
QT += core gui
QT+=charts
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
TARGET = RailMeasurement
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
qcustomplot.cpp
HEADERS += \
mainwindow.h \
qcustomplot.h
FORMS += \
mainwindow.ui
mainwindow.h
qcustomplot.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void PlotWidget();
void MyCustomPlot();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
qcustomplot.cpp
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include <QtCharts>
QT_CHARTS_USE_NAMESPACE
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->PlotWidget();
this->MyCustomPlot();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::PlotWidget()
{
QChart* chart = new QChart();
QLineSeries *series = new QLineSeries();
for (quint32 i = 0; i < 100; i++) {
series->append(i, sin(0.6f*i));
}
chart->addSeries(series);
chart->createDefaultAxes(); // 基于已添加到图表的 series 来创建默认的坐标轴
ui->widget_Plot->setChart(chart);
ui->widget_Plot->resize(500,500);
}
void MainWindow::MyCustomPlot()
{
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
ui->widget_customPlot->addGraph();
ui->widget_customPlot->graph(0)->setData(x, y);
// give the axes some labels:
ui->widget_customPlot->xAxis->setLabel("x");
ui->widget_customPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
ui->widget_customPlot->xAxis->setRange(-1, 1);
ui->widget_customPlot->yAxis->setRange(0, 1);
ui->widget_customPlot->replot();
}