Results 1 to 3 of 3

Thread: physical dots per inch in a .pragma library

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default 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:

    Qt Code:
    1. function dp(dpVal) {
    2. var px = Math.round(dpVal * (screenPixelDensity *0.00625 )); // 1 * 0.00625 === 1 / 160
    3. if(Qt.platform.os == "windows" || Qt.platform.os == "mac") {
    4. return px*2;
    5. } else {
    6. return px;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    I can then use it similar to absolute values:
    Qt Code:
    1. width: Geo.dp(50)
    To copy to clipboard, switch view to plain text mode 

    "screenPixelDensity" is set as a context property by c++:
    Qt Code:
    1. 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?
    Last edited by sedi; 20th October 2016 at 00:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default 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!!

Similar Threads

  1. width of a curve? in Inch ?
    By lwz in forum Qwt
    Replies: 0
    Last Post: 21st December 2013, 04:13
  2. Replies: 2
    Last Post: 3rd September 2013, 15:40
  3. pixel to inch or mm
    By lzpmail in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2011, 03:39
  4. OS/X how to get physical display handle
    By hvengel in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2009, 20:51
  5. Strange Unit em - same as inch? on XSLT-Fo format
    By patrik08 in forum General Programming
    Replies: 3
    Last Post: 16th July 2007, 00:46

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.