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

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

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


雙擊“mainwindow.ui”,往界面上拖拽一個(gè)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);
//可移動(dòng)縮放
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)注可見(jiàn)
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)行測(cè)試
運(yùn)行程序,界面顯示如下

到此這篇關(guān)于Qt使用QCustomPlot的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt使用QCustomPlot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vscode搭建遠(yuǎn)程c開(kāi)發(fā)環(huán)境的圖文教程
很久沒(méi)有寫C語(yǔ)言了,今天抽空學(xué)習(xí)下C語(yǔ)言知識(shí),接下來(lái)通過(guò)本文給大家介紹Vscode搭建遠(yuǎn)程c開(kāi)發(fā)環(huán)境的詳細(xì)步驟,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11
C語(yǔ)言猜兇手及類似題目的實(shí)現(xiàn)示例
本文主要介紹了C語(yǔ)言猜兇手及類似題目的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C語(yǔ)言連續(xù)子向量的最大和及時(shí)間度量實(shí)例
這篇文章主要介紹了C語(yǔ)言連續(xù)子向量的最大和及時(shí)間度量,需要的朋友可以參考下2014-09-09
C語(yǔ)言字符串轉(zhuǎn)換為Python字符串的方法
這篇文章主要介紹了C語(yǔ)言字符串轉(zhuǎn)換為Python字符串的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

