QPainter painter;painter.begin(this);qreal scale = 1.0 / this->devicePixelRatioCache;painter.scale(scale, scale);
I need very high pixel granularity accuracy because in my project, one pixel represents several nanometers for measurement tools. Pixel accuracy is crucial for me. However, with QT QPainter, the coordinates often place the (0,0) point outside the canvas and position (1,1) at the (0,0) point. Sometimes the calculated canvas pixel width does not match the actual measured width. How can I solve this problem?
//I have tried to find various patterns to see if they can cover all situations, but I found that they can only cover some cases. In some situations, pixel accuracy errors still occur.inline char getMapFromRootXyStatus(qreal v) { double mod = std::fmod(v, 1); if (mod == 0) { return 'c'; } return mod < 0.5 ? 'l' : 'r';}PixelCanvasHelper::PixelCanvasHelper(QWidget *curr): currWidget(curr) {}qint32 PixelCanvasHelper::canvasWidth() { qreal realWidth = currWidget->width() * this->devicePixelRatioCache; if (currWidget == this->root) { return realWidth; } char wStatus = getMapFromRootXyStatus(realWidth); if (mapFromRoot.xStatus == 'l'&& wStatus == 'l') { return qint32(realWidth + 1); } else if (mapFromRoot.xStatus == 'r'&& wStatus == 'r') { return qint32(realWidth); } return qRound(realWidth);}qint32 PixelCanvasHelper::canvasHeight() { qreal realHeight = currWidget->height() * this -> devicePixelRatioCache; if (currWidget == this->root) { return realHeight; } char hStatus = getMapFromRootXyStatus(realHeight); if (mapFromRoot.yStatus == 'l'&& hStatus == 'l') { return qint32(realHeight + 1); } else if (mapFromRoot.yStatus == 'r'&& hStatus == 'r') { return qint32(realHeight); } return qRound(realHeight);}qint32 PixelCanvasHelper::canvasBeginX() { if (currWidget == this->root) { return 0; } return getMapFromRootXyStatus(this -> devicePixelRatioCache * this -> mapFromRoot.x) == 'r';}qint32 PixelCanvasHelper::canvasBeginY() { if (currWidget == this->root) { return 0; } return getMapFromRootXyStatus(this -> devicePixelRatioCache * this -> mapFromRoot.y) == 'r';}void PixelCanvasHelper::refreshMapFromRoot(bool onlyFirst) { this -> devicePixelRatioCache = currWidget->devicePixelRatio(); if (this->root == nullptr) { QWidget* w = currWidget; this -> root = w; while (true) { w = w->parentWidget(); if (w && !(w->windowFlags() & 0x7F)) { this -> root = w; continue; } break; } this -> devicePixelRatioCache = currWidget->devicePixelRatio(); } else if (onlyFirst) { return; } if (root == currWidget) { mapFromRoot.x = currWidget->x(); mapFromRoot.y = currWidget->y(); mapFromRoot.xStatus = 'l'; mapFromRoot.yStatus = 'l'; } else { QPointF p = currWidget->mapFrom(this->root, sem::ZERO_POINTF); mapFromRoot.x = std::abs(p.x()); mapFromRoot.y = std::abs(p.y()); mapFromRoot.xStatus = getMapFromRootXyStatus(mapFromRoot.x * this ->devicePixelRatioCache); mapFromRoot.yStatus = getMapFromRootXyStatus(mapFromRoot.y * this ->devicePixelRatioCache); }}
//The rectangles and lines I draw also have some coordinate deviations.inline QPoint sem::getDrawLinePoint(qint32 x, qint32 y, const QPoint& begin, qint32 penSize) { qint32 beginX = (begin.x() + penSize) / 2; qint32 beginY = (begin.y() + penSize) / 2; return QPoint(beginX + x, beginY + y);}inline QRect sem::getDrawRect(const QRect& rect, const QPoint& begin, qint32 penSize) { qint32 w = rect.width(); qint32 h = rect.height(); qint32 beginX = (begin.x() + penSize) / 2; qint32 beginY = (begin.y() + penSize) / 2; w = w - beginX - (penSize - beginX); h = h - beginY - (penSize - beginY); if (rect.width() > 0 && w == 0) { w = 1; } if (rect.height() > 0 && h == 0) { h = 1; } return QRect(beginX + rect.x(), beginY + rect.y(), w, h);}
This is the usage scenario inherited from PixelCanvas:
inline QRect getDrawRect(const QRect& rect, qint32 penSize=1) const{ return sem::getDrawRect(rect, QPoint(this->canvasBeginXInPainting(), this->canvasBeginYInPainting()), penSize); } inline QPoint getDrawLinePoint(qint32 x, qint32 y, qint32 penSize=1) { return sem::getDrawLinePoint(x, y, QPoint(this->canvasBeginXInPainting(), this->canvasBeginYInPainting()), penSize); }void XXXWindow::paintEvent(QPaintEvent* event) { refreshMapFromRoot(true); qint32 titleBar = this->getDevicePixelRatioCache() * this->titleBar; qint32 hw1 = HW1; qint32 hw2 = HW2; qint32 hw3 = HW3; painterBegin(); //If I understand correctly, when you directly use (0,0,canvasWidth, canvasHeight) to draw a rectangle, it may appear as (-1,-1) or (1,1), meaning that the left or top side of the rectangle is outside the screen area or not aligned with the edge but differs by 1 pixel. Therefore, you need the assistance of canvasBeginX and canvasBeginY. QRect rootRect = QRect(0,0,canvasWidthInPainting() - hw2, canvasHeightInPainting() - hw2); painter.setPen(*sem::borderPen); QRect r = getDrawRect(rootRect, hw3); painter.drawRect(getDrawRect(rootRect, hw3)); painter.setPen(*sem::borderShadowPen); painter.drawLine(getDrawLinePoint(1, rootRect.y() + rootRect.height(), hw2), getDrawLinePoint(canvasWidthInPainting() - 1, rootRect.y() + rootRect.height(), hw2)); painter.drawLine(getDrawLinePoint(rootRect.x() + rootRect.width(), 1, hw2), getDrawLinePoint(rootRect.x() + rootRect.width(), canvasHeightInPainting() - 1, hw2)); painter.setPen(*sem::borderGapPen); painter.drawLine(getDrawLinePoint(rootRect.x() + hw3, rootRect.y() + hw3), getDrawLinePoint(rootRect.width() - hw3 - hw1, rootRect.y() + hw3)); painter.drawLine(getDrawLinePoint(rootRect.x() + hw3, rootRect.y() + hw3), getDrawLinePoint(rootRect.x() + hw3, rootRect.height() - hw3 - hw1)); painter.setPen(*sem::borderGapPen2); painter.drawLine(getDrawLinePoint(rootRect.x() + hw3 + 1, rootRect.height() - hw3- 1), getDrawLinePoint(rootRect.width() - hw3 - 1, rootRect.height() - hw3 - 1)); painter.drawLine(getDrawLinePoint(rootRect.width() - hw3 - 1, rootRect.y() + hw3 + 1), getDrawLinePoint(rootRect.width() - hw3 - 1, rootRect.height() - hw3 - 1));...}