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);
}