physical dots per inch in a .pragma library
Hi,
my project relies heavily on QtQuick and is supposed to work on very different screen sizes.
To have reasonable sizes of my items, I import this .js file into every qml document:
Code:
function dp(dpVal) {
var px = Math.round(dpVal * (screenPixelDensity *0.00625 )); // 1 * 0.00625 === 1 / 160
if(Qt.platform.os == "windows" || Qt.platform.os == "mac") {
return px*2;
} else {
return px;
}
}
I can then use it similar to absolute values:
"screenPixelDensity" is set as a context property by c++:
Code:
m_engine.rootContext()->setContextProperty("screenPixelDensity", QGuiApplication::primaryScreen()->physicalDotsPerInch());
Now, if I understand correctly, by using import, this .js is being copied into each .qml file insead of using one instance. So I've tried to define it as a .pragma library.
Unfortunately, I cannot access screenPixelDensity (or any other QML varable) from the .js if I do so.
Is there a possibility to access the screen pixel density value from within js?
Re: physical dots per inch in a .pragma library
Since this calculation is based on two values from C++, the physical dots per inch and the platform, my suggestion would be to just move the function to C++ as well.
It could even do the OS lookup just once, or not all all and decide the factor at compile time.
Cheers,
_
Re: physical dots per inch in a .pragma library
Interesting idea! This solution is located outside my former box of thinking :-)
Sounds perfectly reasonable to do it in C++. I'm not sure of the speed penalty (it' not necessarily a constant , at runtime the user might change screen resolution), of but calling a c++ INVOKABLE shouldn't be worse than calling a js, I guess. Thank you!
I also wasn't actually aware that Qt.platform.os is a C++ thing. Good to know! Cheers!!