Details
-
Bug
-
Resolution: Fixed
-
P4: Low
-
None
-
5.15.0
-
None
-
Windows 10, 2K screen
Description
1. Problem Screenshot:
See attachments "problem screenshot".
2. Steps to Reproduce:
a. Device setup condition:
- Use a Windows device
- In display settings, set system scaling to a non-integer value (e.g. 125%)
If you cannot find the scaling setting, refer to the attached documents screenshot.
b. Code condition:
- Add the following code line
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
- Set Cache Mode in QGraphicsItem
MyGraphicsItem::MyGraphicsItem(QGraphicsItem* parent) : QGraphicsItem(parent) { // or ItemCoordinateCache, the specific cache mode is not important setCacheMode(DeviceCoordinateCache); }
3. Source Code Analysis:
In "/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp", I found this drawItemHelper:
void QGraphicsScenePrivate::drawItemHelper(QGraphicsItem *item, QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget, bool painterStateProtection) { // line 4423 const qreal devicePixelRatio = painter->device()->devicePixelRatio();
In QGraphicsScenePrivate::drawItemHelper, devicePixelRatio() is used instead of devicePixelRatioF().
devicePixelRatio() returns an integer by rounding down the ratio(1.25->1). devicePixelRatioF() returns a float preserving decimals.
This makes me wonder:
- For high-dpi displays with fractional scaling, is the integer ratio from devicePixelRatio() accurate enough?
- Should devicePixelRatioF() be used here to get the precise ratio with decimals?
- Or is there something else I need to set in my application code to properly handle high-dpi fractional scaling?
4. In conclusion
I would be grateful if Qt developers could validate the root cause and provide guidance.
To aid analysis, I have attached a demo project and screenshots demonstrating the problem. Please let me know if any other details are needed.