Hello everyone, I've encountered a strange phenomenon. The area defined by QCPAxisRect->rect() is vertically offset by one pixel compared to the area enclosed by QCPAxis.
Here is my test code, which is quite simple.
this->resize (1000, 800); QCustomPlot* customPlot = new QCustomPlot(this); QVBoxLayout* layout = new QVBoxLayout (this); layout->addWidget (customPlot); this->show(); //customPlot->replot(); QRect rect= customPlot->axisRect()->rect(); qDebug () << "rect:" << "|left:" << rect.left () << "|top:" << rect.top () << "|right:" << rect.left () + rect.width () << "|bottom:" << rect.top () + rect.height ();
To visualize the position where QCPAxis lines are drawn, I added output statements in the function void QCPAxisPainterPrivate::draw(QCPPainter *painter). Below is a snippet of the code.
// draw baseline:
QLineF baseLine;
painter->setPen(basePen);
if (QCPAxis::orientation(type) == Qt::Horizontal)
baseLine.setPoints(origin+QPointF(xCor, yCor), origin+QPointF(axisRect.width()+xCor, yCor));
else
baseLine.setPoints(origin+QPointF(xCor, yCor), origin+QPointF(xCor, -axisRect.height()+yCor));
if (reversedEndings)
baseLine = QLineF(baseLine.p2(), baseLine.p1()); // won't make a difference for line itself, but for line endings later
painter->drawLine(baseLine);
qDebug () <<baseLine;
The snippet of input is as follows:
rect: |left: 16 |top: 15 |right: 963 |bottom: 758 QLineF(QPointF(16,757),QPointF(963,757)) QLineF(QPointF(16,757),QPointF(16,14))
For easier viewing, roughly as follows (it would be great if I could share an image)
AxisRect:
(16,15)----------(935,15)
| |
(16,758)----------(935,758)
enclosed by QCPAxis:
(16,14)----------(935,14)
| |
(16,757)----------(935,757)
The area enclosed by QCPAxis is offset by one pixel upwards compared to the actual axis rectangle!!!
Is this intentional or a bug?
there is a addtional infromation in https://doc.qt.io/qt-6/coordsys.html,may help to understand:
Note that for historical reasons the return value of the QRect::right() and QRect::bottom() functions deviate from the true bottom-right corner of the rectangle.
QRect's right() function returns left() + width() - 1 and the bottom() function returns top() + height() - 1. The bottom-right green point in the diagrams shows the return coordinates of these functions.