Qt使用QCustomPlot的實現(xiàn)示例
一、下載文件
進入官網(wǎng),選擇“Download”、QCustomPlot.tar.gz
Qt Plotting Widget QCustomPlot - Download
二、創(chuàng)建項目
創(chuàng)建一個"Qt Widget Application"項目,基類選擇“QMainWindow”,把剛才下載的壓縮包里的“qcustomplot.h”和“qcustomplot.cpp”拷貝到項目目錄下
右擊項目名稱,添加現(xiàn)有文件,選擇“qcustomplot.h”和“qcustomplot.cpp”
雙擊“mainwindow.ui”,往界面上拖拽一個Widget,并進行柵格布局
右擊“Widget”,選擇“提升為”
填寫類名稱“QCustomPlot”,點擊“添加”
點擊“提升”
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è)定右上角圖形標注可見 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軸坐標范圍 ui->widget->xAxis->setRange(-10, 100); //設(shè)置Y軸坐標范圍 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; }
四、運行測試
運行程序,界面顯示如下
到此這篇關(guān)于Qt使用QCustomPlot的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt使用QCustomPlot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vscode搭建遠程c開發(fā)環(huán)境的圖文教程
很久沒有寫C語言了,今天抽空學習下C語言知識,接下來通過本文給大家介紹Vscode搭建遠程c開發(fā)環(huán)境的詳細步驟,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細,需要的朋友參考下吧2021-11-11