目录
1.1 初始化配置保存文件对话框initSaveFileDialog()
1.2 保存图像工具栏按钮事件on_actionSaveImages_triggered()
二、清空图表on_actionClear_triggered()
三、暂停绘图on_actionPaused_triggered(bool checked)
四、放大选择区域on_actionSelect_triggered(bool checked)
5.2 鼠标移动事件slots_mouseMove(QMouseEvent* event)
5.3 坐标显示函数on_actionCoorTips_triggered(bool checked)
六、亮色背景切换on_actionLBG_triggered()
七、使用说名按钮打开网页on_actionHelpFile_triggered()
工程下载链接:温度助手下载链接
工具栏功能依次为:
保存图像、清空图标、暂停绘图、放大选择区域、坐标显示、亮色背景、使用说明
一、保存图像
1.1 初始化配置保存文件对话框initSaveFileDialog()
在temperassistant.h的public:手动声明
QFileDialog *initSaveFileDialog();//指针函数、文件类用于存储保存的图像
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::initSaveFileDialog 初始化配置保存文件对话框
* @return
*/
QFileDialog * TemperAssistant::initSaveFileDialog()
{
QFileDialog *fileDialog = new QFileDialog();
fileDialog->setOption(QFileDialog::DontUseNativeDialog,true); // 不使用本地的文件对话框
QLabel *lblWidth = new QLabel("Width",fileDialog); // 宽度
QSpinBox *spbWidth = new QSpinBox(fileDialog);
spbWidth->setMinimum(10);
spbWidth->setMaximum(16777215);
spbWidth->setButtonSymbols(QAbstractSpinBox::NoButtons);
spbWidth->setValue(ui->customPlot->width());
QLabel *lblHeight = new QLabel("Height",fileDialog);// 高度
QSpinBox *spbHeight = new QSpinBox(fileDialog);
spbHeight->setMinimum(10);
spbHeight->setMaximum(16777215);
spbHeight->setButtonSymbols(QAbstractSpinBox::NoButtons);
spbHeight->setValue(ui->customPlot->height());
QLabel *lblScale = new QLabel("Scale",fileDialog); // 缩放比例
QDoubleSpinBox *spbScale = new QDoubleSpinBox(fileDialog);
spbScale->setMinimum(0);
spbScale->setMaximum(16777215);
spbScale->setButtonSymbols(QAbstractSpinBox::NoButtons);
spbScale->setValue(1);
QHBoxLayout *hlayout = new QHBoxLayout(); // 新建布局图层
hlayout->addWidget(lblWidth); // 添加Widget
hlayout->addWidget(spbWidth);
hlayout->addWidget(lblHeight);
hlayout->addWidget(spbHeight);
hlayout->addWidget(lblScale);
hlayout->addWidget(spbScale);
hlayout->setSpacing(3);// 间隔为0
QWidget *pWidget = new QWidget(fileDialog);
QLabel *lblImagesSize = new QLabel("ImagesSize:",fileDialog); // 图像大小
fileDialog->layout()->addWidget(lblImagesSize);// 添加Widget,整体添加两个Widget, pWidget另外包含4个Widget
fileDialog->layout()->addWidget(pWidget); //
pWidget->setLayout(hlayout);
fileDialog->setWindowTitle(tr("保存图像")); // 对话框标题
fileDialog->setAcceptMode(QFileDialog::AcceptSave); // 保存模式
fileDialog->setViewMode(QFileDialog::List); // 列表显示
fileDialog->setLabelText(QFileDialog::Accept,"保存");// 按钮文本
fileDialog->setLabelText(QFileDialog::Reject,"取消");
fileDialog->setDirectory(".");
QString filters="png(*.png);;jpg(*.jpg);;bmp(*.bmp);;pdf(*.pdf)";// 过滤类型
fileDialog->setNameFilter(filters);
fileDialog->setDirectory(qApp->applicationDirPath());// 默认路径
return fileDialog;
}
1.2 保存图像工具栏按钮事件on_actionSaveImages_triggered()
在temperassistant.h的private slots:自动声明
void on_actionSaveImages_triggered();//工具栏保存图像
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::initSaveFileDialog 初始化配置保存文件对话框
* @return
*/
QFileDialog * TemperAssistant::initSaveFileDialog()
{
QFileDialog *fileDialog = new QFileDialog();
fileDialog->setOption(QFileDialog::DontUseNativeDialog,true); // 不使用本地的文件对话框
QLabel *lblWidth = new QLabel("Width",fileDialog); // 宽度
QSpinBox *spbWidth = new QSpinBox(fileDialog);
spbWidth->setMinimum(10);
spbWidth->setMaximum(16777215);
spbWidth->setButtonSymbols(QAbstractSpinBox::NoButtons);
spbWidth->setValue(ui->customPlot->width());
QLabel *lblHeight = new QLabel("Height",fileDialog);// 高度
QSpinBox *spbHeight = new QSpinBox(fileDialog);
spbHeight->setMinimum(10);
spbHeight->setMaximum(16777215);
spbHeight->setButtonSymbols(QAbstractSpinBox::NoButtons);
spbHeight->setValue(ui->customPlot->height());
QLabel *lblScale = new QLabel("Scale",fileDialog); // 缩放比例
QDoubleSpinBox *spbScale = new QDoubleSpinBox(fileDialog);
spbScale->setMinimum(0);
spbScale->setMaximum(16777215);
spbScale->setButtonSymbols(QAbstractSpinBox::NoButtons);
spbScale->setValue(1);
QHBoxLayout *hlayout = new QHBoxLayout(); // 新建布局图层
hlayout->addWidget(lblWidth); // 添加Widget
hlayout->addWidget(spbWidth);
hlayout->addWidget(lblHeight);
hlayout->addWidget(spbHeight);
hlayout->addWidget(lblScale);
hlayout->addWidget(spbScale);
hlayout->setSpacing(3);// 间隔为0
QWidget *pWidget = new QWidget(fileDialog);
QLabel *lblImagesSize = new QLabel("ImagesSize:",fileDialog); // 图像大小
fileDialog->layout()->addWidget(lblImagesSize);// 添加Widget,整体添加两个Widget, pWidget另外包含4个Widget
fileDialog->layout()->addWidget(pWidget); //
pWidget->setLayout(hlayout);
fileDialog->setWindowTitle(tr("保存图像")); // 对话框标题
fileDialog->setAcceptMode(QFileDialog::AcceptSave); // 保存模式
fileDialog->setViewMode(QFileDialog::List); // 列表显示
fileDialog->setLabelText(QFileDialog::Accept,"保存");// 按钮文本
fileDialog->setLabelText(QFileDialog::Reject,"取消");
fileDialog->setDirectory(".");
QString filters="png(*.png);;jpg(*.jpg);;bmp(*.bmp);;pdf(*.pdf)";// 过滤类型
fileDialog->setNameFilter(filters);
fileDialog->setDirectory(qApp->applicationDirPath());// 默认路径
return fileDialog;
}
二、清空图表on_actionClear_triggered()
在temperassistant.h的private slots:自动声明
void on_actionClear_triggered();//工具栏清空图表
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::on_actionClear_triggered 清空曲线
*/
void TemperAssistant::on_actionClear_triggered()
{
qint32 graphCounts = ui->customPlot->graphCount();
qint32 i = 0;
while( i < graphCounts)
{
ui->customPlot->graph(i++)->data()->clear();
}
posX = 0;
ui->customPlot->xAxis->setRange(0, 10, Qt::AlignCenter);
ui->customPlot->yAxis->setRange(0, 10, Qt::AlignCenter);
ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
三、暂停绘图on_actionPaused_triggered(bool checked)
在temperassistant.h的private slots:自动声明
void on_actionPaused_triggered(bool checked);//工具栏暂停绘图
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::on_actionPaused_triggered 暂停和继续绘图, 实际是停止定时器
* @param checked
*/
void TemperAssistant::on_actionPaused_triggered(bool checked)
{
if(checked)
{
if(timer.isActive())//如果计时器正在运行(挂起),则返回true;否则返回false。
{
timer.stop();
}
}
else
{
if(!timer.isActive())
{
timer.start();
}
}
}
四、放大选择区域on_actionSelect_triggered(bool checked)
在temperassistant.h的private slots:自动声明
void on_actionSelect_triggered(bool checked);//工具栏选择放大
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::on_actionSelect_triggered 选择放大
* @param checked
*/
void TemperAssistant::on_actionSelect_triggered(bool checked)
{
if(checked)
{
ui->customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);//模式:框选放大
}
else
{
ui->customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);//选择矩形被禁用
}
}
五、坐标显示
5.1 创建一个垂直线类指针
在temperassistant.h的private:手动声明
QCPItemStraightLine * lineV; // 垂直线
QList<QCPItemTracer *> tracerPointArray;//工具栏坐标显示鼠标跟踪的点
QList<QCPItemText *> lblGraph; //工具栏坐标显示显示曲线值
5.2 鼠标移动事件slots_mouseMove(QMouseEvent* event)
在temperassistant.h的public slots:手动声明
void slots_mouseMove(QMouseEvent* event);//工具栏坐标显示鼠标移动事件
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::slots_mouseMove 鼠标移动事件
* @param event
*/
void TemperAssistant::slots_mouseMove(QMouseEvent* event)
{
//获取鼠标坐标点
qint32 x_pos = event->pos().x();
// 把鼠标坐标点 转换为 QCustomPlot 内部坐标值 (pixelToCoord 函数)
// coordToPixel 函数与之相反 是把内部坐标值 转换为外部坐标点
qint32 x_val = qRound(ui->customPlot->xAxis->pixelToCoord(x_pos));
// 然后打印在界面上
lineV->point1->setCoords(x_val, 0);
lineV->point2->setCoords(x_val, 2);
double posAtGraph=0;
qint32 i=0;
foreach( auto tracer, tracerPointArray )
{
/* 跟随点 */
auto iter = ui->customPlot->graph(i)->data()->findBegin(x_val+1);
posAtGraph = iter->mainValue();
tracer->position->setCoords( x_val, posAtGraph);
/* 坐标说明 */
auto label = lblGraph.at(i);
label->position->setCoords( 5, 0);
label->setText(tr("X:%1\nY:%2").arg(x_val).arg(posAtGraph));
i++;
}
ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
5.3 坐标显示函数on_actionCoorTips_triggered(bool checked)
在temperassistant.h的private slots:自动声明
void on_actionCoorTips_triggered(bool checked);//工具栏坐标显示
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::on_actionCoorTips_triggered 坐标显示
* @param checked
*/
void TemperAssistant::on_actionCoorTips_triggered(bool checked)
{
if(graphsCount == 0) return;
if(checked)
{
QPen linesPen(Qt::yellow, 1, Qt::DashLine);
lineV = new QCPItemStraightLine(ui->customPlot);//垂直线
lineV->setLayer("overlay");
lineV->setPen(linesPen);
lineV->setClipToAxisRect(true);
lineV->setVisible(true);
for(qint32 i=0; i<graphsCount;i++)
{
QCPItemTracer *tracer = new QCPItemTracer(ui->customPlot);
tracer->setStyle(QCPItemTracer::tsCircle);
tracer->setPen(QColor(Qt::red));
tracer->setBrush(Qt::NoBrush);
tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
tracer->setSize(6);
tracer->setVisible(true);
tracerPointArray<<tracer;
QCPItemText *label = new QCPItemText(ui->customPlot);
// label->setLayer("overlay");
label->setClipToAxisRect(false);
label->setPadding(QMargins(6, 6, 6, 6));
label->setBrush(QColor(20,20,20,200)); // 颜色填充,半透明
// label->setPen(); // 边框颜色
label->position->setParentAnchor(tracer->position);
label->setColor(ui->customPlot->graph(i)->pen().color()); // 字体颜色与曲线颜色一致
label->setText("test");
label->setPositionAlignment(Qt::AlignLeft | Qt::AlignTop);
label->setVisible(true);
lblGraph<<label;
}
connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(slots_mouseMove(QMouseEvent*)));
}
else
{
ui->customPlot->disconnect(SIGNAL(mouseMove(QMouseEvent*)));
lineV->setVisible(false);
ui->customPlot->removeItem(lineV);
foreach( QCPItemText *label, lblGraph )
{
label->setVisible(false);
ui->customPlot->removeItem(label);
}
foreach( QCPItemTracer *tracer, tracerPointArray )
{
tracer->setVisible(false);
ui->customPlot->removeItem(tracer);
}
lblGraph.clear();
tracerPointArray.clear();
ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
}
六、亮色背景切换on_actionLBG_triggered()
在temperassistant.h的private slots:自动声明
void on_actionLBG_triggered();//工具栏切换显示图表背景色
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::on_actionLBG_triggered 切换绘图背景色
*/
void TemperAssistant::on_actionLBG_triggered()
{
static bool lbg = true;
if( lbg ){
// 高亮背景
// set some pens, brushes and backgrounds:
ui->customPlot->xAxis->setBasePen(QPen(Qt::black, 1));
ui->customPlot->yAxis->setBasePen(QPen(Qt::black, 1));
ui->customPlot->xAxis->setTickPen(QPen(Qt::black, 1));
ui->customPlot->yAxis->setTickPen(QPen(Qt::black, 1));
ui->customPlot->xAxis->setSubTickPen(QPen(Qt::black, 1));
ui->customPlot->yAxis->setSubTickPen(QPen(Qt::black, 1));
ui->customPlot->xAxis->setTickLabelColor(Qt::black);
ui->customPlot->yAxis->setTickLabelColor(Qt::black);
ui->customPlot->xAxis->grid()->setPen(QPen(QColor(115, 115, 115), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setPen(QPen(QColor(115, 115, 115), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(175, 175, 175), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(175, 175, 175), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->grid()->setSubGridVisible(true);
ui->customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
ui->customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
QLinearGradient plotGradient;
plotGradient.setStart(0, 0);
plotGradient.setFinalStop(0, 350);
plotGradient.setColorAt(1, QColor(255, 255, 255));//1无渐变色
plotGradient.setColorAt(1, QColor(255, 255, 255));
ui->customPlot->setBackground(plotGradient);
QLinearGradient axisRectGradient;
axisRectGradient.setStart(0, 0);
axisRectGradient.setFinalStop(0, 350);
axisRectGradient.setColorAt(1, QColor(255, 255, 255));//1无渐变色
axisRectGradient.setColorAt(1, QColor(255, 255, 255));
ui->customPlot->axisRect()->setBackground(axisRectGradient);
ui->customPlot->legend->setTextColor(QColor(0, 0 , 0, 150)); // legend 字体颜色,白色
lbg = false;
}else{
// 深色背景
// set some pens, brushes and backgrounds:
ui->customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setBasePen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1));
ui->customPlot->xAxis->setTickLabelColor(Qt::white);
ui->customPlot->yAxis->setTickLabelColor(Qt::white);
ui->customPlot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
ui->customPlot->xAxis->grid()->setSubGridVisible(true);
ui->customPlot->yAxis->grid()->setSubGridVisible(true);
ui->customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
ui->customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
ui->customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
QLinearGradient plotGradient;
plotGradient.setStart(0, 0);
plotGradient.setFinalStop(0, 350);
plotGradient.setColorAt(1, QColor(80, 80, 80));//无渐变色
plotGradient.setColorAt(1, QColor(50, 50, 50));
ui->customPlot->setBackground(plotGradient);
QLinearGradient axisRectGradient;
axisRectGradient.setStart(0, 0);
axisRectGradient.setFinalStop(0, 350);
axisRectGradient.setColorAt(1, QColor(80, 80, 80));//无渐变色
axisRectGradient.setColorAt(1, QColor(30, 30, 30));
ui->customPlot->axisRect()->setBackground(axisRectGradient);
ui->customPlot->legend->setTextColor(QColor(255, 255, 255, 150)); // legend 字体颜色,白色
lbg = true;
}
ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
七、使用说名按钮打开网页on_actionHelpFile_triggered()
在temperassistant.h添加头文件:
#include <QDesktopServices>/*打开浏览器网页*/
#include <QUrl>/*打开浏览器网页*/
在temperassistant.h的public:用于打开浏览器类声明
QDesktopServices desktopServices;//打开网页浏览器类
在temperassistant.h的private slots:自动声明
void on_actionHelpFile_triggered();//工具栏帮助按钮打开浏览器
在temperassistant.c中写入代码。
/**
* @brief TemperAssistant::on_actionHelpFile_triggered() 帮助按钮打开网页
*/
void TemperAssistant::on_actionHelpFile_triggered()
{
QDesktopServices::openUrl(QUrl(QString("https://www.baidu.com")));
}