[HiDPI] What would be the best place to patch setenv GDK_DPI_SCALE into?

Currently I have put that workaround to cope with Gtk's hiDPI bug (fixed DPI=96) in login.conf(5), but I think there should be some logic in one of KDE's or Qt's startup scripts to set it if Qt is in HiDPI mode. Which one?
BTW Gtk2 apps work fine, only Gtk3 apps are way too small (or way too large iff run with an integer scale of 2). In contrast, GDK_DPI_SCALE can be set to a floating point number, i.e. I can compute it to be GDK_DPI_SCALE=myHiDPI/96.
Thx in advance for any suggestions.
https://ricostacruz.com/til/fractional-scaling-on-xorg-linux said:
There are 3 solutions to changing UI scaling in Xorg. Each one affects different things, so we'll need to combine them appropriately. These things can either scale text (ie, increase font sizes) or scale UI elements (ie, increase "pixels" in margins and paddings).

Also see chapter resources there.

Looks like this problem is a complex one for a one-patch-for-all-solution as there are multiple aspects.
I have it set in ~/.xinitrc so that I get correctly scaled checkboxes and stuff on 4k display:
export GDK_SCALE=2
export GDK_DPI_SCALE=0.5

And in ~/.Xresources (sourced using xrdb .Xresources from ~/.xinitrc):
Xft.dpi:    192

I also have DPI set in /usr/local/etc/X11/xorg.conf.d/nvidia.conf (just in case!) using:
Option "DPI" "192x192"

All that gives me nicely scaled GTK apps (especially, firefox and thunderbird).
I have it set in ~/.xinitrc so that I get correctly scaled checkboxes and stuff on 4k display:
[...] And in ~/.Xresources (sourced using xrdb .Xresources from ~/.xinitrc): [...]
That's a per-user workaround; I already have a workaround for all users by putting GDK_DPI_SCALE in login.conf(5). But these are not universal correct solutions, because the values are computed manually a priori and if we put our disks into another system with a different monitor's resolution, the scaling will be wrong.
I also have DPI set in /usr/local/etc/X11/xorg.conf.d/nvidia.conf (just in case!) using: [...]
The resolution (measured in [DPI]) is a property of the monitor, not of the graphics card/GPU. Once I set the EnableHiDPI=true in the [X11] section of sddm.conf(5), everything related is set up automagically (the Xserver gets the correct DPI and physical dimensions from the monitor's EDID anyway), except the GDK_DPI_SCALE is not set up (computed from (monitor's DPI)/(Gtk's fixed DPI); the latter is usually fixed to 96, but maybe there are circumstances when it is 72?
Clarification: the realm is: set up correct scaling for Gtk3 apps in a Qt-based desktop environment (KDE, LxQt & IIUC Lumina), when Qt's HiDPI is active. I'm searching for the right place to patch in this computation & set GDK_DPI_SCALE, so that it works for any (most) systems.
EDIT maybe the only universal solution is to fix Gtk3. Rationale: setting one scaling parameter will not work correctly for a multi-monitor setup, where the monitors have different resolutions.


If you want to configure it in a flexible way so it would work when you connect a different monitor or put the disk in a different machine, then you have to make the calculation with the original DPI value that Xorg gets from the EDID of the monitor. Something like this:
export GDK_DPI_SCALE=$(xdpyinfo | awk -F'[ x]+' '/resolution/ {print $3 / 96; exit}')
If your monitor has 145 DPI, that will set the variable to 1.51. Of course, you must not hardcode the monitor’s DPI anywhere, so the X server really takes the correct value from the monitor itself. Writing hardcoded DPI values in xorg.conf, Xresources or anywhere else is almost always the wrong thing to do.

However, as you have mentioned, that won’t work correctly if you have multiple screens with different resolutions. The above snippet only takes the first screen into account. Indeed, the only way to handle multi-monitor setups reliably is to fix the problem in GTK3.