做團(tuán)購(gòu)網(wǎng)站的心得廣東今日最新疫情通報(bào)
1. 程序環(huán)境
- Qt6.5.1, mingw11.2
- mathgl 8.0.1: https://sourceforge.net/projects/mathgl/,推薦下載mathgl-8.0.LGPL-mingw.win64.7z,Windows環(huán)境嘗試自己編譯mathgl會(huì)缺失一些庫(kù),補(bǔ)充完整也可以自己編譯,路徑
"D:\mathgl-8.0.LGPL-mingw.win64\bin"
添加至系統(tǒng)環(huán)境變量
2. 程序要點(diǎn)
(1) mathgl默認(rèn)不支持Qt6,一個(gè)折中的辦法是繪圖后先生成svg圖像,然后再用Qt6加載文件進(jìn)行顯示,如果生成bmp、png等格式文件再用QLabel顯示圖片會(huì)虛化很嚴(yán)重,故采用svg的方法:
gr->WriteSVG("plot.svg");
(2) 使用QtSvgWidgets
渲染svg圖像,ui界面先拖入一個(gè)QWidget控件,然后將控件提升為QtSvgWidgets
,最后在代碼中實(shí)現(xiàn)加載svg圖像:
ui->widget_1->load(QString("plot.svg"));
ui->widget_1->show();
(3) .pro
文件添加下列內(nèi)容,添加svgwidgets模塊和mathgl庫(kù)路徑,注意動(dòng)態(tài)鏈接mathgl使用的是libmgl.dll.a
:
QT += core gui svg svgwidgets
INCLUDEPATH += D:/SciComputing/mathgl-8.0.LGPL-mingw.win64/include
LIBS += D:/SciComputing/mathgl-8.0.LGPL-mingw.win64/lib/libmgl.dll.a
(4)mathgl每次繪圖都需要重置一下,否則會(huì)收到之前繪圖的影響,很奇怪,明明不同繪圖對(duì)象已經(jīng)用new的方式創(chuàng)建了。
mglGraph *gr = new mglGraph();
gr->DefaultPlotParam(); // 重設(shè)
3.參考代碼
(1)mainwindows.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsView>
#include <QtSvgWidgets>
#include <QFile>
#include <QSvgRenderer>
#include <QPixmap>
#include <QImage>
#include <QGraphicsScene>
#include "mgl2/mgl.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_btn1_clicked()
{// mgl繪圖mglData dat(30,40); // data to for plottingfor(long i=0;i<30;i++) for(long j=0;j<40;j++)dat.a[i+30*j] = 1/(1+(i-15)*(i-15)/9.+(j-20)*(j-20)/16.);mglGraph *gr = new mglGraph(); // class for plot drawinggr->DefaultPlotParam();gr->Clf();gr->SetRanges(0