Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Handle pixel density in css.
QtWS25 Last Chance

Handle pixel density in css.

Scheduled Pinned Locked Moved General and Desktop
scalingcssresolutionstylesheet
8 Posts 4 Posters 5.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PharmAccess
    wrote on last edited by
    #1

    Hello,

    I have a QT application which uses stylesheets heavily. It should maintain size across various pixel density displays. I read somewhere that using pt instead of px as units would scale graphics to physical size but this did not work.

    The only other solution I can think of is getting the physicalDotsPerInch property from the display and manually parse my stylesheets to scale all the pixel sizes. This is far from ideal due to obvious performance reasons.

    Would anyone happen to know the "proper" way to handle high dpi displays?

    Thanks alot.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PharmAccess
      wrote on last edited by PharmAccess
      #2

      In the end it seems there is no better way, I ended up modifying my css at runtime.
      For others strugling with this, heres how i did it:

      QString cssScale(QString text)
      {
      	QRegExp regex("\\[([0-9]+)\\]");
      	int index;
      	while((index = text.indexOf(regex)) != -1)
      	{
      		text.remove(index, regex.cap(0).length());
      		text.insert(index, QString::number(
      							regex.cap(1).toInt()*floor(dpi_factor)));
      	}
      	return text;
      }
      

      You can get dpi_factor like this:

      double dpi_factor = QApplication::primaryScreen()->physicalDotsPerInch()/109;
      

      109 is the dpi where your css works without modification.

      Then you can write css like this

      width: [5]px;
      

      and the [5] will be replaced by a size scaled to the screen.

      Its not ideal but it works.

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        Hi,

        A more simple solution would be to write your style sheet with placeholder to the value you want to change so you can use QString's arg method to put the correct values in place.

        QString styleSheet("width: %1").arg(width);
        
        

        Hope it helps

        [edit: Fixed code following @mcosta correction SGaist]

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          little correction. The right syntax should be

          QString styleSheet("width: %1").arg(width);
          

          in QString::arg() placeholders shall be in the range 1-99

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mcosta Right ! Python's influence :D

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6

              @SGaist I know, Python sometimes brakes C++ skills :D

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                A more simple solution would be to write your style sheet with placeholder to the value you want to change so you can use QString's arg method to put the correct values in place.

                QString styleSheet("width: %1").arg(width);
                
                

                Hope it helps

                [edit: Fixed code following @mcosta correction SGaist]

                P Offline
                P Offline
                PharmAccess
                wrote on last edited by PharmAccess
                #7

                @SGaist Thanks for the response.

                I just don't quite understand the following:
                A: How this works with arbitrary style sheets(different amounts of values to be replaced)?
                B: Can I only replace up to 100 unique sizes?
                C: Where does the initial value for width come from(the one I will multiply with dpi_factor)?

                Also would this method be more efficient performance wise?... I am running a lot of css through this function.

                Thanks again.

                1 Reply Last reply
                0
                • T3STYT Offline
                  T3STYT Offline
                  T3STY
                  wrote on last edited by
                  #8

                  CSS lets you use relative length units, see this:
                  http://www.w3schools.com/cssref/css_units.asp

                  As suggested in that link before, em and rem create perfectly scalable sizes. Don't let the name and description fool you: although the size refers to the size of the M character, they can be applied to width, height and any other CSS property as well.

                  Also, if you use a main CSS class like this:

                  main_class { width: 1em; height: 1em; font-size: 10em; }
                  

                  and all the other objects (or CSS styles) are childs of this class:

                  main_class.div { .... }
                  // or something like this in HTML
                  <BODY class="main_class">
                      <div class="other_class">something</div>
                  </BODY>
                  // in C++ the BODY widget would the parent of the div widget
                  

                  You can then only change the main_class sizes, and all the child items of that class will scale accordingly to their parent. Suppose you have this CSS for that div element before:

                  other_class { width: 10em; height: 4em; font-size: 1.3em; }
                  

                  and suppose that the main_class CSS is this:

                  main_class { width: 3em; height; 3m; font-size: 10em; }
                  

                  then the real sizes of that other_class are actually:

                  width: 30 em;  // 3 * 10
                  height: 12 em;  // 3 * 4
                  font_size: 13 em; // 10 * 1.3
                  

                  because they have been scaled by 3, 3 and 10ems, respectively. Using ems is for sure the best way in CSS to scale items proportionally.

                  I hope this helps :)

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved