网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
void on_customplot_selectionChangedByUser();
void on_act_zoomIn_toggled(bool arg1);
void on_act_move_toggled(bool arg1);
void on_act_select_toggled(bool arg1);
void contextMenuRequest(QPoint pos);
void rescaleAxes();
构造函数里:
//右键菜单自定义
ui->customplot->setContextMenuPolicy(Qt::CustomContextMenu);
//信号连接槽函数
connect(ui->customplot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint)));
QCustomPlot* customPlot=ui->customplot;
//四边显示坐标轴
customPlot->axisRect()->setupFullAxesBox();
// 生成数据:
int n=100;
QVector<double> x(n), y1(n),y2(n),y3(n); //
for (int i=0; i<n; i+=2)
{
x[i] = i; //
y1[i] = qSin(i/(double)n*M_PI*2); //
y2[i] = qCos(i/(double)n*M_PI*2); //
y3[i] = (i<=50)? i/(double)50*2-1 : -(i-50)/(double)50*2+1;
}
// 创建3个graph
customPlot->addGraph();
customPlot->graph(0)->setData(x, y1);
customPlot->addGraph();
customPlot->graph(1)->setData(x, y2);
customPlot->addGraph();
customPlot->graph(2)->setData(x, y3);
//曲线颜色
customPlot->graph(0)->setPen(QPen(Qt::green,2));
customPlot->graph(1)->setPen(QPen(Qt::red,2));
customPlot->graph(2)->setPen(QPen(Qt::gray,2));
//连接方式
customPlot->graph(0)->setLineStyle((QCPGraph::LineStyle::lsImpulse));//脉冲线
//customPlot->graph(1)->setLineStyle((QCPGraph::LineStyle::lsStepCenter));//阶梯线,左对齐
// customPlot->graph(2)->setLineStyle((QCPGraph::LineStyle::lsStepLeft));//阶梯线,左对齐
//不显示连线
//customPlot->graph(0)->setLineStyle(QCPGraph::LineStyle::lsNone);
customPlot->graph(1)->setLineStyle(QCPGraph::LineStyle::lsNone);
customPlot->graph(2)->setLineStyle(QCPGraph::LineStyle::lsNone);
//数据点显示图案
customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssDisc,8));
customPlot->graph(1)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssCross,8));
customPlot->graph(2)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ScatterShape::ssTriangle,8));
//自动调整范围
customPlot->graph(0)->rescaleAxes();
customPlot->graph(1)->rescaleAxes(true);
customPlot->graph(2)->rescaleAxes(true);
//数据多选
customPlot->graph(0)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
customPlot->graph(1)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
customPlot->graph(2)->setSelectable(QCP::SelectionType::stMultipleDataRanges);
//选择框模式:无
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
//选框黑色虚线
customPlot->selectionRect()->setPen(QPen(Qt::black,1,Qt::DashLine));
customPlot->selectionRect()->setBrush(QBrush(QColor(0,0,100,50)));
//修改多选按键,默认Ctrl
//customPlot->setMultiSelectModifier(Qt::KeyboardModifier::ControlModifier);
//滚动缩放、图表可选、多选
customPlot->setInteractions(QCP::iRangeZoom | QCP::iSelectPlottables| QCP::iMultiSelect);//
customPlot->replot();
放大、拖动、选择action槽函数:
//放大action
void MainWindow::on_act_zoomIn_toggled(bool arg1)
{
QCustomPlot* customPlot=ui->customplot;
if(arg1)
{
ui->act_move->setChecked(false);//取消拖动选项
customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动
ui->act_select->setChecked(false);//取消选择
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
}
else
{
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
}
}
//拖动action
void MainWindow::on_act_move_toggled(bool arg1)
{
QCustomPlot* customPlot=ui->customplot;
if(arg1)
{
ui->act_zoomIn->setChecked(false);//取消放大
ui->act_select->setChecked(false);//取消选择
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
customPlot->setInteraction(QCP::iRangeDrag,true);//使能拖动
}
else
{
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动
}
}
//选择action
void MainWindow::on_act_select_toggled(bool arg1)
{
QCustomPlot* customPlot=ui->customplot;
if(arg1)
{
ui->act_zoomIn->setChecked(false);//取消放大
ui->act_move->setChecked(false);//取消拖动选项
customPlot->setInteraction(QCP::iRangeDrag,false);//取消拖动
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmSelect);
}
else
{
customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);
}
}
选择变化槽函数:
//选择的数据变化
void MainWindow::on_customplot_selectionChangedByUser()
{
QCustomPlot* customPlot=ui->customplot;
//清空listwidget
ui->lst_data->clear();
for(int i=0;i<customPlot->graphCount();i++)
{
//遍历有被选中的graph
if(customPlot->graph(i)->selected())
{
QCPDataSelection selection =customPlot->graph(i)->selection();
//遍历选中范围
for(int j=0;j<selection.dataRangeCount();j++)
{
QCPDataRange dataRange = selection.dataRange(j);
//遍历数据
for(int k=dataRange.begin();k<dataRange.end();k++)
{
QString str_key = QString::number(customPlot->graph(i)->data()->at(k)->key);
QString str_value = QString::number(customPlot->graph(i)->data()->at(k)->value);
QString str_at= QString::number(i);
//添加到listwidget
ui->lst_data->addItem("曲线"+str_at+":"+str_key+", "+str_value);
}
}
}
}
//滚动到底部
ui->lst_data->scrollToBottom();
}
四、下载
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新