Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom measurement units in stylesheets?



  • 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

  • Lifetime Qt Champion

    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 ?



  • @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?


  • Lifetime Qt Champion

    Hi,

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



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


  • Lifetime Qt Champion

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



  • 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.


Log in to reply