一、QCustomPlot的配置
下载地址:http://qcustomplot.com/index.php/download
只需下载QCustomPlot.tar.gz即可,里边包含:documentation、examples以及qcustomplot.h/cpp
运行环境:VS2010+qt-vs-addin-1.2.1-opensource
1.向项目中添加QCustomPlot.h和QCustomPlot.cpp文件
2.通过QtCreator打开ZLDS200.ui,添加一个widgets,右键点击"提升的窗口部件...",
3.报链接错误:“项目属性 -> 配置属性 -> 链接器 -> 输入 -> 附加依赖项”里面添加“Qt5PrintSupportd.lib”;
4.添加documentation到Assistant帮助中:打开QtCreator -> 工具 -> 帮助 -> 文档 -> 选择 qcustomplot.qch
二、绘制随机散布点
examples里有19个例子代码可供参考。
效果图:
Talk is cheap, show the code.
/**
\file ZLDS200.h
\author Jack.Li
*/
#ifndef ZLDS200_H
#define ZLDS200_H
#include "BaseDialog.h"
class ZLDS200 : public BaseDialog
{
Q_OBJECT
public:
ZLDS200(QWidget * parent = 0);
~ZLDS200();
void show();
void installSkin();
void initCustomPlot();
Q_SIGNALS:
void send_date(int, int);
private Q_SLOTS:
void slot_get_date(int, int);
void timerout();
private:
ZLDS200(ZLDS200 const &);
void operator=(ZLDS200 const &);
struct Private;
Private * const d;
};
#endif // ZLDS200_H
/**
\file ZLDS200.cpp
\author Jack.Li
*/
#include "ZLDS200.h"
#include "ui_ZLDS200.h"
#include "Application.h"
#include "resources/dark/DarkSkin.h"
#include
#include
#include
#include
#include
#include
inline void initZLDS200Skin()
{
Q_INIT_RESOURCE(DarkSkin);
}
struct ZLDS200::Private
{
Private(ZLDS200 * parent)
{}
///
Ui::ZLDS200 ui;
///
QTimer * timer;
QVector xAx, yAx;
};
ZLDS200::ZLDS200(QWidget * parent)
: BaseDialog(parent), d(new ZLDS200::Private(this))
{
d->ui.setupUi(this);
installSkin();
initCustomPlot();
connect(this, SIGNAL(send_date(double, double)), this, SLOT(slot_get_date(double, double)));
d->timer = new QTimer(this);
connect(d->timer, SIGNAL(timeout()), this, SLOT(timerout()));
d->timer->start(500);
}
ZLDS200::~ZLDS200()
{
delete d->timer;
delete d;
}
void ZLDS200::show()
{
QDialog::show();
}
void ZLDS200::installSkin()
{
initZLDS200Skin();
QString styleSheet;
QFile file(":/qss/darkskin.qss");
file.open(QFile::ReadOnly);
styleSheet = QLatin1String(file.readAll());
file.close();
setStyleSheet(styleSheet);
}
void ZLDS200::initCustomPlot()
{
// set title of plot:
d->ui.customPlot->plotLayout()->insertRow(0);
d->ui.customPlot->plotLayout()->addElement(0, 0, new QCPTextElement(d->ui.customPlot, "Points Graph"));
d->ui.customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
QCP::iSelectLegend | QCP::iSelectPlottables);
QBrush qBrush(QColor(233, 233, 233));
d->ui.customPlot->setBackground(qBrush);
d->ui.customPlot->xAxis->setLabel("x(location)");
d->ui.customPlot->yAxis->setLabel("y(height)");
d->ui.customPlot->legend->setVisible(true);
d->ui.customPlot->addGraph();
QPen pen;
//pen.setWidth(3);
pen.setColor(Qt::blue);// line color
d->ui.customPlot->graph(0)->setPen(pen);
d->ui.customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));
d->ui.customPlot->graph(0)->setLineStyle(QCPGraph::lsNone);
d->ui.customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));//set points size
d->ui.customPlot->graph(0)->setName("Device1");//set name
//set range
d->ui.customPlot->xAxis->setRange(0,120);
d->ui.customPlot->yAxis->setRange(0,50);
//connect to show tip
connect(d->ui.customPlot, &QCustomPlot::mouseMove, this, &ZLDS200::slot_mouseMoveTip);
}
void ZLDS200::timerout()
{
emit send_date((rand()%120), (rand()%50));
}
void ZLDS200::slot_get_date(double x, double y)
{
d->xAx.push_front(x);
d->yAx.push_front(y);
qDebug() << d->yAx.size();
if(d->xAx.size()>50) {
d->xAx.pop_back();
}
if(d->yAx.size()>50) {
d->yAx.pop_back();
}
d->ui.customPlot->graph(0)->setData(d->xAx, d->yAx);
d->ui.customPlot->replot();
}
void ZLDS200::slot_mouseMoveTip(QMouseEvent *event)
{
double xx = d->ui.customPlot->xAxis->pixelToCoord(event->x());
double yx = d->ui.customPlot->yAxis->pixelToCoord(event->y());
QString str,strToolTip;
QString qsPoint("X:%1,Y:%2");
qsPoint = qsPoint.arg(xx).arg(yx);
//showTip under the mouse
QToolTip::showText(cursor().pos(), qsPoint);
}