Qt使用QCustomPlot的實(shí)現(xiàn)示例
一、下載文件
進(jìn)入官網(wǎng),選擇“Download”、QCustomPlot.tar.gz
Qt Plotting Widget QCustomPlot - Download

二、創(chuàng)建項(xiàng)目
創(chuàng)建一個"Qt Widget Application"項(xiàng)目,基類選擇“QMainWindow”,把剛才下載的壓縮包里的“qcustomplot.h”和“qcustomplot.cpp”拷貝到項(xiàng)目目錄下

右擊項(xiàng)目名稱,添加現(xiàn)有文件,選擇“qcustomplot.h”和“qcustomplot.cpp”


雙擊“mainwindow.ui”,往界面上拖拽一個Widget,并進(jìn)行柵格布局

右擊“Widget”,選擇“提升為”

填寫類名稱“QCustomPlot”,點(diǎn)擊“添加”

點(diǎn)擊“提升”

Widget的基類被更改

三、修改代碼
在.pro文件中添加:QT += printsupport
#-------------------------------------------------
#
# Project created by QtCreator 2023-10-04T14:16:44
#
#-------------------------------------------------
QT += core gui
QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyCustomPlot
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp \
qcustomplot.cpp
HEADERS += \
mainwindow.h \
qcustomplot.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target更改mainwindow.cpp代碼入下
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//可移動縮放
ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom );
//設(shè)置背景顏色
ui->widget->setBackground(QColor(25,35,45));
//軸刻度文字
ui->widget->xAxis->setTickLabelColor(Qt::white);
ui->widget->yAxis->setTickLabelColor(Qt::white);
//設(shè)定右上角圖形標(biāo)注可見
ui->widget->legend->setVisible(true);
ui->widget->legend->setBrush(QColor(25,35,45));
ui->widget->legend->setTextColor(Qt::white);
ui->widget->legend->setFont(QFont("Helvetica", 9));
//設(shè)置X軸坐標(biāo)范圍
ui->widget->xAxis->setRange(-10, 100);
//設(shè)置Y軸坐標(biāo)范圍
ui->widget->yAxis->setRange(-150, 150);
ui->widget->addGraph();
ui->widget->graph(0)->setName("通道1");
ui->widget->graph(0)->setPen(QPen(QColor(178,34,34)));
//傳入數(shù)據(jù) QVector<double>類型
QVector<double> xData;
QVector<double> yData;
for(int i = 0; i < 100; i++)
{
xData.append(i);
yData.append(150 * sin(i));
}
ui->widget->graph(0)->setData(xData, yData);
}
MainWindow::~MainWindow()
{
delete ui;
}四、運(yùn)行測試
運(yùn)行程序,界面顯示如下

到此這篇關(guān)于Qt使用QCustomPlot的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt使用QCustomPlot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
成員初始化列表與構(gòu)造函數(shù)體中的區(qū)別詳細(xì)解析
無論是在構(gòu)造函數(shù)初始化列表中初始化成員,還是在構(gòu)造函數(shù)體中對它們賦值,最終結(jié)果是相同的。不同之處在于,使用構(gòu)造函數(shù)初始化列表的版本初始化數(shù)據(jù)成員,沒有定義初始化列表的構(gòu)造函數(shù)版本在構(gòu)造函數(shù)體中對數(shù)據(jù)成員賦值2013-09-09
C++數(shù)據(jù)結(jié)構(gòu)鏈表基本操作示例過程
這篇文章主要為大家介紹了C++數(shù)據(jù)結(jié)構(gòu)鏈表基本操作的示例過程有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11
C++詳細(xì)分析lambda表達(dá)式的本質(zhì)
Lambda表達(dá)式是現(xiàn)代C++在C ++ 11和更高版本中的一個新的語法糖 ,在C++11、C++14、C++17和C++20中Lambda表達(dá)的內(nèi)容還在不斷更新。 lambda表達(dá)式(也稱為lambda函數(shù))是在調(diào)用或作為函數(shù)參數(shù)傳遞的位置處定義匿名函數(shù)對象的便捷方法2022-06-06
Qt中利用QTextBrowser控件設(shè)計(jì)日志窗口
本文主要介紹了Qt中利用QTextBrowser控件設(shè)計(jì)日志窗口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06

