问题描述
当Qwt多个Plot并排存在时,如果存在刻度范围不一致时,会导致坐标轴不对齐问题,看起来会很不舒服,网上方法只找到一个,感觉不太好。就自己找了一下!
解决方法
使用QwtScaleDraw
中的setMinimumExtent
设置坐标轴计算的最小范围约束,最理想的情况是进行动态绑定操作,但是我不会就是😂,得亏发现这个方法
Qwt源码:
/*!
\brief Set a minimum for the extent
The extent is calculated from the components of the
scale draw. In situations, where the labels are
changing and the layout depends on the extent (f.e scrolling
a scale), setting an upper limit as minimum extent will
avoid jumps of the layout.
\param minExtent Minimum extent
\sa extent(), minimumExtent()
*/
void QwtAbstractScaleDraw::setMinimumExtent( double minExtent )
{
if ( minExtent < 0.0 )
minExtent = 0.0;
d_data->minExtent = minExtent;
}
/*!
Calculate the width/height that is needed for a
vertical/horizontal scale.
The extent is calculated from the pen width of the backbone,
the major tick length, the spacing and the maximum width/height
of the labels.
\param font Font used for painting the labels
\return Extent
\sa minLength()
*/
double QwtScaleDraw::extent( const QFont &font ) const
{
double d = 0;
if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
{
if ( orientation() == Qt::Vertical )
d = maxLabelWidth( font );
else
d = maxLabelHeight( font );
if ( d > 0 )
d += spacing();
}
if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
{
d += maxTickLength();
}
if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
{
const double pw = qMax( 1, penWidth() ); // pen width can be zero
d += pw;
}
d = qMax( d, minimumExtent() );
return d;
}
使用:
//具体设置范围依据实际情况约束,我这里50足够了
qplot_place->axisScaleDraw(QwtPlot::yLeft)->setMinimumExtent(50);
qplot_motor->axisScaleDraw(QwtPlot::yLeft)->setMinimumExtent(50);
效果: