Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved Custom measurement units in stylesheets?

    General and Desktop
    4
    7
    302
    Loading More Posts
    • 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.
    • quiret
      quiret last edited by quiret

      Hello Qt developers,

      can Qt stylesheets be extended to support a custom measurement unit called "cm" so whenever it is used in a stylesheet and the QWidget is to be rendered, a user specified function is called to convert it to actual pixel size?

      Example stylesheet (sample.shell):

      QWidget {
          width: 5cm;
      }
      

      Example code (pseudo C++):

      static int ConvertUnitToPixels( int measurement_value )
      {
          return ( measurement_value * global_averageDPIForAllScreens );
      }
      
      (...)
      
      QApplication::registerStyleSheetMeasurement( "cm", ConvertUnitToPixels );
      QApplication::setStyleSheet( "sample.shell" );
      

      If possible it would be great to have a property-name argument to ConvertUnitToPixels so different calculations can be performed for width and height stylsheet properties.

      Thank you in advance!

      • Martin

      Magic.TXD, an advanced Qt-based RW TXD editor: https://osdn.net/projects/magic-txd/

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi
        Sadly that is not really possible to extend it that way.
        Would be cool with user functions but as far as i know, there is no such concepts.

        Also in most cases, the size of the Widgets are controlled by layouts so such
        "width" in style sheet would not have much effect for many use cases.

        I assume you wish for this to hep doing DPI scaling ?

        quiret 1 Reply Last reply Reply Quote 0
        • quiret
          quiret @mrjj last edited by quiret

          @mrjj said in Custom measurement units in stylesheets?:

          Hi
          Sadly that is not really possible to extend it that way.
          Would be cool with user functions but as far as i know, there is no such concepts.

          Also in most cases, the size of the Widgets are controlled by layouts so such
          "width" in style sheet would not have much effect for many use cases.

          I assume you wish for this to hep doing DPI scaling ?

          Thank you for the very quick reply, mrjj!

          Since you have asked for clarification let me provide thoroughly. There are some instances in which min-width and min-height are used in our stylesheets and have an effect, for example:

          QMenuBar
          {
              min-height: 52px;
              background-color: transparent;
              font-family:"Segoe UI Light";
              font-size: 26px;
              color: #050018;
              border: none;
          }
          
          QMenuBar::item
          {
              spacing: 0px; /* spacing between menu bar items */
              padding: 1px 20px;
              background: transparent;
          }
          
          QScrollBar::handle:vertical
          {
              background: #bbbbbb;
              min-height: 20px;
          }
          
          QPushButton
          {
              background-color: rgba(255, 255, 255, 0);
              border-style: outset;
              border-width: 1px;
              border-color: #050018;
              font-family:"Segoe UI Light";
              font-size: 20px;
              color: #050018;
              min-height: 30px;
              padding: 0px 15px 4px;
          }
          
          (...)
          

          Please note that the same applies to padding and margin.

          You are correct. We do use layouts for the basic look of our Qt applications. Many sizes are calculated at runtime but it could be cool to have customization through stylesheets at least.

          To answer your question: Yes DPI scaling is the issue. I heard of classes like QScreen which might be helpful. But what to do if there are multiple screens. Just to catch all cases: and what if user plugs in another screen in the middle of program execution?

          Magic.TXD, an advanced Qt-based RW TXD editor: https://osdn.net/projects/magic-txd/

          1 Reply Last reply Reply Quote 1
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            Hi,

            You can store a "template" of your style sheet and then fill the blanks at runtime before applying it.

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

            quiret 1 Reply Last reply Reply Quote 2
            • quiret
              quiret @SGaist last edited by

              @SGaist Good idea! Might actually think of tokens that are never valid in Qt stylesheets and use them in a custom stylesheet preprocessing system.

              Magic.TXD, an advanced Qt-based RW TXD editor: https://osdn.net/projects/magic-txd/

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                QString uses the % notation like in Python so you can also build on that.

                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 Reply Quote 1
                • S
                  SimonSchroeder last edited by

                  I am not entirely sure if physical units make sense for sizes. The actual physical size usually depends on the kind of device you are using: people hold phones closer than tablets, and tablets are closer than regular computer screens. However, fonts are usually set up to be comfortable for the distance and size. People with eye sight problems might adjust the font size according to their needs. I believe that the font size is a good measurement for size. And Qt supports that with the em-unit in stylesheet. Maybe you want to try this out.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post