QCustomPlot Discussion and Comments

A pixel offset in axisRect : intentional or a bugReturn to overview

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.

This phenomenon has caused me some confusion. I need to draw a line segment within the AxisRect, and I have set this line using ptAxisRectRatio with a range from 0 to 1. In order to precisely span the entire axis rectangle, this phenomenon causes my line segment to extend beyond the lower axis boundary by one pixel.

Setting setClipToAxisRect addresses this issue, but my other functionalities prohibit this use. ):

Thanks for reporting!
QCustomPlot is intended to be tuned to pixel-perfect accuracy (which is also the reason we have the xCor and yCor variables in the axis draw method), which requires working around various quirks of the Qt painting system which unfortunately also change over the different Qt versions.

So no, this is not intentional. Could you please tell your platform, Qt version, and QCustomPlot version?

Thank you for response!
I didn't expect to get a response so quickly. It's truly a pleasant surprise!:)
my platform:Windows 10 (64-bit)
Qt version: 5.12.12
QCustomPlot version:2.1.1, the latest version currently.
Besides,my compiler version: msvs2019_64

Sorry, I made a mistake. My Qt version is 5.15.2