我使用以下代码在qt中绘制图形的轴:
...
static const std::array<const QPointF,10*2> horizontalAxis = {
QPointF(0.f,0.f), QPointF(1.f,0.f),
// Horizontal scales
QPointF(0.f,0.f), QPointF(0.f,1.f),
QPointF(0.125f,0.f), QPointF(0.125f,1.f),
QPointF(0.25f,0.f), QPointF(0.25f,1.f),
QPointF(0.375f,0.f), QPointF(0.375f,1.f),
QPointF(0.5f,0.f), QPointF(0.5f,1.f),
QPointF(0.625f,0.f), QPointF(0.625f,1.f),
QPointF(0.75f,0.f), QPointF(0.75f,1.f),
QPointF(0.875f,0.f), QPointF(0.875f,1.f),
QPointF(1.f,0.f), QPointF(1.f,1.f)
};
static const std::array<const QPointF,6*2> verticalAxis = {
QPointF(0.f,0.f), QPointF(0.f,1.f),
// Vertical scales
QPointF(0.f,0.f), QPointF(-1.f,0.f),
QPointF(0.f,0.25f), QPointF(-1.f,0.25f),
QPointF(0.f,0.5f), QPointF(-1.f,0.5f),
QPointF(0.f,0.75f), QPointF(-1.f,0.75f),
QPointF(0.f,1.f), QPointF(-1.f,1.f)
};
...
void Plot::paint(QPainter& p)
{
int const width = p.device()->width();
int const height = p.device()->height();
Q_ASSERT(width > xpaddingLeft + xpaddingRight);
Q_ASSERT(height > ypaddingTop + ypaddingBottom);
if(dirty) {
recalculate();
}
p.save();
int const bottomLeftX = xpaddingLeft;
int const bottomLeftY = height - ypaddingBottom;
int const scaleX = width - xpaddingLeft - xpaddingRight;
int const scaleY = height - ypaddingTop - ypaddingBottom;
p.setRenderHint(QPainter::HighQualityAntialiasing,false);
p.setRenderHint(QPainter::Antialiasing,false);
QPen pen(QBrush(QColor::fromRgb(0,0,0)), 2);
pen.setCosmetic(true); // Do not apply scale to the pen
p.setPen(pen);
// Horizontal Axis
p.resetTransform();
p.translate(bottomLeftX,bottomLeftY); // Origin at bottom left
p.scale(scaleX,scaleSize); // y-axis points down. Only scale y to match the scale-mark size
p.drawLines(&horizontalAxis[0], (int)horizontalAxis.size());
// Vertical Axis
p.resetTransform();
p.translate(bottomLeftX,bottomLeftY);
p.scale(scaleSize,-scaleY); // y-axis points up. Only scale x to match the scale-mark size
p.drawLines(&verticalAxis[0], (int)verticalAxis.size());
...
}
这将导致产生如下所示的图形,其中的lhs带有奇怪的伪像(仅显示未加载数据的轴):
水平轴刻度会导致LHS出现奇怪的伪影
注释掉所有用于水平比例尺定义的QPointF定义都可以消除此怪异的伪像。但是,评论任何一对观点似乎都不会对其产生影响。如果我从使用drawLines()方法切换为如下所示,则拉伸消失。
//p.drawLines(&horizontalAxis[0], (int)horizontalAxis.size());
for(size_t k = 0; k < horizontalAxis.size()-1; k += 2) p.drawLine(horizontalAxis[k],horizontalAxis[k+1]);
我得到如下结果,没有任何伪像(仅显示未加载数据的轴):
水平轴刻度显示无切换
我是否缺少某些东西,或者这是drawLines()中的错误?
我再次使用
drawLines()
函数绘制垂直轴和实际数据(作为折线图)而没有(明显)问题,但我想更加确定的是,在我没有的情况下,它不会导致奇异的伪像设法测试。 最佳答案
您正在使用的drawLines()
重载是(来自Qt5文档):
QPainter::drawLines (const QPointF* pointPairs, int lineCount)
请允许我注意第二个参数。它被称为
lineCount
,而不是点数,因此您必须传递一半的点数:assert (0 == horizontalAxis.size () % 2);
p.drawLines (&horizontalAxis[0], static_cast<int> (horizontalAxis.size () / 2));