gpt4 book ai didi

qt - 更改轴刻度和标签以在 QWT 图中在毫米和英寸之间切换

转载 作者:行者123 更新时间:2023-12-04 04:43:10 25 4
gpt4 key购买 nike

我想要的是切换轴标签和刻度以匹配最好的显示英寸和毫米而不改变曲线点。我创建了一个自定义 QwtScaleDraw 并覆盖了 label() 方法,但这不会改变刻度。

class QwtMyScaleDraw : public QwtScaleDraw {
public:
QwtMyScaleDraw() : QwtScaleDraw() {
}
protected:
QwtText label(double value) const {
// global conversion routine that creates a display string either as millimeter or inch
return ConvertToString(value, ConversionFromMillimeter);
}
};

您可以在下图中看到这种行为:
Qwt plot scale demonstration

左边的比例是原始比例,中间的比例是我的自定义标签,右边是所需的比例。

在不使用手动转换的值再次添加整条曲线的情况下,获得所需结果的最佳方法是什么?

最佳答案

在 qwt 库作者的帮助下,我找到了解决方案。答案来自QwtLinearScaleEngine类和重载 autoScaledivideScale方法。

在我的实现中,可以选择任何转换因子(默认为 1 = 无转换)。这是我的代码:

class QwtUnitScaleDraw : public QwtScaleDraw {
public:
QwtUnitScaleDraw(double ConversionFactor = 1.0) : QwtScaleDraw(), _ConversionFactor(ConversionFactor) {
}
protected:
QwtText label(double value) const {
return QwtScaleDraw::label(value * _ConversionFactor);
}
protected:
double _ConversionFactor;
};

class QwtUnitScaleEngine: public QwtLinearScaleEngine {
public:
QwtUnitScaleEngine(double ConversionFactor = 1.0) : QwtLinearScaleEngine(10), _ConversionFactor(ConversionFactor) {
}

void autoScale(int maxSteps, double &x1, double &x2, double &stepSize) const {
x1 *= _ConversionFactor;
x2 *= _ConversionFactor;
stepSize *= _ConversionFactor;
QwtLinearScaleEngine::autoScale(maxSteps, x1, x2, stepSize);
x1 /= _ConversionFactor;
x2 /= _ConversionFactor;
stepSize /= _ConversionFactor;

}

QwtScaleDiv divideScale(double x1, double x2, int numMajorSteps, int numMinorSteps, double stepSize = 0.0) const {
x1 *= _ConversionFactor;
x2 *= _ConversionFactor;
stepSize *= _ConversionFactor;

QwtScaleDiv Div = QwtLinearScaleEngine::divideScale(x1, x2, numMajorSteps, numMinorSteps, stepSize);

QList<double> Ticks[QwtScaleDiv::NTickTypes];

Ticks[QwtScaleDiv::MajorTick] = Div.ticks(QwtScaleDiv::MajorTick);
for (unsigned int i = 0; i < Ticks[QwtScaleDiv::MajorTick].count(); i++) {
Ticks[QwtScaleDiv::MajorTick][i] /= _ConversionFactor;
}
Ticks[QwtScaleDiv::MediumTick] = Div.ticks(QwtScaleDiv::MediumTick);
for (unsigned int i = 0; i < Ticks[QwtScaleDiv::MediumTick].count(); i++) {
Ticks[QwtScaleDiv::MediumTick][i] /= _ConversionFactor;
}
Ticks[QwtScaleDiv::MinorTick] = Div.ticks(QwtScaleDiv::MinorTick);
for (unsigned int i = 0; i < Ticks[QwtScaleDiv::MinorTick].count(); i++) {
Ticks[QwtScaleDiv::MinorTick][i] /= _ConversionFactor;
}

return QwtScaleDiv(QwtInterval(x1 / _ConversionFactor, x2 / _ConversionFactor), Ticks);
}
protected:
double _ConversionFactor;
};

关于qt - 更改轴刻度和标签以在 QWT 图中在毫米和英寸之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18587571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com