优化一下这段代码: // 取出数据 QVector<double> qVector_X, qVector_Y; for (const auto& element : inferHeatDatas) { qVector_X.append(element.distance); qVector_Y.append(element.rock_type); } QCustomPlot* TypeCustom = new QCustomPlot(); TypeCustom = ui->widget_type_curve; TypeCustom->legend->setVisible(true); TypeCustom->legend->setFont(QFont("Helvetica", 9)); QPen pen; pen.setColor(QColor(qSin(4*1+1.2)*80+80, qSin(4*0.3+0)*80+80, qSin(4*0.3+1.5)*80+80)); TypeCustom->addGraph(); TypeCustom->graph()->setPen(pen); TypeCustom->graph()->setName("123"); TypeCustom->graph()->setLineStyle(QCPGraph::lsStepCenter); TypeCustom->graph()->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5)); TypeCustom->graph()->setData(qVector_X, qVector_Y); TypeCustom->graph()->rescaleAxes(true); TypeCustom->yAxis->scaleRange(1.1, TypeCustom->yAxis->range().center()); TypeCustom->xAxis->scaleRange(1.1, TypeCustom->xAxis->range().center()); TypeCustom->xAxis->setTicks(false); TypeCustom->yAxis->setTicks(true); TypeCustom->xAxis->setTickLabels(false); TypeCustom->yAxis->setTickLabels(true); TypeCustom->axisRect()->setupFullAxesBox(); TypeCustom->replot();
时间: 2024-04-18 07:24:03 浏览: 236
Qt-读写二进制文件(数据结构)
5星 · 资源好评率100%
这段代码可以进行以下优化:
1. 重复使用`ui->widget_type_curve`而不是创建新的`QCustomPlot`对象:
```cpp
QCustomPlot* TypeCustom = ui->widget_type_curve;
```
将`ui->widget_type_curve`赋值给`TypeCustom`,避免创建多余的对象。
2. 设置图例的字体和可见性:
```cpp
TypeCustom->legend->setFont(QFont("Helvetica", 9));
TypeCustom->legend->setVisible(true);
```
3. 使用链式调用来设置曲线图的属性:
```cpp
TypeCustom->addGraph()->setPen(pen)->setName("123")->setLineStyle(QCPGraph::lsStepCenter)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5))->setData(qVector_X, qVector_Y)->rescaleAxes(true);
```
将多个属性设置放在一行代码中,使用链式调用来设置。
4. 使用`scaleRange`一次性调整坐标轴的范围:
```cpp
TypeCustom->yAxis->scaleRange(1.1, TypeCustom->yAxis->range().center());
TypeCustom->xAxis->scaleRange(1.1, TypeCustom->xAxis->range().center());
```
5. 使用`setTicks`和`setTickLabels`一次性设置坐标轴刻度和标签的可见性:
```cpp
TypeCustom->xAxis->setTicks(false);
TypeCustom->yAxis->setTicks(true);
TypeCustom->xAxis->setTickLabels(false);
TypeCustom->yAxis->setTickLabels(true);
```
6. 调用`setupFullAxesBox`和`replot`来完成图形的设置和重绘:
```cpp
TypeCustom->axisRect()->setupFullAxesBox();
TypeCustom->replot();
```
这些优化可以使代码更简洁、可读性更高,并减少不必要的操作。最终的优化代码如下所示:
```cpp
QVector<double> qVector_X, qVector_Y;
for (const auto& element : inferHeatDatas)
{
qVector_X.append(element.distance);
qVector_Y.append(element.rock_type);
}
QCustomPlot* TypeCustom = ui->widget_type_curve;
TypeCustom->legend->setVisible(true);
TypeCustom->legend->setFont(QFont("Helvetica", 9));
QPen pen;
pen.setColor(QColor(qSin(4*1+1.2)*80+80, qSin(4*0.3+0)*80+80, qSin(4*0.3+1.5)*80+80));
TypeCustom->addGraph()->setPen(pen)->setName("123")->setLineStyle(QCPGraph::lsStepCenter)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 5))->setData(qVector_X, qVector_Y)->rescaleAxes(true);
TypeCustom->yAxis->scaleRange(1.1, TypeCustom->yAxis->range().center());
TypeCustom->xAxis->scaleRange(1.1, TypeCustom->xAxis->range().center());
TypeCustom->xAxis->setTicks(false);
TypeCustom->yAxis->setTicks(true);
TypeCustom->xAxis->setTickLabels(false);
TypeCustom->yAxis->setTickLabels(true);
TypeCustom->axisRect()->setupFullAxesBox();
TypeCustom->replot();
```
阅读全文