visutal studio 2022使用qcustomplot基础教程
编译
下载,2.1.1版支持到Qt6.4 。
拷贝qcustomplot.h和qcustomplot.cpp到项目源目录(Qt project)。
在msvc中将它俩加入项目中。
使用Qt6.8,需要修改两处代码:
L6779
# if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
if (mDateTimeSpec == Qt::TimeZone)
return locale.toString(keyToDateTime(tick).toTimeZone(mTimeZone), mDateTimeFormat);
else if (mDateTimeSpec == Qt::UTC)
return locale.toString(keyToDateTime(tick).toUTC(), mDateTimeFormat);
else if(mDateTimeSpec == Qt::LocalTime)
return locale.toString(keyToDateTime(tick).toLocalTime(), mDateTimeFormat);
else {
return locale.toString(keyToDateTime(tick).toOffsetFromUtc(0), mDateTimeFormat);
}
# else
return locale.toString(keyToDateTime(tick).toTimeSpec(mDateTimeSpec), mDateTimeFormat);
# endif
}
toOffsetFromUtc是凑数的函数调用。
L6894
return date.startOfDay(QTimeZone("Asia/Shanghai")).toMSecsSinceEpoch() / 1000.0;
这里直接用了上海时区。
不改就会报错,qtcostumplt最近最新是2022年。
编辑UI,添加widget,然后做提升:
注:通过Vs开UI编辑,点保存后会自动进行转换。
添加printsupport
不指定qt库路径,会找不到Qt6PrintSupportd.lib,但是它又能找到qtcore它们。
代码
plot1.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_plot1.h"
class plot1 : public QMainWindow
{
Q_OBJECT
public:
plot1(QWidget *parent = nullptr);
~plot1();
private:
Ui::plot1Class ui;
};
plot1.cpp
#include "plot1.h"
plot1::plot1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
// generate some data:
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.plot->addGraph();
ui.plot->graph(0)->setData(x, y);
// give the axes some labels:
ui.plot->xAxis->setLabel("x轴");
ui.plot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
ui.plot->xAxis->setRange(-1, 1);
ui.plot->yAxis->setRange(0, 1);
ui.plot->replot();
}
plot1::~plot1()
{
}
用公式f(x)=x^2,生成100个数据,设定数据源,设定x,y范围,画出抛物线。