天天看點

Qt之QCustomPlot繪制随機散布點

一、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,右鍵點選"提升的視窗部件...",

Qt之QCustomPlot繪制随機散布點

3.報連結錯誤:“項目屬性 -> 配置屬性 -> 連結器 -> 輸入 -> 附加依賴項”裡面添加“Qt5PrintSupportd.lib”;

4.添加documentation到Assistant幫助中:打開QtCreator -> 工具 -> 幫助 -> 文檔 -> 選擇 qcustomplot.qch

Qt之QCustomPlot繪制随機散布點

二、繪制随機散布點

examples裡有19個例子代碼可供參考。

效果圖:

Qt之QCustomPlot繪制随機散布點

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); } 
         
        
       
      
     
    
   
           

繼續閱讀