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:
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;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
I can then use it similar to absolute values:
width: Geo.dp(50)
width: Geo.dp(50)
To copy to clipboard, switch view to plain text mode
"screenPixelDensity" is set as a context property by c++:
m_engine.rootContext()->setContextProperty("screenPixelDensity", QGuiApplication::primaryScreen()->physicalDotsPerInch());
m_engine.rootContext()->setContextProperty("screenPixelDensity", QGuiApplication::primaryScreen()->physicalDotsPerInch());
To copy to clipboard, switch view to plain text mode
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?
Bookmarks