QCustomPlot Discussion and Comments

devicePixelRatio - High DPI SupportReturn to overview

Hi,

I have been using QCP for a couple of years, and it works really well. A few days ago I have been looking at supporting high dpi monitors, and noticed that QCP2 beta has support for that. I gave it a try and it mostly works, but there are a few problems.

One problem is that the text at the axis ticks seems to be drawn at lower resolution and scaled up. Here is a screenshot that shows the issue with a pixelratio of 2:

http://home.vedder.se/tmp/axis.png

The other problem is minor and easy to fix. It is that devicePixelRatio is not initialized correctly when it is not an integer and has to be set manually. Fixing that can be done by changing the line

setBufferDevicePixelRatio(QWidget::devicePixelRatio());

to (notice the F in the end)

setBufferDevicePixelRatio(QWidget::devicePixelRatioF());

It would be great if you can fix the problem with the axis text or let me know if there is a workaround.

A tip for experimenting with the devicePixelRatio is putting this in the main function before the QApplication object is created:

QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
qputenv("QT_SCALE_FACTOR", "1.5");

Keep up the good work!

Benjamin

I am confused as to how the Windows display scaling (Right click Desktop -> Display settings -> Change the size of text...) interacts with the devicePixelRatioF() in Qt. It seems no matter how I scale Windows, the Qt function returns 1.0.

If I set Windows scaling to 125%, I get 1.0 returned from devicePixelRatioF().

However, the Qt app I am testing is clearly drawn to a different scale than it appears with 100% Windows scaling.

If I use your last code bit above in my main() function, then devicePixelRatioF() returns the QT_SCALE_FACTOR as set by qputenv().

I don't use windows, but it sounds to me like all that its scaling does is changing the font size. What I have noticed is that changing the font size globally does not work too well in Qt unless care is taken when making the UI, so I always set the QApplication font size to 11. You can actually use the font size to scale the Qt application with a trick, but I don't know if it is recommended or good practise:

double scale = 1.0;
// Create a temporary QApplication object in its own scope so that it gets deleted
// afterwards. This is needed for getting the system font size.
{
    QApplication tmp(argc, argv);
    double ptFont = tmp.font().pointSizeF();
    if (ptFont < 0.0) {
        ptFont = tmp.font().pixelSize();
    }
    
    if (ptFont > 11.0) {
        scale = ptFont / 11.0;
    }
}

// Use the derived scale factor
qputenv("QT_SCALE_FACTOR", QString::number(scale).toLocal8Bit());

// Create the QApplication object to use
QApplication a(argc, argv);

// Ignore the OS font settings and do scaling with the method above.
QFont font = qApp->font();
if (font.pixelSize() > 0) {
    font.setPixelSize(11);
} else {
    font.setPointSize(11);
}
qApp->setFont(font);

// Create the main window and start the event loop

MainWindow w;
w.show();

return a.exec();

Regarding the QCP devicePixelRatio, I have noticed that plotting gets a lot slower if it is not equal to 1. This really is a problem...