Qt使用QCustomPlot开发

1、入门ide

一、下载源文件http://www.qcustomplot.com/函数

二、把.cpp和.h放在工程目录下,并将cpp和h加入工程;字体

三、在.pro中:QT += printsupport;ui

四、在ui中添加一个Widget,右键提高为,输入:QCustomPlot,改变对象名称为customPlot;this

五、加入代码:spa

void MainWindow::initUi().net

{3d

QVector<double> x(101), y(101); // initialize with entries 0..100code

for (int i=0; i<101; ++i)orm

{

x[i] = i/50.0 - 1; // x goes from -1 to 1

y[i] = x[i]*x[i]; // let's plot a quadratic function

}

ui->customPlot->addGraph();// 添加数据曲线(一个图像能够有多个数据曲线),graph(0);能够获取某个数据曲线(按添加前后排序);默认x1,y1轴

ui->customPlot->addGraph(ui->customPlot->xAxis,ui->customPlot->yAxis2);//添加的曲线以x1,y2为基准轴

ui->customPlot->graph(0)->setData(x, y);// setData();为数据曲线关联数据

ui->customPlot->xAxis->setLabel("x");// 为坐标轴添加标签

ui->customPlot->yAxis->setLabel("y");

ui->customPlot->xAxis->setRange(-1, 1);// 设置坐标轴的范围,以看到全部数据

ui->customPlot->yAxis->setRange(-1, 1);

ui->customPlot->replot();// 重画图像

//ui->customPlot->rescaleAxes();//自动设置最合适的显示范围

//ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);//可移动可拖放

//ui->customPlot->graph(0)->addData(double,double);//追加点

}

效果:

6:、添加QCustomPlot的帮助文档

在下载的源码包里有个qch文件,放在:D:\QT5.8\Docs\Qt-5.8里面,就能够使用帮助文档了

 

 

ps:

一、设置x坐标轴为时间

QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
ui.customPlot->xAxis->setTicker(timeTicker);
ui.customPlot->xAxis->setRange(-60 * 3.5, 60 * 11);
timeTicker->setTimeFormat("%m:%s");

double secs = QCPAxisTickerDateTime::dateTimeToKey(QDateTime::currentDateTime());//横坐标

 二、设置位置

setPositionAlignment(Qt::AlignTop

三、坐标轴刻度样式

 

使用QCPAxis::setTicker (QSharedPointer< QCPAxisTicker > )来定制刻度

 

四、设置刻度高度

setTickLength

五、设置坐标轴样式

setUpperEnding

 六、设置两个线条之间填充

ui->customplot->graph(0)->setBrush(Qt::cyan);

ui->customplot->graph(0)->setChannelFillGraph(ui->customplot_2_1->graph(1));

 

七、设置曲线形状

QCPGraph::setScatterStyle(QCPScatterStyle &style);  

八、坐标轴相关参数命名

九、坐标区相关距离命名

  9.一、设置整个cp坐标轴距离左侧的距离

  ui.customPlot->yAxis->setPadding(40);//距离左边的距离

十、设置刻度label旋转

ui.customPlot->xAxis->setTickLabelRotation(60);

 十一、设置坐标轴方向

ui.customPlot->xAxis->setRangeReversed(false);//x轴反向

 十二、获取当前x、y轴range的左右值

realLeft = ui.customPlot->xAxis->range().lower;
realRight = ui.customPlot->xAxis->range().upper;

1三、设置当前显示范围有多少个刻度

 ticker->setTickCount(n);

1四、设置长刻度步长

QCPAxisTickerFixed ticker->setTickStep(0.1);

1五、时间轴定制

QDateTime dateTime = QDateTime::currentDateTime();
double  now = dateTime.toTime_t();
QSharedPointer<QCPAxisTickerDateTime> yTicker(new QCPAxisTickerDateTime);
yTicker->setTickCount(2);
yTicker->setDateTimeFormat("yyyy.MM.dd-hh:mm");//
ui->customPlot->xAxis->setTicker(yTicker);
yTicker->setTickStepStrategy(QCPAxisTicker::tssMeetTickCount);
ui->customPlot->xAxis->setRange(now-3600,now+3600);//显示3个小时的数据

默认tickStep为1s,因此没有setTickStep函数进行设置

通常坐标轴定制须要两个参数:坐标轴显示范文【range】;此坐标轴有多少个刻度【setTickCount】

1六、QCPAbstractItem

 

 

2、高级

一、单级柱状图【只有一种颜色】

①、Bar声明

QCPBars *cpBar;

②、定义

cpBar = new QCPBars(ui.customPlot->xAxis, ui.customPlot->yAxis);

③、设置值

QVector<double> ticks;//定制值
ticks << 1 << 2 << 3 << 4;
QVector<double> yCount;

yCount<<2<<3<<3<<1;

cpBar->setData(ticks, yCount);
ui.customPlot->replot();

④、效果

 二、多级柱状图

先看效果【官网】

①、声明

QCPBars *max;
QCPBars *min;
QCPBars *sdDev;
QCPBars *varice;

②、定义

max = new QCPBars(ui.customPlot->xAxis, ui.customPlot->yAxis);
min = new QCPBars(ui.customPlot->xAxis, ui.customPlot->yAxis);
sdDev = new QCPBars(ui.customPlot->xAxis, ui.customPlot->yAxis);
varice = new QCPBars(ui.customPlot->xAxis, ui.customPlot->yAxis);

③、相关设置

max->setStackingGap(0);//设置上下bar之间的距离
min->setStackingGap(0);
sdDev->setStackingGap(0);
varice->setStackingGap(0);
max->setAntialiased(false); //提供更清晰、像素对齐的条形边框
min->setAntialiased(false);
sdDev->setAntialiased(false);
varice->setAntialiased(false);

sdDev->moveAbove(varice);//必需要写和这个,否则不会重叠
min->moveAbove(sdDev);//设置位置
max->moveAbove(min);

④、设置值

QVector<double> ticks;//定制值
ticks << 1 << 2 << 3 << 4;
QVector<double> yCount;

yCount<<2<<3<<3<<1;

max->setData(ticks, yCount);

min->setData(ticks, yCount);

varice->setData(ticks, yCount);

sdDev->setData(ticks, yCount);
ui.customPlot->replot();

 三、更好看的网格线

①、默认

 

②、定制

ui.customPlot->xAxis->grid()->setVisible(true);
ui.customPlot->xAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

ui.customPlot->yAxis->grid()->setSubGridVisible(true);
ui.customPlot->yAxis->grid()->setPen(QPen(QColor(130, 130, 130), 0, Qt::SolidLine));
ui.customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(130, 130, 130), 0, Qt::DotLine));

四、鼠标事件

头文件中:

void myMousePressEvent(QMouseEvent *event);
void myMouseReleaseEvent(QMouseEvent *event);

 绑定:

connect(ui.customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(myMousePressEvent(QMouseEvent*)));
connect(ui.customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(myMouseReleaseEvent(QMouseEvent*)));

实现:

void RoadAllData::myMousePressEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton)return;//若是不是鼠标左键按下则返回

int x_pos = event->pos().x();
int y_pos = event->pos().y();

// 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 (pixelToCoord 函数)
// coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
double x_val = ui.customPlot->xAxis->pixelToCoord(x_pos);
double y_val = ui.customPlot->yAxis->pixelToCoord(y_pos);

}

void RoadAllData::myMouseReleaseEvent(QMouseEvent *event)
{
  if (event->button() != Qt::LeftButton)return;
}

五、textitem的使用

①、声明

QCPItemText* itemText = NULL;

②、定义

   QCPItemText* itemText = new QCPItemText(ui->customplot_1_1);
    itemText->setPositionAlignment(Qt::AlignHCenter | Qt::AlignBottom);//以哪一个位置为标准
    itemText->position->setType(QCPItemPosition::ptAxisRectRatio);//以屏幕可见范围为参考
    itemText->position->setCoords(0.5, 0.95); //位置,从坐标矩形左上开始为(0,0),右下角为(1,1),x是向右,y是向下
    itemText->setFont(QFont(font().family(), 12)); //字体库,大小
    itemText->setTextAlignment(Qt::AlignLeft);
#if CLOSE_IF
    itemText->setPen(QPen(Qt::black)); //边框
#endif
    itemText->setText("增益降低0.225dB\n指向偏离0°\n波束展宽4.9954%\n副瓣抬高7.353dB");//
    itemText->setBrush(QBrush(QColor("#C4DDC3")));//背景色
    itemText->setPadding(QMargins(3, 3, 3, 3));

相关设置可参考:https://blog.csdn.net/umke888/article/details/54572647

③、设置值

itemText->setText("......"); 

 六、QSlider与显示range对应

**领导说,界面固定,用滑条来滑动。这都21世纪了,为何要用20世纪的东西,不过行,**知足你,/微笑

①、使用QSlider,按下、释放、获取值

    QObject::connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(sliderMovedSlot(int)));
    QObject::connect(ui.horizontalSlider, SIGNAL(sliderPressed()), this, SLOT(sliderPressSlot()));
    QObject::connect(ui.horizontalSlider, SIGNAL(sliderReleased()), this, SLOT(sliderReleaseSlot()));

②、经过设置range来显示Customplot当前位置

ui.customPlot->xAxis->setRange(realMax, realMax );
相关文章
相关标签/搜索